Skip to content

Instantly share code, notes, and snippets.

@sophiajt
Last active March 16, 2016 19:48
Show Gist options
  • Save sophiajt/97bd4bba5ed273254944 to your computer and use it in GitHub Desktop.
Save sophiajt/97bd4bba5ed273254944 to your computer and use it in GitHub Desktop.
use std::any::Any;
#[derive(Debug)]
struct Engine {
x: i32
}
trait FnRegister1V {
fn register<T: Fn(U)->V, U, V>(&mut self, name: &str, f: T);
}
impl FnRegister1V for Engine {
fn register<T: Fn(U)->V, U, V>(&mut self, name: &str, f: T) {
self.x += 2;
}
}
trait FnRegister2V {
fn register<T: Fn(U, V)->W, U, V, W>(&mut self, name: &str, f: T);
}
impl FnRegister2V for Engine {
fn register<T: Fn(U, V)->W, U, V, W>(&mut self, name: &str, f: T) {
self.x += 2;
}
}
fn id(x: i32) -> i32 {
x
}
fn main() {
let mut engine = Engine { x: 0 };
engine.register("foo", id);
println!("Engine: {:?}", engine);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment