# [−][src]Struct owning_ref::OwningRef

    pub struct OwningRef<O, T: ?Sized> { /* 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]

#### `pub fn new(o: O) -> Self where
O: StableAddress,
O: Deref<Target = T>,` [src]

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);
    }

#### `pub unsafe fn new_assert_stable_address(o: O) -> Self where
O: Deref<Target = T>,` [src]

Like `new`, but doesn’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 for cases where coherence rules prevents implementing the trait without adding a dependency to this crate in a third-party library.

#### `pub fn map<F, U: ?Sized>(self, f: F) -> OwningRef<O, U> where
O: StableAddress,
F: FnOnce(&T) -> &U,` [src]

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);
    }

#### `pub fn try_map<F, U: ?Sized, E>(self, f: F) -> Result<OwningRef<O, U>, E> where
O: StableAddress,
F: FnOnce(&T) -> Result<&U, E>,` [src]

Tries to convert `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.try_map(|array| {
            if array[2] == 3 { Ok(&array[2]) } else { Err(()) }
        });
        assert_eq!(*owning_ref.unwrap(), 3);
    }

#### `pub unsafe fn map_owner<F, P>(self, f: F) -> OwningRef<P, T> where
O: StableAddress,
P: StableAddress,
F: FnOnce(O) -> P,` [src]

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.

#### `pub fn map_owner_box(self) -> OwningRef<Box<O>, T>`[src]

Converts `self` into a new owning reference where the owner 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>`.

#### `pub fn erase_owner<'a>(self) -> OwningRef<O::Erased, T> where
O: IntoErased<'a>,` [src]

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);
    }

#### `pub fn as_owner(&self) -> &O`[src]

A reference to the underlying owner.

#### `pub fn into_owner(self) -> O`[src]

Discards the reference and retrieves the owner.

## Trait Implementations

### `impl<O, T: ?Sized> Send for OwningRef<O, T> where
O: Send,
&'a T: Send,` [src]

### `impl<O, T: ?Sized> Eq for OwningRef<O, T> where
T: Eq,` [src]

### `impl<O, T: ?Sized> Sync for OwningRef<O, T> where
O: Sync,
&'a T: Sync,` [src]

### `impl<O, T: ?Sized> Clone for OwningRef<O, T> where
O: CloneStableAddress,` [src]

#### `fn clone(&self) -> Self`[src]

Returns a copy of the value. Read more

#### `fn clone_from(&mut self, source: &Self)`1.0.0[src]

Performs copy-assignment from `source`. Read more

### `impl<O, T: ?Sized> AsRef<T> for OwningRef<O, T>`[src]

#### `fn as_ref(&self) -> &T`[src]

Performs the conversion.

### `impl<O, T: ?Sized> PartialOrd<OwningRef<O, T>> for OwningRef<O, T> where
T: PartialOrd,` [src]

#### `fn partial_cmp(&self, other: &Self) -> Option<Ordering>`[src]

This method returns an ordering between `self` and `other` values if one exists. Read more

#### `#[must_use] fn lt(&self, other: &Rhs) -> bool` 1.0.0[src]

This method tests less than (for `self` and `other`) and is used by the `<` operator. Read more

#### `#[must_use] fn le(&self, other: &Rhs) -> bool` 1.0.0[src]

This method tests less than or equal to (for `self` and `other`) and is used by the `<=` operator. Read more

#### `#[must_use] fn gt(&self, other: &Rhs) -> bool` 1.0.0[src]

This method tests greater than (for `self` and `other`) and is used by the `>` operator. Read more

#### `#[must_use] fn ge(&self, other: &Rhs) -> bool` 1.0.0[src]

This method tests greater than or equal to (for `self` and `other`) and is used by the `>=` operator. Read more

### `impl<O, T: ?Sized> PartialEq<OwningRef<O, T>> for OwningRef<O, T> where
T: PartialEq,` [src]

#### `fn eq(&self, other: &Self) -> bool`[src]

This method tests for `self` and `other` values to be equal, and is used by `==`. Read more

#### `#[must_use] fn ne(&self, other: &Rhs) -> bool` 1.0.0[src]

This method tests for `!=`.

### `impl<O, T: ?Sized> Ord for OwningRef<O, T> where
T: Ord,` [src]

#### `fn cmp(&self, other: &Self) -> Ordering`[src]

This method returns an `Ordering` between `self` and `other`. Read more

#### `fn max(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the maximum of two values. Read more

#### `fn min(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the minimum of two values. Read more

#### `fn clamp(self, min: Self, max: Self) -> Self`[src]

🔬 This is a nightly-only experimental API. (`clamp`)

Restrict a value to a certain interval. Read more

### `impl<O, T: ?Sized> From<O> for OwningRef<O, T> where
O: StableAddress,
O: Deref<Target = T>,` [src]

#### `fn from(owner: O) -> Self`[src]

Performs the conversion.

### `impl<O, T: ?Sized> From<OwningRefMut<O, T>> for OwningRef<O, T> where
O: StableAddress,
O: DerefMut<Target = T>,` [src]

#### `fn from(other: OwningRefMut<O, T>) -> Self`[src]

Performs the conversion.

### `impl<O, T: ?Sized> Hash for OwningRef<O, T> where
T: Hash,` [src]

#### `fn hash<H: Hasher>(&self, state: &mut H)`[src]

Feeds this value into the given [`Hasher`]. Read more

#### `fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,` 1.3.0[src]

Feeds a slice of this type into the given [`Hasher`]. Read more

### `impl<O, T: ?Sized> Deref for OwningRef<O, T>`[src]

#### `type Target = T`

The resulting type after dereferencing.

#### `fn deref(&self) -> &T`[src]

Dereferences the value.

### `impl<O, T: ?Sized> Debug for OwningRef<O, T> where
O: Debug,
T: Debug,` [src]

#### `fn fmt(&self, f: &mut Formatter) -> Result<(), Error>`[src]

Formats the value using the given formatter. Read more

### `impl<O, T: ?Sized> Borrow<T> for OwningRef<O, T>`[src]

#### `fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<O, T: ?Sized> StableDeref for OwningRef<O, T>`[src]

### `impl<O, T: ?Sized> CloneStableDeref for OwningRef<O, T> where
O: CloneStableAddress,` [src]

## Auto Trait Implementations

### `impl<O, T: ?Sized> Unpin for OwningRef<O, T> where
O: Unpin,`

### `impl<O, T: ?Sized> UnwindSafe for OwningRef<O, T> where
O: UnwindSafe,
T: RefUnwindSafe,`

### `impl<O, T: ?Sized> RefUnwindSafe for OwningRef<O, T> where
O: RefUnwindSafe,
T: RefUnwindSafe,`

## Blanket Implementations

### `impl<T> Erased for T`[src]

### `impl<T> ToOwned for T where
T: Clone,` [src]

#### `type Owned = T`

The resulting type after obtaining ownership.

#### `fn to_owned(&self) -> T`[src]

Creates owned data from borrowed data, usually by cloning. Read more

#### `fn clone_into(&self, target: &mut T)`[src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into`)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

### `impl<T, U> Into<U> for T where
U: From<T>,` [src]

#### `fn into(self) -> U`[src]

Performs the conversion.

### `impl<T> From<T> for T`[src]

#### `fn from(t: T) -> T`[src]

Performs the conversion.

### `impl<T, U> TryFrom<U> for T where
U: Into<T>,` [src]

#### `type Error = Infallible`

The type returned in the event of a conversion error.

#### `fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

### `impl<T, U> TryInto<U> for T where
U: TryFrom<T>,` [src]

#### `type Error = <U as TryFrom<T>>::Error`

The type returned in the event of a conversion error.

#### `fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.

### `impl<T> BorrowMut<T> for T where
T: ?Sized,` [src]

#### `fn borrow_mut(&mut self) -> &mut T`[src]

Mutably borrows from an owned value. Read more

### `impl<T> Borrow<T> for T where
T: ?Sized,` [src]

#### `fn borrow(&self) -> &T`[src]

Immutably borrows from an owned value. Read more

### `impl<T> Any for T where
T: 'static + ?Sized,` [src]

#### `fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)