Reputation: 8960
In my mind, this problem should be trivial to solve, but after some searching I am still unable to do this. I have a tokio::sync::mpsc::Receiver
(let us call it receiver
). I would like to wait for receiver
to have messages in it, without however pulling any message out. Having looked at the docs, I cannot find anything other than receiver.recv()
to get the job done, which however pulls the first message out. I cannot seem to find anything along the lines of receiver.readable()
or receiver.peek()
or anything like that. Am I missing something? Is there a workaround to do what I need to do?
Upvotes: 2
Views: 1753
Reputation: 2511
Current implementation has a semaphore inside which really has what you need, but interface does not expose it. As a workaround, you can use some other implementation, not from tokio. Here is an example which is sync+async and has is_empty function which works like you need.
Upvotes: 1
Reputation: 503
I believe you are right. You may consider using a second channel.
If you are not interested in the value (like with readable
) you can send to both channels at the same time, one passing the actual data, and one passing simply the unit (()
) just to signal there is something happening.
If you do want to see the value (like with peek
), another approach may be sending the data to the first channel as usual, and sending the data into the second channel directly after receiving it in the first.
Upvotes: 1