Created
December 3, 2016 06:07
-
-
Save flukejones/dba3e95d9de5cb978d47ac9a678e912a to your computer and use it in GitHub Desktop.
A basic kind of callback macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// 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