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
SinkandStreamforWsClient.Why is this useful?
If you use
tokio-tungsteniteas your websocket client, you will usually have atokio-tungstenite::WebSocketStreamto work with, which exclusively uses theSinkandStreamtraits for sending and receiving messages. ImplementingSinkandStreamforWsClientas well allows for writing code that works with both the realtokio-tungstenite::WebSocketStreamandWsClientby relying only on theSinkandStreamtraits in the implementation, especially if you combine it with #903 which provides conversions betweenwarp::ws::Messageandtungstenite::protocol::Message.Why not use
tokio::sync::mpsc::unbounded_channelInitially I tried to implement this using the tokio mpsc channels that
WsClientwas using, but found it to be impossible.Implementing
Streamisn't an issue because of theStreamimplementation provided intokio-streamand becausetokio::sync::mpsc::UnboundedReceiverhas apoll_recvmethod exactly for that purpose.Implementing
Sinkhowever doesn't work because there is no way thatpoll_flushorpoll_closecan notify a task to be polled again.Therefore this PR switches the channel implementation from
tokiotofutures, sincefutues::channel::mpsc::UnboundedSenderandUnboundedReceiveralready implementSinkandStreamand the implementation onWsClientthen only need to proxy their calls to the underlying channel.