Struct owning_ref::OwningRef [−][src]
pub struct OwningRef<O, T: ?Sized> { /* fields omitted */ }Expand description
An owning reference.
This wraps an owner O and a reference &T pointing
at something reachable from O::Target while keeping
the ability to move self around.
The owner is usually a pointer that points at some base type.
For more details and examples, see the module and method docs.
Implementations
Creates a new owning reference from a owner initialized to the direct dereference of it.
Example
extern crate owning_ref;
use owning_ref::OwningRef;
fn main() {
let owning_ref = OwningRef::new(Box::new(42));
assert_eq!(*owning_ref, 42);
}Like new, but dosen’t require O to implement the StableAddress trait.
Instead, the caller is responsible to make the same promises as implementing the trait.
This is useful to use when coherence rules prevent implememnting the trait without adding a dependency to this crate in a third-party library.
Converts self into a new owning reference that points at something reachable
from the previous one.
This can be a reference to a field of U, something reachable from a field of
U, or even something unrelated with a 'static lifetime.
Example
extern crate owning_ref;
use owning_ref::OwningRef;
fn main() {
let owning_ref = OwningRef::new(Box::new([1, 2, 3, 4]));
// create a owning reference that points at the
// third element of the array.
let owning_ref = owning_ref.map(|array| &array[2]);
assert_eq!(*owning_ref, 3);
}Variant of map() that may fail.
pub unsafe fn map_owner<F, P>(self, f: F) -> OwningRef<P, T> where
O: StableAddress,
P: StableAddress,
F: FnOnce(O) -> P,
pub unsafe fn map_owner<F, P>(self, f: F) -> OwningRef<P, T> where
O: StableAddress,
P: StableAddress,
F: FnOnce(O) -> P,
Converts self into a new owning reference with a different owner type.
The new owner type needs to still contain the original owner in some way so that the reference into it remains valid. This function is marked unsafe because the user needs to manually uphold this guarantee.
Converts self into a new owning reference where the onwer is wrapped
in an additional Box<O>.
This can be used to safely erase the owner of any OwningRef<O, T>
to a OwningRef<Box<Erased>, T>.
Erases the concrete base type of the owner with a trait object.
This allows mixing of owned references with different owner base types.
Example
extern crate owning_ref;
use owning_ref::{OwningRef, Erased};
fn main() {
// NB: Using the concrete types here for explicitnes.
// For less verbose code type aliases like `BoxRef` are provided.
let owning_ref_a: OwningRef<Box<[i32; 4]>, [i32; 4]>
= OwningRef::new(Box::new([1, 2, 3, 4]));
let owning_ref_b: OwningRef<Box<Vec<(i32, bool)>>, Vec<(i32, bool)>>
= OwningRef::new(Box::new(vec![(0, false), (1, true)]));
let owning_ref_a: OwningRef<Box<[i32; 4]>, i32>
= owning_ref_a.map(|a| &a[0]);
let owning_ref_b: OwningRef<Box<Vec<(i32, bool)>>, i32>
= owning_ref_b.map(|a| &a[1].0);
let owning_refs: [OwningRef<Box<Erased>, i32>; 2]
= [owning_ref_a.erase_owner(), owning_ref_b.erase_owner()];
assert_eq!(*owning_refs[0], 1);
assert_eq!(*owning_refs[1], 1);
}Discards the reference and retrieves the owner.
Trait Implementations
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
Auto Trait Implementations
impl<O, T: ?Sized> RefUnwindSafe for OwningRef<O, T> where
O: RefUnwindSafe,
T: RefUnwindSafe,
impl<O, T: ?Sized> UnwindSafe for OwningRef<O, T> where
O: UnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
