Struct Semaphore

Source
pub struct Semaphore<T: ?Sized> { /* private fields */ }
Expand description

Allows up to max references to the data in the Semaphore

This behaves like RwLock<T> with some key differences

  1. You can’t get a &mut T, only a &T
  2. You can have up to a maximum number of references at once

Implementations§

Source§

impl<T: ?Sized> Semaphore<T>

Source

pub fn at_max(&self, ordering: Ordering) -> bool

Returns true if the current cound is >= the maximum count

Source

pub fn count(&self, ordering: Ordering) -> usize

Get the current number of references to the data

Source

pub fn get(&self) -> SemaphoreGuard<'_, T>

This function can be inefficient, as it uses std::thread::sleep on std and core::hint::spin_loop on no_std.

§Panics

This function will panic if max == 0 because that will cause an infinite loop

Source

pub fn try_get(&self) -> Result<SemaphoreGuard<'_, T>, SemaphoreError>

Attempt to get the value in the semaphore.

This function will never block

§Errors

This function will return [SemaphoreError::AtMax] if the current count is >= the maximum count

Source

pub fn get_mut(&mut self) -> &mut T

Get a mutable reference to the data in the semaphore

Source§

impl<T> Semaphore<T>

Source

pub fn new(value: T, max: usize) -> Self

Create a new semaphore with 0 counted references

Source

pub fn into_inner(self) -> T

Move the value out of the semaphore

Trait Implementations§

Source§

impl<T: ?Sized + Send> Send for Semaphore<T>

Source§

impl<T: ?Sized + Send> Sync for Semaphore<T>

Auto Trait Implementations§

§

impl<T> !Freeze for Semaphore<T>

§

impl<T> RefUnwindSafe for Semaphore<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for Semaphore<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for Semaphore<T>
where T: UnwindSafe + ?Sized,

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.