调用tokio::spawn时如何解决"the trait bound `[closure]: tokio::prelude::Future` is not satisfied"?
How do I solve "the trait bound `[closure]: tokio::prelude::Future` is not satisfied" when calling tokio::spawn?
extern crate tokio; // 0.1.22
use tokio::io;
use tokio::net::TcpListener;
use tokio::prelude::*;
use bytes::Bytes; // 0.4.12
fn main() {
let addr = "0.0.0.0:1502".parse().unwrap();
let mut listener = TcpListener::bind(&addr).unwrap();
let done = listener
.incoming()
.map_err(|e| println!("failed to accept socket; error = {:?}", e))
.for_each(move |socket| {
let process = move || {};
tokio::spawn(process)
});
tokio::run(done);
Ok(());
tokio::run(done);
}
error[E0277]: the trait bound `[closure@src/main.rs:17:27: 17:37]: tokio::prelude::Future` is not satisfied
--> src/main.rs:19:13
|
19 | tokio::spawn(process)
| ^^^^^^^^^^^^ the trait `tokio::prelude::Future` is not implemented for `[closure@src/main.rs:17:27: 17:37]`
|
= note: required by `tokio::spawn`
请检查definition of tokio::spawn
:
pub fn spawn<F>(f: F) -> Spawn
where
F: Future<Item = (), Error = ()> + 'static + Send
它期望 Future
的实现作为它的参数。您传递给 tokio::spawn
的闭包不是 Future
的实现。 Futures-rs 没有闭包,而是 lazy
Creates a new future which will eventually be the same as the one created by the closure provided.
简单地说,lazy
未来持有一个闭包来执行。这发生在执行者第一次执行 poll
时(在这种情况下,您使用的是 Tokio 执行者)。
如果你用 lazy
包装你的闭包,你的代码将按预期工作。
extern crate tokio; // 0.1.22
use tokio::net::TcpListener;
use tokio::prelude::*;
fn main() {
let addr = "0.0.0.0:1502".parse().unwrap();
let listener = TcpListener::bind(&addr).unwrap();
let done = listener
.incoming()
.map_err(|e| println!("failed to accept socket; error = {:?}", e))
.for_each(move |_socket| {
let process = futures::future::lazy(move || {
println!("My closure executed at future");
Ok(())
});
tokio::spawn(process)
});
tokio::run(done);
}
extern crate tokio; // 0.1.22
use tokio::io;
use tokio::net::TcpListener;
use tokio::prelude::*;
use bytes::Bytes; // 0.4.12
fn main() {
let addr = "0.0.0.0:1502".parse().unwrap();
let mut listener = TcpListener::bind(&addr).unwrap();
let done = listener
.incoming()
.map_err(|e| println!("failed to accept socket; error = {:?}", e))
.for_each(move |socket| {
let process = move || {};
tokio::spawn(process)
});
tokio::run(done);
Ok(());
tokio::run(done);
}
error[E0277]: the trait bound `[closure@src/main.rs:17:27: 17:37]: tokio::prelude::Future` is not satisfied
--> src/main.rs:19:13
|
19 | tokio::spawn(process)
| ^^^^^^^^^^^^ the trait `tokio::prelude::Future` is not implemented for `[closure@src/main.rs:17:27: 17:37]`
|
= note: required by `tokio::spawn`
请检查definition of tokio::spawn
:
pub fn spawn<F>(f: F) -> Spawn
where
F: Future<Item = (), Error = ()> + 'static + Send
它期望 Future
的实现作为它的参数。您传递给 tokio::spawn
的闭包不是 Future
的实现。 Futures-rs 没有闭包,而是 lazy
Creates a new future which will eventually be the same as the one created by the closure provided.
简单地说,lazy
未来持有一个闭包来执行。这发生在执行者第一次执行 poll
时(在这种情况下,您使用的是 Tokio 执行者)。
如果你用 lazy
包装你的闭包,你的代码将按预期工作。
extern crate tokio; // 0.1.22
use tokio::net::TcpListener;
use tokio::prelude::*;
fn main() {
let addr = "0.0.0.0:1502".parse().unwrap();
let listener = TcpListener::bind(&addr).unwrap();
let done = listener
.incoming()
.map_err(|e| println!("failed to accept socket; error = {:?}", e))
.for_each(move |_socket| {
let process = futures::future::lazy(move || {
println!("My closure executed at future");
Ok(())
});
tokio::spawn(process)
});
tokio::run(done);
}