rust - 当闭包作为特征对象参数传递时,为什么需要强制转换?
问题描述
我正在尝试创建一个应该抽象出具有不同数量参数的函数/闭包的特征。像这样的东西:
trait MyTrait {}
impl MyTrait for Box<Fn() -> &'static str> {}
impl MyTrait for Box<Fn(u8) -> u8> {}
最初我打算这样使用它:
fn myf<F: MyTrait>(_fun: F) {}
fn main() {
myf(Box::new(|i: u8| i + 2))
}
但是此代码失败并出现错误:
error[E0277]: the trait bound `std::boxed::Box<[closure@src/main.rs:11:18: 11:31]>: MyTrait` is not satisfied
当我像这样投射盒子时,一切都会正确编译:
myf(Box::new(|i: u8| i + 2) as Box<Fn(_) -> _>)
为什么 Rust 编译器不能在没有强制转换的情况下推断出这个特征?我的方法(使用演员表)是否正确,还是有更简单的方法?我更喜欢为我的项目启用trivial_casts
警告,这种语法会触发它。
解决方案
这是一个容易忘记的事情:每个闭包都是一个不同的结构,它实现Fn
:Fn
是一个特征,而不是一个结构,并且特征实现不是传递的。
这是一个说明这一点的小例子:
trait Base {}
trait Derived {}
struct Foo {}
impl Base for Derived {}
impl Derived for Foo {}
fn myf<T>(_t: Box<T>)
where
T: Base + ?Sized,
{
}
fn main() {
let foo = Box::new(Foo {});
//myf(foo) // does not compile
myf(foo as Box<Derived>)
}
你真正想做的是:
trait MyTrait {}
impl<T> MyTrait for T
where
T: Fn() -> &'static str,
{
}
impl<T> MyTrait for T
where
T: Fn(u8) -> u8,
{
}
fn myf<F>(_fun: Box<F>)
where
F: MyTrait,
{
}
fn main() {
myf(Box::new(|i: u8| i + 2))
}
但这无法编译,因为有两个相互冲突的实现。
推荐阅读
- sml - CM.make 找不到文件“smlnj-lib.cm”
- c# - 防止 Diagnostics.Process 在完成后关闭
- cassandra - Cassandra 中推荐的分区数
- bash - 如果 X & ( Y<1 or Z>2 ) 如何正确报告
- python - 如何绘制多边形每一侧的表面?
- c++ - 如何使用运算符编写可继承的模板类
- python-3.x - 如何将输入()与变量(列表)进行比较
- python - 如何为 AWS Sagemaker 托管的自定义 Tensorflow 模型使用多个输入
- angular - 如何从循环单选按钮获取数据
- javascript - Node.js 中的posenet TensorFlow 模型结果中没有位置细节(“x”、“y”)