[][src]Trait into_owned::IntoOwned

pub trait IntoOwned where
    Self: Borrow<Self::Owned>, 
{ type Owned; const IS_OWNED: bool; fn into_owned(self) -> 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. Since there are blanket implementations for &T and &mut T where T: IntoOwned<Owned = T> + Clone, manually implementing IntoOwned for the owned type is sufficient in most cases.

use into_owned::IntoOwned;

#[derive(Clone)]
struct A();

impl IntoOwned for A {
    type Owned = Self;

    const IS_OWNED: bool = true;

    fn into_owned(self) -> Self::Owned {
        self
    }
}

// this function can be called with an owned or borrowed value
fn is_owned<T>(t: T) -> bool
where T: IntoOwned
{
    T::IS_OWNED
}

assert!(is_owned(A()));
assert!(!is_owned(&A()));
assert!(!is_owned(&mut A()));

Associated Types

type Owned

The owned type associated with Self.

Loading content...

Associated Constants

const IS_OWNED: bool

true if Self is an owned type, false otherwise.

Loading content...

Required methods

fn into_owned(self) -> Self::Owned

Converts self into an owned value.

If self is an owned value (i.e. Self and Self::Owned are the same), usually it just returns self. If self is a borrowed value, usually it returns an owned value by cloning.

Loading content...

Implementations on Foreign Types

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

type Owned = T

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

type Owned = T

impl IntoOwned for i8[src]

type Owned = Self

impl IntoOwned for i16[src]

type Owned = Self

impl IntoOwned for i32[src]

type Owned = Self

impl IntoOwned for i64[src]

type Owned = Self

impl IntoOwned for i128[src]

type Owned = Self

impl IntoOwned for isize[src]

type Owned = Self

impl IntoOwned for u8[src]

type Owned = Self

impl IntoOwned for u16[src]

type Owned = Self

impl IntoOwned for u32[src]

type Owned = Self

impl IntoOwned for u64[src]

type Owned = Self

impl IntoOwned for u128[src]

type Owned = Self

impl IntoOwned for usize[src]

type Owned = Self

impl IntoOwned for f32[src]

type Owned = Self

impl IntoOwned for f64[src]

type Owned = Self

Loading content...

Implementors

Loading content...