Skip to content
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

channel: add max_capacity method #4904

Merged
merged 10 commits into from Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests-build/tests/fail/macros_dead_code.stderr
@@ -1,11 +1,11 @@
error: function is never used: `f`
--> $DIR/macros_dead_code.rs:6:10
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
error: function `f` is never used
--> tests/fail/macros_dead_code.rs:6:10
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
|
6 | async fn f() {}
| ^
|
note: the lint level is defined here
--> $DIR/macros_dead_code.rs:1:9
--> tests/fail/macros_dead_code.rs:1:9
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
|
1 | #![deny(dead_code)]
| ^^^^^^^^^
30 changes: 30 additions & 0 deletions tokio/src/sync/mpsc/bounded.rs
Expand Up @@ -1036,6 +1036,36 @@ impl<T> Sender<T> {
chan: self.chan.downgrade(),
}
}

/// Returns the max buffer capacity of the channel.
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
///
/// The max capacity is the buffer capacity you initially specified with [`channel`]
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Examples
///
/// ```
/// use tokio::sync::mpsc;
///
/// #[tokio::main]
/// async fn main() {
/// let (tx, rx) = mpsc::channel::<()>(5);
///
/// // both max capacity and capacity are the same at first
/// assert_eq!(tx.max_capacity(), 5);
/// assert_eq!(tx.capacity(), 5);
///
/// // Making a reservation doesn't change the max capacity.
/// let permit = tx.reserve().await.unwrap();
/// assert_eq!(tx.max_capacity(), 5);
/// // but drops the capacity by one
/// assert_eq!(tx.capacity(), 4);
hawkw marked this conversation as resolved.
Show resolved Hide resolved
/// }
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
/// ```
///
/// [`channel`]: channel
pub fn max_capacity(&self) -> usize {
self.chan.semaphore().1
}
Daksh14 marked this conversation as resolved.
Show resolved Hide resolved
}

impl<T> Clone for Sender<T> {
Expand Down