将闭包传递给 trait 方法:预期类型参数,找到闭包

Sim*_*son 5 closures rust

我对如何让这个工作感到有些困惑,我已经把它从真实的东西中剪下来了。我写了一个特征:

pub trait Renderable<F: Fn(&PropertyTags)> {
    fn set_property_changed_callback(&mut self, callback: Option<F>);
}
Run Code Online (Sandbox Code Playgroud)

其中 'child' 参数add_child受限制并且PropertyTags只是一个枚举。我已经包含了类型的模拟实现child来演示我的用法:

pub struct Child<F: Fn(&PropertyTags)> {
    property_changed_callback: Option<F>,
}

impl<F: Fn(&PropertyTags)> Renderable<F> for Child<F> {
    fn set_property_changed_callback(&mut self, callback: Option<F>) {
        self.property_changed_callback = callback;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后这些将用作:

pub fn add_child<REND, C>(&mut self, child: &mut REND)
    where C: Fn(&PropertyTags),
        REND: Renderable<C>
{
    let tc = Some(|property_tag: &PropertyTags|{
            });

    child.set_property_changed_callback(tc);
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

child.set_property_changed_callback(tc);
   |                                ^^ expected type parameter, found closure
   |
   = note: expected type `std::option::Option<C>`
   = note:    found type `std::option::Option<[closure@src/rendering/mod.rs:74:31: 76:18]>`
   = help: here are some functions which might fulfill your needs:
 - .take()
 - .unwrap()
Run Code Online (Sandbox Code Playgroud)

我已经设置了一个最小的操场示例,它在这里重现了这些问题:https : //play.rust-lang.org/?gist=bcc8d67f25ac620fe062032d8737954b&version=stable&backtrace=0

Fra*_*gné 4

问题是,add_child声称接受 any Renderable<C>,其中C可以是任何实现的类型Fn(&PropertyTags),但随后该函数尝试给它一个可能与 不同的特定闭包类型C

为了使其工作,add_child的签名应如下所示:

pub fn add_child<REND>(&mut self, child: &mut REND)
    where REND: Renderable<AddChildCallback>
Run Code Online (Sandbox Code Playgroud)

其中AddChildCallback是具体类型的名称(实现Fn(&PropertyTags))。

这里的困难在于,一方面,闭包类型没有可以在 Rust 代码中使用的名称,另一方面,Fn手动实现不稳定,因此需要夜间编译器。

我还要注意,通过将回调类型设置为类型参数,Renderable在设置第一个回调后,无法为 a 分配不同类型的回调,因为第一个回调将确定Renderable. 这可能适合您的使用,我只是想确保您意识到这一点。

如果您想要一个适用于稳定编译器(从 Rust 1.14.0 开始)的解决方案,那么您必须对回调进行装箱。add_child的签名将如下所示:

pub fn add_child<REND>(&mut self, child: &mut REND)
    where REND: Renderable<Box<Fn(&PropertyTags)>>
Run Code Online (Sandbox Code Playgroud)

是更新的 Playground 链接,其中包含Fn. call请注意, 、call_mut和的参数call_once作为元组传递,正如特征定义所要求的那样。为了完整性,代码复制如下:

struct RenderableCallback {

}

impl<'a> Fn<(&'a PropertyTags,)> for RenderableCallback {
    extern "rust-call" fn call(&self, args: (&'a PropertyTags,)) -> Self::Output {

    }
}

impl<'a> FnMut<(&'a PropertyTags,)> for RenderableCallback {
    extern "rust-call" fn call_mut(&mut self, args: (&'a PropertyTags,)) -> Self::Output {

    }
}

impl<'a> FnOnce<(&'a PropertyTags,)> for RenderableCallback {
    type Output = ();
    extern "rust-call" fn call_once(self, args: (&'a PropertyTags,)) -> Self::Output {
    }
}
Run Code Online (Sandbox Code Playgroud)