我对Rust编程还很陌生,仍在努力理解内存和所有权的概念。
我有这个代码。
#[tokio::main]
async fn main() {
let file_root = String::from("test_dir/");
let content_dir = String::from("posts_");
let obj = msgnode::MsgNode::new(file_root.clone(), content_dir.clone());
let node_filter = warp::any().map(move || obj.clone());
// GET /hello/warp => 200 OK with body "Hello, warp!"
let hello = warp::get()
.and(warp::path("waservice"))
.and(warp::path::end())
.and(msg_body())
.and(node_filter.clone())
.and_then(store_wamsg);
warp::serve(hello)
.run(([127, 0, 0, 1], 3030))
.await;
}
mod msgnode;
这一切都很好,只是我不能处理MsgNode
对象的克隆实例。在store_wamsg中,我使用MsgNode对象如下:
async fn store_wamsg(item: MsgItem, node : &msgnode::MsgNode) -> Result<impl warp::Reply, warp::Rejection> {
let mut node2 = node.clone();
node2.create_file(item.msg_content);
Ok(warp::reply::with_status(
"All good",
http::StatusCode::OK,
))
}
我的问题是,如果有一种方法,我不需要在主函数中使用MsgNode对象的多个克隆实例,每次向服务发出新请求时?
更确切地说,我想做这样的事情:
let node_filter = warp::any().map(move || &obj);
并以某种方式在store_wamsg函数内部传递引用。现在,当我这样做的时候,我遇到了以下错误:
116 | let node_filter = warp::any().map(move || &obj);
| ------- ^^^^ returning this value requires that `'1` must outlive `'2`
| | |
| | return type of closure is &'2 MsgNode
| lifetime `'1` represents this closure's body
|
= note: closure implements `Fn`, so references to captured variables can't escape the closure
您可以在Arc
Mutex
中封装消息节点以同步访问:
use std::sync::{Arc, Mutex};
let obj = Arc::new(Mutex::new(msgnode::MsgNode::new(
file_root.clone(),
content_dir.clone(),
)));
let node_filter = warp::any().map(move || obj.clone());
这里,obj.clone()
克隆原子资源计数器,而不是msgnode。因此,MsgNode只有一个实例,线程在Arc和Mutex的帮助下协调对它的访问。