Implement Sink and Stream for WsClient #907
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This implements
Sink
andStream
forWsClient
.Why is this useful?
If you use
tokio-tungstenite
as your websocket client, you will usually have atokio-tungstenite::WebSocketStream
to work with, which exclusively uses theSink
andStream
traits for sending and receiving messages. ImplementingSink
andStream
forWsClient
as well allows for writing code that works with both the realtokio-tungstenite::WebSocketStream
andWsClient
by relying only on theSink
andStream
traits in the implementation, especially if you combine it with #903 which provides conversions betweenwarp::ws::Message
andtungstenite::protocol::Message
.Why not use
tokio::sync::mpsc::unbounded_channel
Initially I tried to implement this using the tokio mpsc channels that
WsClient
was using, but found it to be impossible.Implementing
Stream
isn't an issue because of theStream
implementation provided intokio-stream
and becausetokio::sync::mpsc::UnboundedReceiver
has apoll_recv
method exactly for that purpose.Implementing
Sink
however doesn't work because there is no way thatpoll_flush
orpoll_close
can notify a task to be polled again.Therefore this PR switches the channel implementation from
tokio
tofutures
, sincefutues::channel::mpsc::UnboundedSender
andUnboundedReceiver
already implementSink
andStream
and the implementation onWsClient
then only need to proxy their calls to the underlying channel.