我是新手,并尝试理解以下错误:
use rybw_config::ListenerConfig;
use tokio::prelude::*;
use tokio::task::JoinHandle;
pub mod tcp;
use tcp::TCPListener;
pub struct Listener {
config: ListenerConfig,
tcp: Vec<tcp::TCPListener>,
}
impl Listener {
pub fn new(config: ListenerConfig) -> Listener {
let tcp: Vec<TCPListener> = config
.tcp
.clone()
.unwrap()
.iter()
.map(|x| TCPListener::new((*x).clone()))
.collect();
Listener {
tcp: tcp,
config: config.clone(),
}
}
pub async fn run(&self) {
let handles: Vec<JoinHandle<()>> = self.tcp.iter().map(|i| {
tokio::spawn(async {
i.run().await
})
}).collect();
futures::future::join_all(handles);
}
尝试通过 tokio 运行多 tcp 侦听器,但得到如下错误:
error: cannot infer an appropriate lifetime
--> rybw-listener/src/lib.rs:28:22
|
28 | pub async fn run(&self) {
| ^^^^^
| |
| data with this lifetime...
| ...is captured here...
29 | let handles: Vec<JoinHandle<()>> = self.tcp.iter().map(|i| {
30 | tokio::spawn(async {
| ------------ ...and required to be `'static` by this