From 51343e109b94b335e1beffd5f23e305c6c731311 Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Thu, 11 Apr 2024 13:54:49 +0200 Subject: [PATCH] sync: use usize instead of u32 for `SemaphorePermit::split` Signed-off-by: Alice Ryhl --- tokio/src/sync/semaphore.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tokio/src/sync/semaphore.rs b/tokio/src/sync/semaphore.rs index a952729b563..4c26a9473fe 100644 --- a/tokio/src/sync/semaphore.rs +++ b/tokio/src/sync/semaphore.rs @@ -994,7 +994,12 @@ impl<'a> SemaphorePermit<'a> { /// Splits `n` permits from `self` and returns a new [`SemaphorePermit`] instance that holds `n` permits. /// /// If there are insufficient permits and it's not possible to reduce by `n`, returns `None`. - pub fn split(&mut self, n: u32) -> Option { + pub fn split(&mut self, n: usize) -> Option { + let n = match u32::try_from(n) { + Ok(n) => n, + Err(_) => return None, + }; + if n > self.permits { return None; } @@ -1008,8 +1013,8 @@ impl<'a> SemaphorePermit<'a> { } /// Returns the number of permits held by `self`. - pub fn num_permits(&self) -> u32 { - self.permits + pub fn num_permits(&self) -> usize { + self.permits as usize } } @@ -1047,7 +1052,12 @@ impl OwnedSemaphorePermit { /// # Note /// /// It will clone the owned `Arc` to construct the new instance. - pub fn split(&mut self, n: u32) -> Option { + pub fn split(&mut self, n: usize) -> Option { + let n = match u32::try_from(n) { + Ok(n) => n, + Err(_) => return None, + }; + if n > self.permits { return None; } @@ -1066,8 +1076,8 @@ impl OwnedSemaphorePermit { } /// Returns the number of permits held by `self`. - pub fn num_permits(&self) -> u32 { - self.permits + pub fn num_permits(&self) -> usize { + self.permits as usize } }