From e648b6e1a20eadec696e751aedc9c6cc621c7135 Mon Sep 17 00:00:00 2001 From: Vitaly _Vi Shukela Date: Sat, 29 Oct 2022 17:20:08 +0200 Subject: [PATCH] sync: Expose Semaphore::MAX_PERMITS and document some panics Resolves #5129 --- tokio/src/sync/semaphore.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs index ccf44ba8a88..14fc50597c2 100644 --- a/tokio/src/sync/semaphore.rs +++ b/tokio/src/sync/semaphore.rs @@ -123,7 +123,14 @@ fn bounds() { } impl Semaphore { + /// The maximum number of permits which a semaphore can hold. + /// + /// Exceeding this limit typically results in a panic. + pub const MAX_PERMITS: usize = super::batch_semaphore::Semaphore::MAX_PERMITS; + /// Creates a new semaphore with the initial number of permits. + /// + /// Panics if `permits` exceeds [`Semaphore::MAX_PERMITS`]. #[track_caller] pub fn new(permits: usize) -> Self { #[cfg(all(tokio_unstable, feature = "tracing"))] @@ -186,7 +193,7 @@ impl Semaphore { /// Adds `n` new permits to the semaphore. /// - /// The maximum number of permits is `usize::MAX >> 3`, and this function will panic if the limit is exceeded. + /// The maximum number of permits is [`Semaphore::MAX_PERMITS`], and this function will panic if the limit is exceeded. pub fn add_permits(&self, n: usize) { self.ll_sem.release(n); }