我學習Rust並嘗試構建一個構建在hyper之上的微型路由系統(它僅用於學習目的,我知道框架存在)。`對於<'r, 'r, 'r> ......`不能在線程之間安全地發送
我不知道如何與hyper::server::Handler
共享「複雜」類型。我閱讀錯誤信息,但不幸的是,我不明白如何解決它(大多數時候,鐵鏽編譯器只是說要修復什麼,現在我不確定要理解)。
這裏是一個(非)工作過於簡單化的是我的嘗試例如:
extern crate hyper;
use std::sync::Mutex;
use hyper::*;
type Route = (method::Method, String, Box<Fn(server::Request, server::Response)>);
struct MyHandler {
routes: Mutex<Vec<Route>>
}
impl server::Handler for MyHandler {
fn handle(&self, req: server::Request, mut res: server::Response) {
// This is not important
}
}
fn main() {
// This is not important
}
和錯誤是:
error: the trait bound `for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static: std::marker::Send` is not satisfied [--explain E0277]
--> src/main.rs:12:10
|>
12 |> impl server::Handler for MyHandler {
|> ^^^^^^^^^^^^^^^
note: `for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static` cannot be sent between threads safely
note: required because it appears within the type `Box<for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static>`
note: required because it appears within the type `(hyper::method::Method, std::string::String, Box<for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static>)`
note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<(hyper::method::Method, std::string::String, Box<for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static>)>`
note: required because it appears within the type `alloc::raw_vec::RawVec<(hyper::method::Method, std::string::String, Box<for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static>)>`
note: required because it appears within the type `std::vec::Vec<(hyper::method::Method, std::string::String, Box<for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static>)>`
note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex<std::vec::Vec<(hyper::method::Method, std::string::String, Box<for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static>)>>`
note: required because it appears within the type `MyHandler`
note: required by `hyper::server::Handler`
它的工作原理,如果我用一個簡單的整數,但不適用於Route
類型。
所以,這個特質有一個問題,而且「不能在線程之間安全地發送」。閱讀hyper
doc,我增加了一個Mutex
,但我一定是啞巴,我不知道我在做什麼,不知道我是否應該停下來學習Rust,或者繼續嘗試。
超清晰。謝謝! –