Skip to content

Instantly share code, notes, and snippets.

@flukejones
Created December 3, 2016 06:07
Show Gist options
  • Save flukejones/dba3e95d9de5cb978d47ac9a678e912a to your computer and use it in GitHub Desktop.
Save flukejones/dba3e95d9de5cb978d47ac9a678e912a to your computer and use it in GitHub Desktop.
A basic kind of callback macro
///
/// A generic version of `callback macro`
///
/// # Example
/// ```rust
/// #[macro_use(callback)]
/// # extern crate framework;
/// trait Command {
/// fn run(&self) {}
/// }
/// struct Test { x: i32 }
/// impl Test {
/// fn new() -> Test { Test{ x: 42} }
/// }
/// impl Command for Test {
/// fn run(&self) { println!("{:?}", self.x); }
/// }
/// fn main() {
/// let mut t = Test::new();
/// let macro_test = callback!(Command::run);
/// println!("{:?}", macro_test(&mut t as &mut Command) )
/// }
/// ```
#[macro_export]
macro_rules! callback {
($t:ident :: $m:ident) => {{
fn __f(x: &mut $t) { x.$m() }
__f
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment