Struct SemaphoreGuard

Source
pub struct SemaphoreGuard<'a> { /* private fields */ }
Expand description

A guard returned by Semaphore::acquire that will call Semaphore::release when it is dropped (falls out of scope).

§Examples

use async_weighted_semaphore::{Semaphore, SemaphoreGuard};
let semaphore = Semaphore::new(1);
let guard: SemaphoreGuard = semaphore.acquire(1).await.unwrap();

Implementations§

Source§

impl<'a> SemaphoreGuard<'a>

Source

pub fn new(semaphore: &'a Semaphore, amount: usize) -> Self

Source

pub fn extend(&mut self, other: SemaphoreGuard<'a>)

Combine two SemaphoreGuards into one, with the sum of the originals’ permits.

§Examples
let semaphore = Semaphore::new(15);
let mut g1 = semaphore.acquire(10).await.unwrap();
let g2 = semaphore.acquire(5).await.unwrap();
g1.extend(g2);
Source

pub fn forget(self) -> usize

Drop the guard without calling Semaphore::release. This is useful when releases don’t correspond one-to-one with acquires or it’s difficult to send the guard to the releaser.

§Examples
use async_channel::{Sender, SendError};
// Limit size of a producer-consumer queue. Receivers may wait for any number of items
// to be available.
async fn send<T>(semaphore: &Semaphore,
                 sender: &Sender<T>,
                 message: T
        ) -> Result<(), SendError<T>>{
    match semaphore.acquire(1).await {
        // A semaphore can be poisoned to prevent deadlock when a channel closes.
        Err(PoisonError) => Err(SendError(message)),
        Ok(guard) => {
            sender.send(message).await?;
            guard.forget();
            Ok(())
        }
    }
}
Source

pub fn split( &mut self, permits: usize, ) -> Result<SemaphoreGuard<'a>, SemaphoreGuardSplitErr>

Split this SemaphoreGuard into two.

The new guard will have permits permits, and this guard’s permits will be reduced accordingly.

§Examples
let semaphore = Semaphore::new(15);
let mut g1 = semaphore.acquire(15).await.unwrap();
let g2 = g1.split(5).unwrap();

Trait Implementations§

Source§

impl<'a> Debug for SemaphoreGuard<'a>

Source§

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

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

impl<'a> Drop for SemaphoreGuard<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for SemaphoreGuard<'a>

§

impl<'a> RefUnwindSafe for SemaphoreGuard<'a>

§

impl<'a> Send for SemaphoreGuard<'a>

§

impl<'a> Sync for SemaphoreGuard<'a>

§

impl<'a> Unpin for SemaphoreGuard<'a>

§

impl<'a> UnwindSafe for SemaphoreGuard<'a>

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.