Struct readlock::Shared

source ·
pub struct Shared<T: ?Sized>(/* private fields */);
Expand description

A wrapper around a resource possibly shared with SharedReadLocks and WeakReadLocks, but no other Shareds.

Implementations§

source§

impl<T> Shared<T>

source

pub fn new(data: T) -> Self

Create a new Shared.

source

pub fn unwrap(this: Self) -> Result<T, Self>

Returns the inner value, if the Shared has no associated SharedReadLocks.

Otherwise, an Err is returned with the same Shared that was passed in.

This will succeed even if there are outstanding weak references.

§Panics

This function will panic if the lock around the inner value is poisoned.

source§

impl<T: ?Sized> Shared<T>

source

pub fn get(this: &Self) -> &T

Get a reference to the inner value.

Usually, you don’t need to call this function since Shared<T> implements Deref. Use this if you want to pass the inner value to a generic function where the compiler can’t infer that you want to have the Shared dereferenced otherwise.

§Panics

This function will panic if the lock around the inner value is poisoned.

source

pub fn try_get(this: &Self) -> LockResult<&T>

Try to get a reference to the inner value, returning an error if the lock around it is poisoned.

source

pub fn lock(this: &mut Self) -> SharedWriteGuard<'_, T>

Lock this Shared to be able to mutate it, blocking the current thread until the operation succeeds.

source

pub fn get_read_lock(this: &Self) -> SharedReadLock<T>

Get a SharedReadLock for accessing the same resource read-only from elsewhere.

source

pub fn try_from_inner(rwlock: Arc<RwLock<T>>) -> Result<Self, Arc<RwLock<T>>>

Attempt to create a Shared from its internal representation, Arc<RwLock<T>>.

This returns Ok(_) only if there are no further references (including weak references) to the inner RwLock since otherwise, Shareds invariant of being the only instance that can mutate the inner value would be broken.

source

pub fn into_inner(this: Self) -> Arc<RwLock<T>>

Turns this Shared into its internal representation, Arc<RwLock<T>>.

source

pub fn read_count(this: &Self) -> usize

Gets the number of associated SharedReadLocks.

source

pub fn weak_count(this: &Self) -> usize

Gets the number of associated WeakReadLocks.

Trait Implementations§

source§

impl<T: Debug + ?Sized> Debug for Shared<T>

source§

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

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

impl<T: Default> Default for Shared<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: ?Sized> Deref for Shared<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> Freeze for Shared<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Shared<T>
where T: ?Sized,

§

impl<T> Send for Shared<T>
where T: Send + Sync + ?Sized,

§

impl<T> Sync for Shared<T>
where T: Send + Sync + ?Sized,

§

impl<T> Unpin for Shared<T>
where T: ?Sized,

§

impl<T> UnwindSafe for Shared<T>
where T: ?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>,

§

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>,

§

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.