[][src]Trait into_owned::IntoOwned

pub trait IntoOwned where
    Self: Sized + Borrow<Self::Owned>, 
{ type Owned; fn is_owned(&self) -> bool;
fn into_owned(self) -> Self::Owned;
fn try_as_mut(&mut self) -> Option<&mut Self::Owned>;
fn as_is<'a>(self) -> Is<'a, Self>; }

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

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

impl IntoOwned for A {
    type Owned = Self;

    fn is_owned(&self) -> bool {
        true
    }

    fn into_owned(self) -> Self {
        self
    }

    fn try_as_mut(&mut self) -> Option<&mut Self> {
        Some(self)
    }

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

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.

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

assert_eq!(is_owned(A()), true);
assert_eq!(is_owned(&A()), false);
assert_eq!(is_owned(&mut A()), false);

Associated Types

type Owned

The owned type associated with Self.

Loading content...

Required methods

fn is_owned(&self) -> bool

Returns true if self is an owned value (e.g. if Self and Self::Owned are the same), or false otherwise.

This method is used as a hint of whether into_owned method is cheap or not.

fn into_owned(self) -> Self::Owned

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.

fn try_as_mut(&mut self) -> Option<&mut Self::Owned>

Returns a mutable reference to the owned value of self if possible. This method should be cheap.

If a mutable reference is required and taking the reference must be cheap, BorrowMut trait should be used instead.

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

Returns self as Is<'a, Self>.

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