RankedSemaphorePermit

Struct RankedSemaphorePermit 

Source
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 dropped

Implementations§

Source§

impl<'a> RankedSemaphorePermit<'a>

Source

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);
Source

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);
Source

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);
Source

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 holding n permits
  • None - If this permit holds fewer than n permits
§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);

Trait Implementations§

Source§

impl<'a> Debug for RankedSemaphorePermit<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Drop for RankedSemaphorePermit<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.