我正在尝试实现JavaScript样式的回调。我有一个使用库的应用程序(都是我的),我需要应用程序能够将闭包或函数传递给库中的方法,然后在满足条件时生成一个线程并在线程内部它将打电话给回电。
main.rs
fn main(){
welcome_message();
mylib::connect(|| println!("Connected to service! Please enter a command. (hint: help)\n\n"));
loop {
match prompt_input() {
true => {},
false => break,
}
}
}
lib.rs
pub fn connect<F>(resolve: F) -> (mpsc::Sender<Message>, mpsc::Receiver<Message>)
where F: Fn()
{
...
let receive_loop = Thread::scoped(move || {
for response in receiver.incoming_messages::<Message>(){
let json_string = match response.unwrap() {
Message::Text(txt) => txt,
_ => "".to_string(),
};
let message = json::Json::from_str(json_string.as_slice());
let message_json = message.unwrap();
if message_json.is_object() {
let ref something = receiver_tx;
let obj = message_json.as_object().unwrap();
let something_json = obj.get("lsri").unwrap();
let something = something_json.to_string().replace("\"", "");
match something.as_slice() {
"service#connected" => resolve(),
_ => println!("{}", type),
}
} else {
println!("Invalid service response");
}
}
});
...
}
错误
src/lib.rs:54:24: 54:38 error: the trait `core::marker::Send` is not implemented for the type `F` [E0277]
src/lib.rs:54 let receive_loop = Thread::scoped(move || {
^~~~~~~~~~~~~~
src/lib.rs:54:24: 54:38 note: `F` cannot be sent between threads safely
src/lib.rs:54 let receive_loop = Thread::scoped(move || {
^~~~~~~~~~~~~~
它不需要是我传递的闭包,我也可以传递一个函数。它不需要任何参数或返回类型,但如果有帮助,我可以添加一些虚拟参数。我非常愿意接受其他方法或方法的建议,以实现相同的目标。
我尝试过使用:
这可能与Rust有关吗?还有更好的方法吗?
感谢您提前抽出时间。
解决方案:
对于将来发现这一点的人,根据以下答案的说明,我将connect
的签名更改为以下内容,这允许将回调传递给线程。
pub fn connect<'a, T, F>(resolve: F) -> (mpsc::Sender<Message>, mpsc::Receiver<Message>)
where T: Send + 'a, F: FnOnce() -> T, F: Send + 'a
答案 0 :(得分:2)
尝试使用与Thread::scoped相同的限制标记F
:
fn scoped<'a, T, F>(f: F) -> JoinGuard<'a, T>
where T: Send + 'a, F: FnOnce() -> T, F: Send + 'a
具体而言,将类型与Send
特征绑定应清除
特征
实施core::marker::Send
未针对类型F