如题。你可以吗?或者它是否需要一个中间步骤来进行特征转换?
我意识到关闭改革还没有完全完成,还有很多 Unresolved 问题。
但是,bug 跟踪器充满了 bug 和 RFC 以及指向过时和新票证的链接,我很难理解新的未装箱的 rust 闭包的运行状态。
这个有效:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test(bar:Box<FnOnce<(int,), ()> + Send>) {
bar.call_once((10,));
}
fn main() {
let bar:Box<FnOnce<(int,), ()> + Send> = box |:x:int| { println!("Hi:{}", x); };
test(bar);
test(box |:x:int| { println!("Hi:{}", x); });
}
这有效:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test2(mut bar:Option<Box<FnOnce<(int,), ()> + Send>>) {
bar.take().unwrap().call_once((10,));
}
fn main() {
let bar2:Box<FnOnce<(int,), ()> + Send> = box |:x:int| { println!("Hi:{}", x); };
test2(Some(bar2));
}
但是,由于某些原因这不起作用:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test2(mut bar:Option<Box<FnOnce<(int,), ()> + Send>>) {
bar.take().unwrap().call_once((10,));
}
fn main() {
test2(Some(box |:x:int| { println!("Hi:{}", x); }));
}
错误:
foo.rs:21:9: 21:53 error: mismatched types: expected `core::option::Option<Box<core::ops::FnOnce<(int,), ()>+Send>>`, found `core::option::Option<Box<closure>>` (expected trait core::ops::FnOnce, found closure)
foo.rs:21 test2(Some(box |:x:int| { println!("Hi:{}", x); }));
那么,问题:
1) 应该上面的代码有效吗?或者 Option<>
/Some()
暗示我在这种情况下还没有理解的复杂事物?
2) 如果是这样,是否有在 |:|
之间进行隐式转换的公开票证糖和FnOnce
类型?
(因为 https://github.com/rust-lang/rust/issues/18101 似乎建议应该这样做,这就是示例 #1 起作用的原因,但我已经完全迷失了试图通过那个元错误来找到任何引用闭包糖的特定错误)。
最佳答案
值得注意的是,这是特征对象和通用结构/枚举的一般属性,完全不特定于未装箱的闭包,FnOnce
, Option
或 |:|
(尤其不是这个,因为它只是创建实现特定特征的值的好方法)。
此外,最近的RFC #401是相关的。
1) Should the above code work? Or does Option<>/Some() imply something complex that I haven't understood in this context?
Box<FnOnce<..>>
是特征对象,因此与非动态 Box<F>
不具有相同的表示或类型对于 F: FnOnce<...>
.从理论上讲,我们可以通过某些类型的枚举和结构支持更高级的深度强制转换,但它不一定能很好地/统一地处理协方差和反方差。
在任何情况下,你都可以做一个普通的特征对象转换:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test2(mut bar:Option<Box<FnOnce<(int,), ()> + Send>>) {
bar.take().unwrap().call_once((10,));
}
fn main() {
test2(Some((box |: x| println!("Hi:{}", x)) as Box<FnOnce(int)+Send>));
}
关于方差的说明,请考虑:
enum Option2<T> {
Empty,
One(fn() -> T)
}
也就是说,它要么不存储任何内容,要么存储一个返回 T
的函数。 .投Option2<Box<X>>
是什么意思至 Option2<Box<Trait>>
?不能强行假装一个函数返回 Box<X>
返回 Box<Trait>
类型的值.
2) If so, is there an open ticket for implicit conversions between the |:| sugar and FnOnce types?
|:|
糖是FnOnce
- 实现类型。 FnOnce
是一个特征,强制转换就像任何其他特征对象一样发生。
关于rust - 你能强制 Some(box| :|) to Option<Box<FnOnce<. ..>>> 使用rust 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828582/