Skip to content

Instantly share code, notes, and snippets.

Created July 28, 2015 00:14
Show Gist options
  • Save anonymous/aef8a9a4a97a0e89d773 to your computer and use it in GitHub Desktop.
Save anonymous/aef8a9a4a97a0e89d773 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
trait Star<Ret> {
type F: ?Sized;
fn star(self, f: &Self::F) -> Ret;
}
macro_rules! star_impl {
($($n:ident),*) => {
impl<Ret,$($n),*> Star<Ret> for ($($n,)*) {
type F = Fn($($n),*) -> Ret;
#[allow(non_snake_case)]
fn star(self, f: &Self::F) -> Ret {
let ($($n,)*) = self;
f($($n),*)
}
}
}
}
star_impl!();
star_impl!(A);
star_impl!(A, B);
star_impl!(A, B, C);
star_impl!(A, B, C, D);
star_impl!(A, B, C, D, E);
fn foo(i: isize, s: &str, b: bool) -> String {
format!("{} {} {}", i, s, b)
}
fn main() {
println!("{}", ().star(&|| "hi"));
println!("{}", (1,).star(&|x| x+5));
println!("{}", (1, "2").star(&|x, y| x+y.parse::<isize>().unwrap()));
println!("{}", (1, "2", true).star(&foo));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment