left_right

Struct ReadGuard

Source
pub struct ReadGuard<'rh, T: ?Sized> { /* private fields */ }
Expand description

A guard wrapping a live reference into a left-right protected T.

As long as this guard lives, the T being read cannot change. If a writer attempts to call WriteHandle::publish, that call will block until this guard is dropped.

To scope the guard to a subset of the data in T, use map and try_map.

Implementations§

Source§

impl<'rh, T: ?Sized> ReadGuard<'rh, T>

Source

pub fn map<F, U: ?Sized>(orig: Self, f: F) -> ReadGuard<'rh, U>
where F: for<'a> FnOnce(&'a T) -> &'a U,

Makes a new ReadGuard for a component of the borrowed data.

This is an associated function that needs to be used as ReadGuard::map(...), since a method would interfere with methods of the same name on the contents of a Readguard used through Deref.

§Examples
use left_right::{ReadGuard, ReadHandle};

fn get_str(handle: &ReadHandle<Vec<(String, i32)>>, i: usize) -> Option<ReadGuard<'_, str>> {
    handle.enter().map(|guard| {
        ReadGuard::map(guard, |t| {
            &*t[i].0
        })
    })
}
Source

pub fn try_map<F, U: ?Sized>(orig: Self, f: F) -> Option<ReadGuard<'rh, U>>
where F: for<'a> FnOnce(&'a T) -> Option<&'a U>,

Makes a new ReadGuard for a component of the borrowed data that may not exist.

This method differs from map in that it drops the guard if the closure maps to None. This allows you to “lift” a ReadGuard<Option<T>> into an Option<ReadGuard<T>>.

This is an associated function that needs to be used as ReadGuard::try_map(...), since a method would interfere with methods of the same name on the contents of a Readguard used through Deref.

§Examples
use left_right::{ReadGuard, ReadHandle};

fn try_get_str(handle: &ReadHandle<Vec<(String, i32)>>, i: usize) -> Option<ReadGuard<'_, str>> {
    handle.enter().and_then(|guard| {
        ReadGuard::try_map(guard, |t| {
            t.get(i).map(|v| &*v.0)
        })
    })
}

Trait Implementations§

Source§

impl<'rh, T: ?Sized> AsRef<T> for ReadGuard<'rh, T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'rh, T: Debug + ?Sized> Debug for ReadGuard<'rh, T>

Source§

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

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

impl<'rh, T: ?Sized> Deref for ReadGuard<'rh, T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<'rh, T: ?Sized> Drop for ReadGuard<'rh, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'rh, T> Freeze for ReadGuard<'rh, T>
where T: ?Sized,

§

impl<'rh, T> !RefUnwindSafe for ReadGuard<'rh, T>

§

impl<'rh, T> !Send for ReadGuard<'rh, T>

§

impl<'rh, T> !Sync for ReadGuard<'rh, T>

§

impl<'rh, T> Unpin for ReadGuard<'rh, T>
where T: ?Sized,

§

impl<'rh, T> !UnwindSafe for ReadGuard<'rh, T>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.