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>
impl<T> VisitingMut<T>
Sourcepub fn new(value: T) -> (Self, Return<T>)
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);
Sourcepub fn downgrade(value: Self) -> VisitingRef<T>
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);
Sourcepub fn run_with<U, R>(
value: T,
f: impl FnOnce(VisitingMut<T>) -> R,
) -> impl Future<Output = (T, U)>where
R: Future<Output = U>,
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);