pub trait TypeIdentity {
    type Type: ?Sized;

    fn into_type(self) -> Self::Type
    where
        Self: Sized,
        Self::Type: Sized
, { ... } fn as_type(&self) -> &Self::Type { ... } fn as_type_mut(&mut self) -> &mut Self::Type { ... } fn into_type_box(self: Box<Self>) -> Box<Self::Type> { ... } fn into_type_arc(self: Arc<Self>) -> Arc<Self::Type> { ... } fn into_type_rc(self: Rc<Self>) -> Rc<Self::Type> { ... } fn from_type(this: Self::Type) -> Self
    where
        Self: Sized,
        Self::Type: Sized
, { ... } fn from_type_ref(this: &Self::Type) -> &Self { ... } fn from_type_mut(this: &mut Self::Type) -> &mut Self { ... } fn from_type_box(this: Box<Self::Type>) -> Box<Self> { ... } fn from_type_arc(this: Arc<Self::Type>) -> Arc<Self> { ... } fn from_type_rc(this: Rc<Self::Type>) -> Rc<Self> { ... } }
Available on crate feature type_identity only.
Expand description

Allows converting Self to Self::Type by proving that both types are equal.

Usecases

This trait allows:

  • Defining extension traits without repeating method signatures.

  • Creating a type Alias in a where clause, eg: Vec<i32>: TypeIdentity<Type = List>.

  • Unwrapping a generic type, eg: I: TypeIdentity<Type = Option<U>>.

Example

Defining an extension trait on Vec<T>.

use core_extensions::TypeIdentity;

trait VecExt<T>: TypeIdentity<Type = Vec<T>> + Sized {
    fn is_nonempty(&self) -> bool {
        !self.as_type().is_empty()
    }

    fn moved_vec(self) -> Vec<T> {
        self.into_type()
    }

    fn mutable_vec(&mut self) -> &mut Vec<T> {
        self.as_type_mut()
    }
}
impl<T> VecExt<T> for Vec<T> {}

assert!( vec![100].is_nonempty());
assert!(!Vec::<i32>::new().is_nonempty());

Example of a method requiring Self == Other

Wrapper::iter is only callable on Wrapper<Vec<T>>

use core_extensions::TypeIdentity;

use std::slice;

struct Wrapper<U>(U);

impl<U> Wrapper<U> {
     fn iter<T>(&self) -> slice::Iter<T>
     where U: TypeIdentity<Type = Vec<T>>
     {
         self.0.as_type().iter()
     }
}

assert_eq!(
    Wrapper(vec![0, 1, 2, 3, 4]).iter().cloned().collect::<Vec<_>>(),
    vec![0, 1, 2, 3, 4]
);

Example of creating a type alias in a where clause

use core_extensions::TypeIdentity;

use std::ops::Deref;

struct Example<T>(T);

impl<T, Target0, Target1> Deref for Example<T>
where
    T: Deref,
    <T as Deref>::Target: TypeIdentity<Type = Target0>,
    Target0: Deref,
    <Target0 as Deref>::Target: TypeIdentity<Type = Target1>,
{   
    type Target = Target1;
     
    fn deref(&self) -> &Target1 {
        &**self
    }
}

Required Associated Types

This is always Self.

Provided Methods

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

Available on crate feature alloc only.

Converts a box back to the original type.

Available on crate feature alloc only.

Converts an Arc back to the original type.

Self parameter

Enabling the “rust_1_46” feature changes this method from taking a this parameter to taking aself parameter, which allows calling it with .into_type_arc()

Available on crate feature alloc only.

Converts an Rc back to the original type.

Self parameter

Enabling the “rust_1_46” feature changes this method from taking a this parameter to taking aself parameter, which allows calling it with .into_type_rc()

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

Available on crate feature alloc only.

Converts a box back to the original type.

Available on crate feature alloc only.

Converts an Arc back to the original type.

Available on crate feature alloc only.

Converts an Rc back to the original type.

Implementors