[][src]Trait into_owned::IntoOwned

pub trait IntoOwned where
    Self: Sized
{ type Owned: Owned; fn as_is<'a>(self) -> Is<'a, Self::Owned>
    where
        Self: 'a
; fn into_owned(self) -> Self::Owned
    where
        Self::Owned: Clone
, { ... }
fn into_gyu<'a>(self) -> Gyu<'a, Self::Owned>
    where
        Self: 'a
, { ... }
fn try_into_gyu_mut<'a>(self) -> Option<GyuMut<'a, Self::Owned>>
    where
        Self: 'a
, { ... }
fn to_gyu_mut<'a>(self) -> GyuMut<'a, Self::Owned>
    where
        Self: 'a,
        Self::Owned: Clone
, { ... }
fn into_cow<'a, B: ?Sized>(self) -> Cow<'a, B>
    where
        Self: 'a,
        Self::Owned: Borrow<B>,
        B: ToOwned<Owned = Self::Owned>
, { ... } }

A trait for associating a type with its owned variant.

Examples

Implementing the IntoOwned trait for an owned type is straighitforward. Maybe a derive macro is supplied in future.

use into_owned::{IntoOwned, Is};

struct A();

impl IntoOwned for A {
    type Owned = Self;

    fn as_is<'a>(self) -> Is<'a, Self::Owned> {
        Is::Owned(self)
    }
}

Since there are blanket implementations for &T and &mut T where T: IntoOwned<Owned = T>, manually implementing IntoOwned for the owned type is sufficient in most cases.

Associated Types

type Owned: Owned

The owned type associated with Self.

Loading content...

Required methods

fn as_is<'a>(self) -> Is<'a, Self::Owned> where
    Self: 'a, 

Returns self as Is<'a, T> (T is Self::Owned here).

This method is useful when self is used differently depending on its state: owned, (immutably) borrowed, or mutably borrowed.

Examples

use into_owned::{IntoOwned, Is};

// returns:
// * `0` if `t` is an owned value
// * `1` if `t` is an immutably borrowed reference
// * `2` if `t` is a mutably borrowed reference
fn a<T>(t: T) -> i8
where T: IntoOwned
{
    match t.as_is() {
        Is::Owned(x) => {
            // here `x: T::Owned`
            0
        }
        Is::Borrowed(x) => {
            // here `x: &T::Owned`
            1
        }
        Is::MutBorrowed(x) => {
            // here `x: &mut T::Owned`
            2
        }
    }
}

assert_eq!(a(1.0), 0);
assert_eq!(a(&1.0), 1);
assert_eq!(a(&mut 1.0), 2);

In addition, Is<'a, T> has a functionality similar to Cow<'a, B>. See its documentation for more.

Loading content...

Provided methods

fn into_owned(self) -> Self::Owned where
    Self::Owned: Clone

Converts self into an owned value.

If Self and Self::Owned are the same, usually it just returns self. If not, usually it returns an owned value by cloning.

This method requires Self::Owned to be Clone.

fn into_gyu<'a>(self) -> Gyu<'a, Self::Owned> where
    Self: 'a, 

Converts self into Gyu<'a, T> (T is Self::Owned here).

Gyu<'a, T> has a functionality similar to Is<'a, T>. Unlike Is<'a, T>, it cannot hold a mutably borrowed reference, and thus it is more similar to Cow<'a, B>. See its documentation for more.

fn try_into_gyu_mut<'a>(self) -> Option<GyuMut<'a, Self::Owned>> where
    Self: 'a, 

Converts self into GyuMut<'a, T> (T is Self::Owned here) if possible, i.e., self is not an immutably borrowed reference. If GyuMut<'a, T> is necessary, use to_gyu_mut method instead.

GyuMut<'a, T> has a functionality similar to Is<'a, T>. Unlike Is<'a, T>, it cannot hold a immutably borrowed reference. GyuMut<'a, T> implements more traits which require mutablity. See its documentation for more.

fn to_gyu_mut<'a>(self) -> GyuMut<'a, Self::Owned> where
    Self: 'a,
    Self::Owned: Clone

Converts self into GyuMut<'a, T> (T is Self::Owned here). Unlike try_into_gyu_mut method, it clones self if it is an immutably borrowed reference.

fn into_cow<'a, B: ?Sized>(self) -> Cow<'a, B> where
    Self: 'a,
    Self::Owned: Borrow<B>,
    B: ToOwned<Owned = Self::Owned>, 

Converts self into Cow<'a, B>.

This method is available if std feature is enabled (enabled by default).

Loading content...

Implementations on Foreign Types

impl<'_, T> IntoOwned for &'_ T where
    T: Owned
[src]

type Owned = T

impl<'_, T> IntoOwned for &'_ mut T where
    T: Owned
[src]

type Owned = T

impl IntoOwned for i8[src]

type Owned = Self

impl IntoOwned for u8[src]

type Owned = Self

impl IntoOwned for i16[src]

type Owned = Self

impl IntoOwned for u16[src]

type Owned = Self

impl IntoOwned for i32[src]

type Owned = Self

impl IntoOwned for u32[src]

type Owned = Self

impl IntoOwned for i64[src]

type Owned = Self

impl IntoOwned for u64[src]

type Owned = Self

impl IntoOwned for isize[src]

type Owned = Self

impl IntoOwned for usize[src]

type Owned = Self

impl IntoOwned for i128[src]

type Owned = Self

impl IntoOwned for u128[src]

type Owned = Self

impl IntoOwned for f32[src]

type Owned = Self

impl IntoOwned for f64[src]

type Owned = Self

Loading content...

Implementors

impl<'_, T> IntoOwned for Gyu<'_, T> where
    T: Owned
[src]

type Owned = T

impl<'_, T> IntoOwned for GyuMut<'_, T> where
    T: Owned
[src]

type Owned = T

impl<'_, T> IntoOwned for Is<'_, T> where
    T: Owned
[src]

type Owned = T

Loading content...