Search code examples
websocketrustrust-tokio

Where is the split() method of tokio_tungstenite::WebSocketStream implemented?


Where does tokio_tungstenite::WebSocketStream implement the split() function as used in one of their examples?

The following is a snippet of the example:

let (ws_stream, _) = connect_async(url).await.expect("Failed to connect");

let (write, read) = ws_stream.split(); 

Someone else had a similar problem finding split() in tokio::net::TcpStream and the answer was that TcpStream implements AsyncRead. In the case of WebSocketStream I cannot see any impl AsyncRead for WebSocketStream code.

My broader question is what knowledge am I missing that stops me from finding methods such as split() and their implementations of a struct such as WebSocketStream in docs.rs or in the source code?


Solution

  • The concept I was missing was that of blanket implementations where traits in modules that know nothing about the code in my module can add functionality to my structs. This is so long as the trait bounds of the external code fit my code.

    In this situation WebSocketStream implements Stream. There is a trait StreamExt in the futures crate that knows nothing about WebSocketStream but is a blanket implementation for Stream. It provides the functionality of split() to Stream and as WebSocketStream implements Stream it also gets that functionality.