将闭包传递给特征方法:期望的类型参数,找到的闭包

西蒙·杰克逊

我对如何使它正常工作有些困惑,我已经从真实的东西中删除了它。我写了一个特质:

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

的“子级”参数受其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;
    }
}

然后这些将用作:

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);
}

我收到错误消息:

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()

我设置了一个最小的游乐场示例,在此处重现了问题:https : //play.rust-lang.org/?gist=bcc8d67f25ac620fe062032d8737954b&version=stable&backtrace=0

弗朗西斯·加涅

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

为了使它起作用,add_child的签名应如下所示:

pub fn add_child<REND>(&mut self, child: &mut REND)
    where REND: Renderable<AddChildCallback>

AddChildCallback具体类型的名称在哪里(实现Fn(&PropertyTags))。

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

我还将注意到,通过将回调类型设置为类型参数,Renderable在设置了第一个回调之后不能为分配其他类型的回调,因为第一个回调将确定的具体类型Renderable这可能适合您的用法,我只是想确保您知道这一点。

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

pub fn add_child<REND>(&mut self, child: &mut REND)
    where REND: Renderable<Box<Fn(&PropertyTags)>>

是更新的游乐场链接,其中包含的示例实现Fn注意,对于这些参数callcall_mutcall_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 {
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将参数传递给函数闭包

将闭包传递给期望std :: ops :: Fn的函数

尾随闭包传递给类型'Set <HTTPMethod>'的参数,该参数不接受闭包

尾随闭包传递给不接受闭包错误的“MKCoordinateRegion”类型参数

groovy ..将闭包作为参数传递给另一个闭包

如何将引用参数传递给盒装闭包?

将具体实例传递给需要协议参数的闭包

将变量传递给闭包

将 args 传递给 StreamingMarkupBuilder 的闭包

如何将方法传递给采用闭包的变量?

无法将闭包作为参数传递

将(迭代器)值传递给闭包(预期类型参数`I`,找到结构`std::slice::Iter`)有什么问题?

谁将参数传递给 complition 处理程序闭包的参数?

将类型传递给函数以返回返回该类型的闭包?

在swift中将多个闭包作为参数传递给函数

Jenkins/Groovy:如何将命名参数和闭包传递给同一个方法?

错误:预期的类型参数,找到了闭包

闭包:预期的u32找到类型参数

javascript,将参数传递给函数内部的闭包内的回调

将闭包捕获构造函数参数传递给基类构造函数

遍历将闭包传递给过程元素的列表-Groovy

如何将 Arc 克隆传递给闭包?

将JavaScript传递给JavaScript中的闭包

无法将闭包传递给 `Hyper::service_fn`

将数据传递给Laravel 4中的闭包

将可选的闭包传递给视图

Swift 以参数作为参数传递闭包

Swift:如何将闭包作为函数参数传递

如何传递闭包以将 Ruby 作为参数发送