pub trait WithRef {
type Item;
// Required method
fn with_ref<F, R>(&self, f: F) -> R
where F: FnOnce(&Self::Item) -> R;
// Provided methods
fn cloned(&self) -> Self::Item
where Self::Item: Clone { ... }
fn copied(&self) -> Self::Item
where Self::Item: Copy { ... }
}Expand description
Similar to AsRef, except that instead of returning &T, it accepts a
closure that takes &T.
This is required for getting a &T from a Ref or
RefMut because these types only contain references to each of
T’s fields. In order to get a reference to T, T must first be
constructed on the stack, but returning that reference is a lifetime
violation. Therefore, we must invert the control flow.
This trait also provides the cloned and copied convenience methods
for getting a clone of T from Ref<T> or RefMut<T>.
Note that the same idea applied to &mut T is less effective, as we would
have to write all the fields to T back to their actual storage locations
in the Soa after the closure, even if only some of the fields were
modified.
Required Associated Types§
Required Methods§
Provided Methods§
Object Safety§
This trait is not object safe.