#Trait bounds error and question how to fix it

4 messages · Page 1 of 1 (latest)

silver crown
#

Hello, new rust user here;;
Just wanted to ask if anyone can help me with them traits usage:
I have a splitstream and splitsink (got them from tokio WS library split)
The underlying type for sink is for example:

SplitStream<WebSocketStream<TlsStream<TcpStream>>>

I want to "store" this (move it) to a struct member

pub struct Whatever {
...
sink : SplitStream<WebSocketStream<TlsStream<TcpStream>>>,
...
}

If i move the splitsink to a structure member, and then try to use it inside a method i define:

impl Whatever {
      async fn consume_msg(&self) {
      let f = self.sink.next().await;
      ^^^^ method cannot be called due to unsatisfied trait bounds

I hit the usatisfied trait bounds err;

47 | pub struct SplitSink<S, Item> {
   | -----------------------------
   | |                          
   | doesn't satisfy `_: StreamExt`
   | doesn't satisfy `_: Stream`                                   
   |                                                              
   = note: the following trait bounds were not satisfied:                          
`SplitSink<WebSocketStream<tokio_native_tls::TlsStream<tokio::net::TcpStream>>, tokio_tungstenite::tungstenite::Mess
age>: futures_util::Stream`
           which is required by `SplitSink<WebSocketStream<tokio_native_tls::TlsStream<tokio::net::TcpStream>>, tokio_tungstenite::tungstenite::Message>:StreamExt`

However, the following snippet works fine...
(i want to implement a method of struct Whatever that would resolve-in the next element in WebSocketStream)

let (outgoing, mut incoming) = ws_stream.split();
let msg =  incoming.next().await;

I'm not really sure it's the place to ask, but can anyone please hlep me understand the trait bounding concept? I mean, why do i even bother with trait bounds, if i'm encapsulating the underlying instance of SplitSink in my own structure, why my structure or method has to implement the StreamExt traits?

calm rune
#

I haven't any personal experience with these libraries but looking at the doc for the split function: https://docs.rs/futures/latest/futures/prelude/stream/trait.StreamExt.html#method.split

I can see that the tuple returned has the first element being the SplitSink and the second element being SplitStream

In your second example that works you're calling the next on outgoing which is SplitStream that does implement Stream (and also gets the methods defined on StreamExt due to it being an extension trait)

The first snippet is complaining because you're trying to call a stream method on something that isn't actually a stream (it's a sink)

The type in the first example, and the type in the second example are different and have different capabilities

silver crown
silver crown