Skip to content

Instantly share code, notes, and snippets.

@durka
Forked from anonymous/playground.rs
Created July 31, 2015 16:09
Show Gist options
  • Save durka/b16614c262795ba394a8 to your computer and use it in GitHub Desktop.
Save durka/b16614c262795ba394a8 to your computer and use it in GitHub Desktop.
Rust star operator
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