我如何使用特征对象进行函数回调?

Syn*_*ose 4 traits rust

我试图围绕特征对象以及如何使用它们.一种情况是我可能想要为回调传递函数,当满足某些条件时调用回调.

fn bind_callback( key: u64, /* pass function */) {
    // when key is matched with the event, call the function
}
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?我听说我可以使用trait对象这样的东西,但是我该如何实现呢?有人能告诉我一个例子吗?这就是我的意思:

trait Callback {
    fn callback(self);
}

fn pass_callback(f: &Callback) {
    f.callback();
}

fn run_me() {
    println!("Hello World!");
}

fn main() {
    pass_callback(&run_me); // run simple no arg void ret function
    pass_callback(|| println!("Hello World!")); // same thing
}
Run Code Online (Sandbox Code Playgroud)

我知道这是非常错误的,我试图理解我将如何完成这样的事情.我的错误输出是:

<anon>:14:19: 14:26 error: the trait `Callback` is not implemented for the type `fn() {run_me}` [E0277]
<anon>:14     pass_callback(&run_me);
                            ^~~~~~~
<anon>:14:19: 14:26 help: see the detailed explanation for E0277
<anon>:14:19: 14:26 note: required for the cast to the object type `Callback`
<anon>:15:19: 15:46 error: mismatched types:
 expected `&Callback`,
    found `[closure@<anon>:15:19: 15:46]`
(expected &-ptr,
    found closure) [E0308]
<anon>:15     pass_callback(|| println!("Hello World!"));
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:15:19: 15:46 help: see the detailed explanation for E0308
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
Run Code Online (Sandbox Code Playgroud)

She*_*ter 6

如果要将闭包和函数用作参数,则不能使用自己的特征.相反,你使用其中一个Fn*家庭:

fn pass_callback<F>(f: F)
    where F: Fn()
{
    f();
}

fn run_me() {
    println!("Hello World!");
}

fn main() {
    pass_callback(run_me);
    pass_callback(|| println!("Hello World!"));
}
Run Code Online (Sandbox Code Playgroud)

如果你真的想使用自己的特性,你可以,但是你需要在某些东西上实现特征并将该项传递给你的函数:

trait Callback {
    fn callback(&self, value: u8) -> bool;
}

struct IsEven;
impl Callback for IsEven {
    fn callback(&self, value: u8) -> bool {
        value % 2 == 0
    }
}

fn pass_callback<C>(f: C)
    where C: Callback
{
    if f.callback(42) {
        println!("Callback passed");
    }
}

fn main() {
    pass_callback(IsEven);
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你的评论

没有arg void ret功能

不太正确.Rust没有void"类型",它有空元组,通常称为单元类型.