2016-08-25 23 views
3

我學習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,或者繼續嘗試。

回答

5

你快到了。

錯誤是說MyHandler沒有實現Send特點,這意味着一個類型可以被安全地發送到其他線程:

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` 

錯誤仍指向正確的地方,但它是一個有點壓倒性。第一行是:

note: `for<'r, 'r, 'r> std::ops::Fn(hyper::server::Request<'r, 'r>, hyper::server::Response<'r>) + 'static` cannot be sent between threads safely 

這是說,一個Fn型沒有實現Send。這是一個在你的Route類型:

type Route = (method::Method, String, Box<Fn(server::Request, server::Response)>); 

一個Fn閉只Send如果所有它捕獲的變量也Send,但在這裏我們不知道通過任何罩是否是合適的。

的解決方案是簡單:約束Fn類型爲Send

type Route = (method::Method, String, Box<Fn(server::Request, server::Response)+Send>); 
+0

超清晰。謝謝! –