Struct VisitingMut

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

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

VisitingMut implements both Deref and DerefMut to allow for immutable or mutable 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 and DerefMut trait documentation.

VisitingMut instances can be permanently downgraded to VisitingRef using either VisitingMut::downgrade or the From trait implementation for VisitingRef. A VisitingRef cannot be converted back into a VisitingMut.

Implementations§

Source§

impl<T> VisitingMut<T>

Source

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

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

§Examples
use visiting_ref::VisitingMut;

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

*item = 7;

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

pub fn downgrade(value: Self) -> VisitingRef<T>

Permanently downgrades a VisitingMut into a VisitingRef.

§Examples
use visiting_ref::VisitingMut;

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

*item = 7;

let item = VisitingMut::downgrade(item);
assert_eq!(*item, 7);
Source

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

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

§Examples
use visiting_ref::VisitingMut;

struct Foo {
    value: i32,
}

let foo = Foo { value: 27 };

let (foo, result) = VisitingMut::run_with(foo, |mut foo| {
    async move {
        foo.value *= 3;
        foo.value
    }
})
.await;

assert_eq!(result, 81);
assert_eq!(foo.value, 81);

Trait Implementations§

Source§

impl<T> Binary for VisitingMut<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 VisitingMut<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 VisitingMut<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T> DerefMut for VisitingMut<T>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T> Display for VisitingMut<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 VisitingMut<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 VisitingMut<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 VisitingMut<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 VisitingMut<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 VisitingMut<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 VisitingMut<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 VisitingMut<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for VisitingMut<T>

§

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

§

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

§

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

§

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

Source§

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.