pub struct SemaphoreGuardArc { /* private fields */ }Expand description
A guard returned by Semaphore::acquire_arc that will call Semaphore::release when it
is dropped (falls out of scope). Can be sent between threads.
Implementations§
Source§impl SemaphoreGuardArc
impl SemaphoreGuardArc
pub fn new(semaphore: Arc<Semaphore>, amount: usize) -> Self
Sourcepub fn extend(&mut self, other: SemaphoreGuardArc)
pub fn extend(&mut self, other: SemaphoreGuardArc)
Combine two SemaphoreGuardArcs 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);Sourcepub fn forget(self) -> usize
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: &Arc<Semaphore>,
sender: &Sender<T>,
message: T
) -> Result<(), SendError<T>>{
match semaphore.acquire_arc(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(())
}
}
}Sourcepub fn split(
&mut self,
permits: usize,
) -> Result<SemaphoreGuardArc, SemaphoreGuardSplitErr>
pub fn split( &mut self, permits: usize, ) -> Result<SemaphoreGuardArc, SemaphoreGuardSplitErr>
Split this SemaphoreGuardArc into two.
The new guard will have permits permits, and this guard’s permits will be reduced
accordingly.
§Examples
let semaphore = Arc::new(Semaphore::new(15));
let mut g1 = semaphore.acquire_arc(15).await.unwrap();
let g2 = g1.split(5).unwrap();Trait Implementations§
Source§impl Debug for SemaphoreGuardArc
impl Debug for SemaphoreGuardArc
Auto Trait Implementations§
impl Freeze for SemaphoreGuardArc
impl RefUnwindSafe for SemaphoreGuardArc
impl Send for SemaphoreGuardArc
impl Sync for SemaphoreGuardArc
impl Unpin for SemaphoreGuardArc
impl UnwindSafe for SemaphoreGuardArc
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more