我一直在用Rust构建一个websocket服务器库大约4-6周。今天,我使用rustc
脚本抓取了最新的rustup.sh
,并且构建突破了新的生命周期要求。我不确定在哪里查找这些更改以找出我需要指定的内容以满足要求? This Week in Rust没有最近更改的列表,Lifetimes Guide仅列出了各种示例。
各种结构定义
pub struct Server<'a> {
pub sockets: Vec<Socket<'a>>,
pub events: Vec<Event<'a>>,
pub to_event_loop: Sender<Action<'a>>,
pub socket_id: String
}
impl<'a> Clone for Server<'a> {
fn clone(&self) -> Server<'a> {
Server {
sockets: self.sockets.clone(),
events: self.events.clone(),
to_event_loop: self.to_event_loop.clone(),
socket_id: self.socket_id.clone()
}
}
}
pub struct Action<'a> {
pub event: String,
pub socket: Socket<'a>,
pub message: Message
}
pub struct Event<'a> {
pub name: String,
pub execute: fn(data: json::Json, server: super::Server)
}
pub struct Socket<'a> {
pub id: String,
pub stream: TcpStream
}
功能产生错误
pub fn start(server: Server, ip: &str, port: u16) {
/*
* Communication channel
* - From HTTP Server to Event Loop (Action Passed)
*/
let (to_event_loop, from_conn_pool): (Sender<Action>, Receiver<Action>) = channel();
// Start up event loop
let server_clone = server.clone();
let to_event_loop_clone = to_event_loop.clone();
spawn(proc() {
event_loop(server_clone, from_conn_pool, to_event_loop_clone)
});
// Start TCP server
let listener = TcpListener::bind(ip, port);
let mut acceptor = listener.listen();
for stream in acceptor.incoming() {
match stream {
Ok(stream) => {
let event_loop_msgr = to_event_loop.clone();
spawn(proc() {
process_new_connection(stream, event_loop_msgr)
})
}
Err(e) => {
println!("Error accepting connection: {}", e)
}
}
}
drop(acceptor);
}
编译器输出
src/rustic_io.rs:47:24: 47:30 error: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
src/rustic_io.rs:47 let server_clone = server.clone();
^~~~~~
src/rustic_io.rs:50:9: 50:70 note: first, the lifetime cannot outlive the call at 50:8...
src/rustic_io.rs:50 event_loop(server_clone, from_conn_pool, to_event_loop_clone)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/rustic_io.rs:50:20: 50:32 note: ...so that argument is valid for the call
src/rustic_io.rs:50 event_loop(server_clone, from_conn_pool, to_event_loop_clone)
^~~~~~~~~~~~
src/rustic_io.rs:38:51: 70:2 note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the block at 38:50...
src/rustic_io.rs:38 pub fn start(server: Server, ip: &str, port: u16) {
src/rustic_io.rs:39
src/rustic_io.rs:40 /*
src/rustic_io.rs:41 * Communication channel
src/rustic_io.rs:42 * - From HTTP Server to Event Loop (Action Passed)
src/rustic_io.rs:43 */
...
src/rustic_io.rs:47:24: 47:30 note: ...so that types are compatible (expected `&server::Server<'_>`, found `&server::Server<'_>`)
src/rustic_io.rs:47 let server_clone = server.clone();
^~~~~~
编译器对问题是非常具体的,我只是不知道它期望什么语法,以便我能满足要求?