Rust 正在将闭包传递给trait方法:需要类型参数,找到了闭包
我有点困惑于如何让它工作,我已经把它从真实的东西中删掉了。我写了一个特点:Rust 正在将闭包传递给trait方法:需要类型参数,找到了闭包,rust,closures,Rust,Closures,我有点困惑于如何让它工作,我已经把它从真实的东西中删掉了。我写了一个特点: pub trait Renderable<F: Fn(&PropertyTags)> { fn set_property_changed_callback(&mut self, callback: Option<F>); } 然后这些将被用作: pub fn add_child<REND, C>(&mut self, child: &mut R
pub trait Renderable<F: Fn(&PropertyTags)> {
fn set_property_changed_callback(&mut self, callback: Option<F>);
}
然后这些将被用作:
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);
}
pub fn add_child(&mut self,child:&mut REND)
式中C:Fn(&PropertyTags),
撕裂:可渲染
{
设tc=Some(| property_标记:&PropertyTags|{
});
设置\属性\更改\回调(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()
child.set\u property\u changed\u回调(tc);
|^^应为类型参数,找到了闭包
|
=注意:应为'std::option::option'类型`
=注意:找到类型“std::option::option”`
=帮助:以下是一些可能满足您需要的功能:
-.take()
-.unwrap()
我已经设置了一个最小的游乐场示例,它再现了这里的问题:问题是
add\u child
声明接受任何Renderable
,其中C
可以是实现Fn(&PropertyTags)
的任何类型,但是,该函数尝试为其指定一个特定的闭包类型,该类型可能与C
不同
为了使其正常工作,add_child
的签名应如下所示:
pub fn add_child<REND>(&mut self, child: &mut REND)
where REND: Renderable<AddChildCallback>
pub fn add_child<REND>(&mut self, child: &mut REND)
where REND: Renderable<Box<Fn(&PropertyTags)>>
是一个更新的游乐场链接,带有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 {
}
}
struct RenderableCallback{
}
RenderableCallback的impl{
extern“rust call”fn call(&self,args:(&a PropertyTags,)->self::Output{
}
}
RenderableCallback的impl{
extern“rust call”fn call_mut(&mut self,args:(&'a PropertyTags,)->self::Output{
}
}
RenderableCallback的impl{
类型输出=();
extern“rust call”fn call_一次(self,args:(&'a PropertyTags,)->self::Output{
}
}
如果这是一个重复的问题,我很抱歉,我已经阅读了一系列关于SO的类似问题,但没有一个能帮助我解决它。您需要包含更多代码,以便我们能够测试它。与此同时,你看到了吗?您可能需要将闭包包装在一个框中
。我不确定这是同一个问题,因为他们试图返回闭包。我希望避免将其装箱,我认为这将使其动态调度?只是澄清一下,可以分配不同的AddChildCallback,但不能分配Fn(&PropertyTags)的不同具体实现?
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 {
}
}