-
-
Save durka/b16614c262795ba394a8 to your computer and use it in GitHub Desktop.
Rust star operator
This file contains hidden or 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
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