Implement Sink and Stream for WsClient#907
Merged
jxs merged 5 commits intoseanmonstar:masterfrom Oct 30, 2021
Merged
Conversation
Contributor
Author
|
I'm going to rebase this shortly since merging #906 broke this pull request. This PR now also needs to add |
7a86eff to
e2a07bf
Compare
jxs
approved these changes
Oct 30, 2021
Collaborator
|
Hi, and sorry for the delay! Makes sense, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.