pub trait Semaphore: Sized {
type Error;
// Required methods
async fn acquire(
&self,
permits: usize,
) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>;
fn try_acquire(&self, permits: usize) -> Option<SemaphoreReleaser<'_, Self>>;
async fn acquire_all(
&self,
min: usize,
) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>;
fn try_acquire_all(&self, min: usize) -> Option<SemaphoreReleaser<'_, Self>>;
fn release(&self, permits: usize);
fn set(&self, permits: usize);
}
Expand description
An asynchronous semaphore.
A semaphore tracks a number of permits, typically representing a pool of shared resources. Users can acquire permits to synchronize access to those resources. The semaphore does not contain the resources themselves, only the count of available permits.
Required Associated Types§
Required Methods§
Sourceasync fn acquire(
&self,
permits: usize,
) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>
async fn acquire( &self, permits: usize, ) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>
Asynchronously acquire one or more permits from the semaphore.
Sourcefn try_acquire(&self, permits: usize) -> Option<SemaphoreReleaser<'_, Self>>
fn try_acquire(&self, permits: usize) -> Option<SemaphoreReleaser<'_, Self>>
Try to immediately acquire one or more permits from the semaphore.
Sourceasync fn acquire_all(
&self,
min: usize,
) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>
async fn acquire_all( &self, min: usize, ) -> Result<SemaphoreReleaser<'_, Self>, Self::Error>
Asynchronously acquire all permits controlled by the semaphore.
This method will wait until at least min
permits are available, then acquire all available permits
from the semaphore. Note that other tasks may have already acquired some permits which could be released
back to the semaphore at any time. The number of permits actually acquired may be determined by calling
SemaphoreReleaser::permits
.
Sourcefn try_acquire_all(&self, min: usize) -> Option<SemaphoreReleaser<'_, Self>>
fn try_acquire_all(&self, min: usize) -> Option<SemaphoreReleaser<'_, Self>>
Try to immediately acquire all available permits from the semaphore, if at least min
permits are available.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.