Struct owning_ref::OwningRef
[−]
[src]
pub struct OwningRef<O, T: ?Sized> { // some fields omitted }
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.
Methods
impl<O, T: ?Sized> OwningRef<O, T>
[src]
fn new(o: O) -> Self where O: StableAddress, O: Deref<Target=T>
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); }
fn map<F, U: ?Sized>(self, f: F) -> OwningRef<O, U> where O: StableAddress, F: FnOnce(&T) -> &U
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); }
fn erase_owner<'a>(self) -> OwningRef<O::Erased, T> where O: IntoErased<'a>
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); }
fn owner(&self) -> &O
A getter for the underlying owner.
fn into_inner(self) -> O
Discards the reference and retrieves the owner.
Trait Implementations
impl<O, T: ?Sized> Deref for OwningRef<O, T>
[src]
type Target = T
The resulting type after dereferencing
fn deref(&self) -> &T
The method called to dereference a value
impl<O, T: ?Sized> AsRef<T> for OwningRef<O, T>
[src]
fn as_ref(&self) -> &T
Performs the conversion.
impl<O, T: ?Sized> Borrow<T> for OwningRef<O, T>
[src]
impl<O, T: ?Sized> From<O> for OwningRef<O, T> where O: StableAddress, O: Deref<Target=T>
[src]
fn from(owner: O) -> Self
Performs the conversion.
impl<O, T: ?Sized> Debug for OwningRef<O, T> where O: Debug, T: Debug
[src]
impl<O, T: ?Sized> Clone for OwningRef<O, T> where O: CloneStableAddress
[src]
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<O: Send, T: ?Sized> Send for OwningRef<O, T>
[src]
impl<O: Sync, T: ?Sized> Sync for OwningRef<O, T>
[src]
impl<O, T: ?Sized> PartialEq for OwningRef<O, T> where T: PartialEq
[src]
fn eq(&self, other: &Self) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.
impl<O, T: ?Sized> Eq for OwningRef<O, T> where T: Eq
[src]
impl<O, T: ?Sized> PartialOrd for OwningRef<O, T> where T: PartialOrd
[src]
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
1.0.0
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, other: &Rhs) -> bool
1.0.0
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, other: &Rhs) -> bool
1.0.0
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl<O, T: ?Sized> Ord for OwningRef<O, T> where T: Ord
[src]
fn cmp(&self, other: &Self) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl<O, T: ?Sized> Hash for OwningRef<O, T> where T: Hash
[src]
fn hash<H: Hasher>(&self, state: &mut H)
Feeds this value into the state given, updating the hasher as necessary.
fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0
Feeds a slice of this type into the state provided.