Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis
Created December 15, 2014 17:21
Show Gist options
  • Save nikomatsakis/b36e5240e33a13757971 to your computer and use it in GitHub Desktop.
Save nikomatsakis/b36e5240e33a13757971 to your computer and use it in GitHub Desktop.
#![feature(default_type_params)]
trait FnBox<A,R = ()> {
fn call_box(self: Box<Self>, arg: A) -> R;
}
impl<A,R,F> FnBox<A,R> for F
where F : FnOnce(A) -> R
{
fn call_box(self: Box<F>, arg: A) -> R {
(*self)(arg)
}
}
fn do_it<F:FnOnce(int) -> int>(f: F) -> int {
do_it_boxed(box f)
}
fn do_it_boxed(b: Box<FnBox<int,int>>) -> int {
b.call_box(22)
}
fn main() {
assert_eq!(do_it(|x| x * 44), 968);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment