visiting_ref

Struct VisitingRef

Source
pub struct VisitingRef<T> { /* private fields */ }
Expand description

Container that automatically returns ownership of a value to another async context upon exiting scope, allowing immutable access to the value while active.

VisitingRef implements Deref to allow for immutable access to the wrapped value, either explicitly using the unary * operator or implicitly by the compiler under various circumstances. More information can be found in the Deref trait documentation.

For mutable value access, see VisitingMut.

Implementations§

Source§

impl<T> VisitingRef<T>

Source

pub fn new(value: T) -> (Self, Return<T>)

Creates a new VisitingRef wrapping the given value, along with a future that resolves back the wrapped value once the VisitingRef is dropped.

§Examples
use visiting_ref::VisitingRef;

let (item, receiver) = VisitingRef::new(5);
assert_eq!(*item, 5);

drop(item);
let original = receiver.await;
assert_eq!(original, 5);
Source

pub fn run_with<U, R>( value: T, f: impl FnOnce(VisitingRef<T>) -> R, ) -> impl Future<Output = (T, U)>
where R: Future<Output = U>,

Wraps a given T value in a VisitingRef and runs an asynchronous closure with the VisitingRef<T> as its argument.

§Examples
use visiting_ref::VisitingRef;

struct Foo {
    value: i32,
}

let foo = Foo { value: 27 };

let (foo, result) = VisitingRef::run_with(foo, |foo| async move { foo.value * 3 }).await;
assert_eq!(result, 81);
assert_eq!(foo.value, 27);

Trait Implementations§

Source§

impl<T> Binary for VisitingRef<T>
where T: Binary,

Source§

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

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

impl<T> Debug for VisitingRef<T>
where T: Debug,

Source§

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

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

impl<T> Deref for VisitingRef<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> Display for VisitingRef<T>
where T: Display,

Source§

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

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

impl<T> From<VisitingMut<T>> for VisitingRef<T>

Source§

fn from(value: VisitingMut<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> LowerExp for VisitingRef<T>
where T: LowerExp,

Source§

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

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

impl<T> LowerHex for VisitingRef<T>
where T: LowerHex,

Source§

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

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

impl<T> Octal for VisitingRef<T>
where T: Octal,

Source§

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

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

impl<T> Pointer for VisitingRef<T>
where T: Pointer,

Source§

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

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

impl<T> UpperExp for VisitingRef<T>
where T: UpperExp,

Source§

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

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

impl<T> UpperHex for VisitingRef<T>
where T: UpperHex,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for VisitingRef<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for VisitingRef<T>

§

impl<T> Send for VisitingRef<T>
where T: Send,

§

impl<T> Sync for VisitingRef<T>
where T: Sync + Send,

§

impl<T> Unpin for VisitingRef<T>
where T: Unpin,

§

impl<T> !UnwindSafe for VisitingRef<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<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.