[][src]Struct async_weighted_semaphore::SemaphoreGuard

#[must_use]pub struct SemaphoreGuard<'a> { /* fields omitted */ }

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

impl<'a> SemaphoreGuard<'a>[src]

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

pub fn forget(self) -> usize[src]

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(())
        }
    }
}

Trait Implementations

impl<'a> Debug for SemaphoreGuard<'a>[src]

impl<'a> Drop for SemaphoreGuard<'a>[src]

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.