Skip to content

Instantly share code, notes, and snippets.

@flukejones
Forked from anonymous/playground.rs
Created December 4, 2016 20:57
Show Gist options
  • Save flukejones/1b5b860d046f569a95a03ead45bc4f54 to your computer and use it in GitHub Desktop.
Save flukejones/1b5b860d046f569a95a03ead45bc4f54 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![feature(core_intrinsics)]
pub trait Deserialize: Sized {
fn deserialize(String) -> Self;
}
struct Position(i32, i32);
struct Velocity(i32, i32);
impl Deserialize for Position {
fn deserialize(s: String) -> Self {
Position(5, 5)
}
}
pub struct EntityBuilder;
impl EntityBuilder {
pub fn with<T>(self, v: T) {
println!("test");
}
}
use std::collections::HashMap;
use std::intrinsics;
pub struct Deserializer {
pub map: HashMap<String, Box<Fn(EntityBuilder, String)>>,
}
fn main() {
let x = |e: EntityBuilder, s: String| {
e.with(Position::deserialize(s));
};
let mut deserializer = Deserializer { map: HashMap::new() };
deserializer.map.insert("Test".to_string(), Box::new(x));
let entity_builder = EntityBuilder;
let create = deserializer.map.get("Test").unwrap();
create(entity_builder, "Test".to_string());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment