pub struct RankedSemaphorePermit<'a> { /* private fields */ }Expand description
A permit that grants access to a resource protected by a semaphore.
This permit holds a reference to the semaphore and represents one or more permits that have been acquired. When the permit is dropped, the permits are automatically returned to the semaphore.
§Examples
use ranked_semaphore::RankedSemaphore;
let sem = RankedSemaphore::new_fifo(3);
let permit = sem.acquire().await.unwrap();
assert_eq!(permit.num_permits(), 1);
// Permit is automatically returned when droppedImplementations§
Source§impl<'a> RankedSemaphorePermit<'a>
impl<'a> RankedSemaphorePermit<'a>
Sourcepub fn forget(self)
pub fn forget(self)
Forgets this permit without releasing it back to the semaphore.
This effectively removes the permits from circulation permanently. Use this method when you want to prevent the permits from being returned to the semaphore when the permit is dropped.
§Examples
use ranked_semaphore::RankedSemaphore;
let sem = RankedSemaphore::new_fifo(3);
let permit = sem.acquire().await.unwrap();
assert_eq!(sem.available_permits(), 2);
permit.forget(); // Permit is not returned to semaphore
assert_eq!(sem.available_permits(), 2);Sourcepub fn num_permits(&self) -> usize
pub fn num_permits(&self) -> usize
Returns the number of permits held by this permit object.
§Examples
use ranked_semaphore::RankedSemaphore;
let sem = RankedSemaphore::new_fifo(5);
let permit = sem.acquire_many(3).await.unwrap();
assert_eq!(permit.num_permits(), 3);Sourcepub fn merge(&mut self, other: Self)
pub fn merge(&mut self, other: Self)
Merges another permit into this one, combining their permit counts.
Both permits must belong to the same semaphore instance. After merging, the other permit becomes invalid (holds 0 permits) and this permit holds the combined count.
§Arguments
other- Another permit from the same semaphore to merge
§Panics
Panics if the permits belong to different semaphores.
§Examples
use ranked_semaphore::RankedSemaphore;
let sem = RankedSemaphore::new_fifo(5);
let mut permit1 = sem.acquire_many(2).await.unwrap();
let permit2 = sem.acquire_many(1).await.unwrap();
permit1.merge(permit2);
assert_eq!(permit1.num_permits(), 3);Sourcepub fn split(&mut self, n: u32) -> Option<Self>
pub fn split(&mut self, n: u32) -> Option<Self>
Splits off a specified number of permits into a new permit.
This reduces the current permit’s count by n and returns a new
permit holding n permits. If there are insufficient permits,
returns None.
§Arguments
n- The number of permits to split off
§Returns
Some(RankedSemaphorePermit)- New permit holdingnpermitsNone- If this permit holds fewer thannpermits
§Examples
use ranked_semaphore::RankedSemaphore;
let sem = RankedSemaphore::new_fifo(5);
let mut permit = sem.acquire_many(3).await.unwrap();
let split_permit = permit.split(2).unwrap();
assert_eq!(permit.num_permits(), 1);
assert_eq!(split_permit.num_permits(), 2);