-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tokio: add mpsc::Receiver::max_capacity
and mpsc::Receiver::capacity
#6511
tokio: add mpsc::Receiver::max_capacity
and mpsc::Receiver::capacity
#6511
Conversation
We already have an |
This is supposed to be an addition to that: |
pub fn capacity(&self) -> usize { | ||
self.chan.semaphore().semaphore.available_permits() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, this is not actually equal to max_capacity - len
. Generally, sending a message goes through this procedure:
- Acquire a permit for the message.
- Write the message into the channel's storage.
- Make the message available to receivers.
With the len
method, the length only changes in step 3, but with this method, the capacity already changes in step 1. If the user is using Permit
to acquire permits before sending messages, then there may be a long amount of time between step 1 and step 2.
The documentation should clearly explain the difference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the example to explain the relation with Receiver::len
. Is that sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you.
Motivation
mpsc::Sender
hasmax_capacity
andcapacity
methods, howevermpsc::Receiver
does not. It may help to have these methods when trying to pre-allocate forReceiver::recv_many
.Solution
Adds
Receiver::max_capacity
andReceiver::capacity
.