#up #args #fn-onces

yanked fn_box

Box up your FnOnces

Uses old Rust 2015

1.0.5 Apr 1, 2015
1.0.4 Feb 12, 2015
1.0.2 Jan 13, 2015
1.0.1 Dec 22, 2014

#65 in #args

CC0 license

3KB
58 lines

Build Status

fn_box

Box up your FnOnces!

API

trait FnBox<Args, Result = ()> {
  extern "rust-call" fn call_box(self: Box<Self>, args: Args) -> Result;
}

impl<F, Args, Result> FnBox<Args, Result> for F where F: FnOnce<Args, Result> {}

#[cfg(not(when_coherence_is_fixed))]
impl<'a> FnOnce() for Box<FnBox() + 'a> {}

#[cfg(when_coherence_is_fixed)]
impl<'a, Args, Result> FnOnce<Args, Result> for Box<FnBox<Args, Result> + 'a> {}

Usage

let hello: Box<FnBox()> = Box::new(move || { println!("hello world!") });
hello();

let plus_one: Box<FnBox(_) -> _> = Box::new(move |x: i32| { x + 1 });
assert_eq!(plus_one.call_box((3,)), 4);

No runtime deps