Skip to content

Provide wrappers over Box<Fn> #20878

Closed
Closed
@bombless

Description

@bombless

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions