Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created July 1, 2018 09:57
Show Gist options
  • Save Pzixel/baa7d58bf8de0e44e917c1a7bdbc9d82 to your computer and use it in GitHub Desktop.
Save Pzixel/baa7d58bf8de0e44e917c1a7bdbc9d82 to your computer and use it in GitHub Desktop.
#![feature(unboxed_closures, fn_traits)]
pub enum Either<L, R> {
Left(L),
Right(R),
}
impl<L: FnOnce() -> i32, R: FnOnce() -> i32> FnOnce<()> for Either<L, R> {
type Output = i32;
extern "rust-call" fn call_once(self, _args: ()) -> i32 {
match self {
Either::Left(l) => l(),
Either::Right(r) => r()
}
}
}
impl<L: FnMut() -> i32, R: FnMut() -> i32> FnMut<()> for Either<L, R> {
extern "rust-call" fn call_mut(&mut self, _args: ()) -> i32 {
match self {
Either::Left(l) => l(),
Either::Right(r) => r()
}
}
}
impl<L: Fn() -> i32, R: Fn() -> i32> Fn<()> for Either<L, R> {
extern "rust-call" fn call(&self, _args: ()) -> i32 {
match self {
Either::Left(l) => l(),
Either::Right(r) => r()
}
}
}
pub extern fn foobarbaz(b: bool) -> Either<impl Fn() -> i32, impl Fn() -> i32> {
if b {
Either::Left(|| 10)
} else {
Either::Right(|| 20)
}
}
fn main() {
println!("{}", foobarbaz(true)());
println!("{}", foobarbaz(false)());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment