【发布时间】:2016-06-12 21:36:40
【问题描述】:
这是我要调用的函数:
#[inline]
pub fn spawn<F>(f: F) -> Handle
where F: FnOnce(&mut Coroutine) + Send + 'static
{
Self::spawn_opts_impl(Box::new(f), Options::default())
}
然后我创建了一个枚举,因为我实际上想将它从一个线程发送到另一个线程,这也是我将函数装箱的原因。我也匹配了特征约束。
enum Message {
Task(Box<FnOnce(&mut Coroutine) + Send + 'static>),
}
但如果我尝试从Message 中提取函数:
fn main(){
let m = Message::Task(Box::new(|me| {
}));
let c = match m{
Message::Task(f) => Coroutine::spawn(f)
};
}
我收到以下错误:
src/main.rs:168:29: 168:45 error: the trait bound `for<'r> Box<for<'r> std::ops::FnOnce(&'r mut coroutine::asymmetric::Coroutine) + Send>: std::ops::FnOnce<(&'r mut coroutine::asymmetric::Coroutine,)>` is not satisfied [E0277]
src/main.rs:168 Message::Task(f) => Coroutine::spawn(f)
^~~~~~~~~~~~~~~~
src/main.rs:168:29: 168:45 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:168:29: 168:45 help: the following implementations were found:
src/main.rs:168:29: 168:45 help: <Box<std::boxed::FnBox<A, Output=R> + 'a> as std::ops::FnOnce<A>>
src/main.rs:168:29: 168:45 help: <Box<std::boxed::FnBox<A, Output=R> + Send + 'a> as std::ops::FnOnce<A>>
src/main.rs:168:29: 168:45 note: required by `coroutine::asymmetric::Coroutine::spawn`
我不知道 Rust 在这里试图告诉我什么。我认为问题在于spawn 需要一个非装箱函数,但如果我尝试取消装箱函数,我会得到同样的错误。
请注意,在提出这个问题时,coroutine-rs 没有构建,我修复了 this fork 中的错误。
【问题讨论】:
标签: rust