Why does the compiler need a hint of this trait?

I had this code:

pub trait MiddlewareHandler: Clone + Send {
    //...probably unimportant for the question
}

#[deriving(Clone)]
pub struct Middleware {
    handlers: Vec<Box<MiddlewareHandler>>
}

#[deriving(Clone)]
pub struct Server{
    middleware: Middleware
}

This left me with a compiler yelling at me:

src/server.rs:20:31: 20:37 error: the type `server::Server', which does not fulfill `Send`, cannot implement this trait
src/server.rs:20 impl http::server::Server for Server {
                                           ^~~~~~
src/server.rs:20:31: 20:37 note: types implementing this trait must fulfill `Send+Sized`
src/server.rs:20 impl http::server::Server for Server {

It took me a long time to figure out what I had to change Vec<Box<MiddlewareHandler>>to Vec<Box<MiddlewareHandler + Send>>, so that the final code would look like this:

pub trait MiddlewareHandler: Clone + Send {
    //...probably unimportant for the question
}

#[deriving(Clone)]
pub struct Middleware {
    handlers: Vec<Box<MiddlewareHandler + Send>>
}

#[deriving(Clone)]
pub struct Server{
    middleware: Middleware
}

Now the code is compiling, but I absolutely do not understand what the problem is. Why +Sendin the definition Vec? I mean, the trait MiddlewareHandleralready implements Send + Clone. For me it looks unnecessary.

Can anyone share their wisdom with me, why did I have to change the code?

+4
source share
1 answer

Looks like an error, I filed # 15155 .


"" - Send http::server::Server.

pub trait Server: Send + Clone {

, Clone ( , Clone #[deriving(Clone)]) Send. Send , Send ( : ), ,

pub struct Middleware {
    handlers: Vec<Box<Trait>>
}

Send : , , Box<Trait> Send , . Rc .

, , Send, -: Box<Trait + Send>...

MiddlewareHandler Send supertrait ( , - Send), , Box<MiddlewareHandler> Send (, ).

+1

Source: https://habr.com/ru/post/1545891/


All Articles