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>
impl<T> VisitingRef<T>
Sourcepub fn new(value: T) -> (Self, Return<T>)
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);
Sourcepub fn run_with<U, R>(
value: T,
f: impl FnOnce(VisitingRef<T>) -> R,
) -> impl Future<Output = (T, U)>where
R: Future<Output = U>,
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,
impl<T> Binary for VisitingRef<T>where
T: Binary,
Source§impl<T> Debug for VisitingRef<T>where
T: Debug,
impl<T> Debug for VisitingRef<T>where
T: Debug,
Source§impl<T> Deref for VisitingRef<T>
impl<T> Deref for VisitingRef<T>
Source§impl<T> Display for VisitingRef<T>where
T: Display,
impl<T> Display for VisitingRef<T>where
T: Display,
Source§impl<T> From<VisitingMut<T>> for VisitingRef<T>
impl<T> From<VisitingMut<T>> for VisitingRef<T>
Source§fn from(value: VisitingMut<T>) -> Self
fn from(value: VisitingMut<T>) -> Self
Converts to this type from the input type.
Source§impl<T> LowerExp for VisitingRef<T>where
T: LowerExp,
impl<T> LowerExp for VisitingRef<T>where
T: LowerExp,
Source§impl<T> LowerHex for VisitingRef<T>where
T: LowerHex,
impl<T> LowerHex for VisitingRef<T>where
T: LowerHex,
Source§impl<T> Octal for VisitingRef<T>where
T: Octal,
impl<T> Octal for VisitingRef<T>where
T: Octal,
Source§impl<T> Pointer for VisitingRef<T>where
T: Pointer,
impl<T> Pointer for VisitingRef<T>where
T: Pointer,
Source§impl<T> UpperExp for VisitingRef<T>where
T: UpperExp,
impl<T> UpperExp for VisitingRef<T>where
T: UpperExp,
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>
impl<T> Unpin for VisitingRef<T>where
T: Unpin,
impl<T> !UnwindSafe for VisitingRef<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more