Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Created April 7, 2016 02:41
Show Gist options
  • Select an option

  • Save Sgeo/6055c51710245c6b43154bf2d9771152 to your computer and use it in GitHub Desktop.

Select an option

Save Sgeo/6055c51710245c6b43154bf2d9771152 to your computer and use it in GitHub Desktop.
trait Identity {
fn call<T>(&self, T) -> T;
}
macro_rules! id {
($closure:expr) => {{
struct ThisID;
impl Identity for ThisID {
fn call<T>(&self, t: T) -> T {
$closure(t)
}
}
ThisID
}}
}
fn wants_id<F: Identity>(f: F) {
f.call(5);
f.call("Hello");
}
fn main() {
wants_id(id!(|a| a));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment