Rust Lifetimes with mpsc :: Sender <T <'a >> and threads

I am creating a multi-threaded application in which I create a receive channel and a structure for storing a send channel (which will be used later in implementation). However, the type that I send on the channel has a specification for life. This type of websocket::message:Message from the rust-weboscket library. Due to this specification, rust does not seem to be able to correctly determine the lifetimes when it passes through a stream.

Here is an example of a rust error: https://play.rust-lang.org/?gist=7e37547d1c811185654f10a6a461e1ef&version=stable&backtrace=1

Now I tried to use the crossbar to span the lifetime, and this seems to solve this problem, but practically just delegates the problem of specifying the lifetime to somewhere else.

In my code, I get an error:

  $ cargo check Compiling rump v0.1.0 (file:///home/alainh/UPenn/CIS198/Rump) transport.rs:200:42: 200:57 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495] transport.rs:200 self.sender.send(self.serializer.encode(message)); ^~~~~~~~~~~~~~~ transport.rs:199:5: 202:6 help: consider using an explicit lifetime parameter as shown: fn send<T: Encodable>(&'a mut self, message: &T) -> WampResult<()> transport.rs:199 fn send<T: Encodable>(&mut self, message: &T) -> WampResult<()> { transport.rs:200 self.sender.send(self.serializer.encode(message)); transport.rs:201 Ok(()) transport.rs:202 } error: aborting due to previous error Could not compile `rump`. 

The line in question is: https://github.com/aehernandez/Rump/blob/ad717c7ef11857e94d0e1c02539667c8034676c4/src/transport.rs#L199

At this point, I’m not sure how to accurately solve this problem throughout my life. I do not want to delegate it anywhere else. Is there a good solution for this?

+5
source share
1 answer

When you create a stream, it can potentially live forever; of course, surviving your Transport<'a> for any 'a lifetime other than 'static (error messages are very confusing). When you call thread::spawn with a closure, that closure should have a lifetime of 'static , which is only true if 'a == 'static .

Since you are not actually sending an object with a lifetime over the channel, use explicitly the 'static lifetime:

 impl Connector for Transport<'static> { ... } 

Dummy

Edit:

Manually annotate types for sender and receiver

  let (tx, rx): (mpsc::Sender<Message<'a>>, mpsc::Receiver<Message<'a>>) = mpsc::channel(); let tx_send: mpsc::Sender<Message<'a>> = tx.clone(); 

shows a much more reasonable error

 <anon>:27:22: 27:35 error: the type `[ closure@ <anon>:27:36: 29:10 tx_send:std::sync::mpsc::Sender<Message<'a>>]` does not fulfill the required lifetime [E0477] <anon>:27 let handle = thread::spawn(move || { ^~~~~~~~~~~~~ note: type must outlive the static lifetime error: aborting due to previous error playpen: application terminated with error code 101 

Dummy

+3
source

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


All Articles