Closed
Description
Something like this:
#![feature(unboxed_closures, box_syntax)]
#![allow(unstable)]
struct Proxy<'a, Args, Ret> {
closure: Box<Fn<Args, Ret> + 'a>
}
impl<'a, Args, Ret> Fn<Args, Ret> for Proxy<'a, Args, Ret> {
extern "rust-call" fn call(&self, args: Args)->Ret {
self.closure.call(args)
}
}
impl<'a, Args, Ret> Proxy<'a, Args, Ret> {
fn new<F: Fn<Args, Ret> + 'a>(closure: F)->Proxy<'a, Args, Ret> {
Proxy { closure: box closure }
}
}
fn main(){
let celebrate = Proxy::new(|&: num: i64| {
println!("Feels great to implement C++ std::function in {} lines.", num)
});
let lucy_number = Proxy::new(|&: num: i64| {
println!("My lucky number is {}", num)
});
let func = if std::rand::random() { celebrate } else { lucy_number };
func(25)
}
Or is it part of the plan? :p
Metadata
Metadata
Assignees
Labels
No labels