Skip to content

Instantly share code, notes, and snippets.

Created December 4, 2016 20:56
Show Gist options
  • Save anonymous/af57f1ea1be5d7757e6f6adf83d89e9e to your computer and use it in GitHub Desktop.
Save anonymous/af57f1ea1be5d7757e6f6adf83d89e9e 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