Trait Generic

Source
pub trait Generic {
    type Repr: Repr;

    // Required methods
    fn into_repr(self) -> Self::Repr;
    fn from_repr(repr: Self::Repr) -> Self;
    fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>;
    fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>;
}
Expand description

类型与其表示的互转

Generic::Repr 提供了类型的表示,即其数据的结构。

拥有某类型的数据,可以通过 Generic::into_repr 转换成其表示, 或通过 Generic::from_repr 转换回来。 若有数据的引用,可以通过 Generic::as_repr 转换成引用形式的表示; Generic::as_mut_repr 也类似。注意,引用形式的表示无法转换回来。

可以使用 derive 宏,在 struct 或 enum 上自动实现之。 它无法实现于 &T&mut T,因为无法实现 from_repr

类型的表示,是其底层类型的积的和,呈现为类型层面的列表。 它只体现结构,不包含其他信息,例如字段或变体的名字、枚举的值之类。 表示的伪代码如下:

repr ::= sums
sums ::= Zero | Sum<products, sums>
products ::= One | Product<type, products>
type ::= 任何类型,如 i32、()、Vec<String>

§举例

#[derive(Generic, Debug, PartialEq)]
enum E {
    A(i32),
    B(bool),
    C,
}

assert_eq!(None::<i32>.into_repr(), Sum::This(One));
assert_eq!(<[_; 2]>::from_repr(Sum::This(Product(true, Product(false, One)))), [true, false]);
assert_eq!(E::B(true).as_repr(), Sum::Next(Sum::This(Product(&true, One))));
assert_eq!((1, E::A(1)).as_mut_repr(), Sum::This(Product(&mut 1, Product(&mut E::A(1), One))));

Required Associated Types§

Source

type Repr: Repr

类型的表示

Required Methods§

Source

fn into_repr(self) -> Self::Repr

把数据转换成其表示

Source

fn from_repr(repr: Self::Repr) -> Self

从数据的表示转换成数据

Source

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

获取数据的表示的引用形式

Source

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

获取数据的表示的可变引用形式

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Generic for bool

Source§

type Repr = Sum<One, Sum<One, Zero>>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl Generic for ()

Source§

type Repr = Sum<One, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1> Generic for (T1,)

Source§

type Repr = Sum<Product<T1, One>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2> Generic for (T1, T2)

Source§

type Repr = Sum<Product<T1, Product<T2, One>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3> Generic for (T1, T2, T3)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, One>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4> Generic for (T1, T2, T3, T4)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, One>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5> Generic for (T1, T2, T3, T4, T5)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, One>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5, T6> Generic for (T1, T2, T3, T4, T5, T6)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, Product<T6, One>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5, T6, T7> Generic for (T1, T2, T3, T4, T5, T6, T7)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, Product<T6, Product<T7, One>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8> Generic for (T1, T2, T3, T4, T5, T6, T7, T8)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, Product<T6, Product<T7, Product<T8, One>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Generic for (T1, T2, T3, T4, T5, T6, T7, T8, T9)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, Product<T6, Product<T7, Product<T8, Product<T9, One>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Generic for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, Product<T6, Product<T7, Product<T8, Product<T9, Product<T10, One>>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Generic for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, Product<T6, Product<T7, Product<T8, Product<T9, Product<T10, Product<T11, One>>>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Generic for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)

Source§

type Repr = Sum<Product<T1, Product<T2, Product<T3, Product<T4, Product<T5, Product<T6, Product<T7, Product<T8, Product<T9, Product<T10, Product<T11, Product<T12, One>>>>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for Option<T>

Source§

type Repr = Sum<One, Sum<Product<T, One>, Zero>>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 0]

Source§

type Repr = Sum<One, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 1]

Source§

type Repr = Sum<Product<T, One>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 2]

Source§

type Repr = Sum<Product<T, Product<T, One>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 3]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, One>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 4]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, One>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 5]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 6]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 7]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 8]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 9]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 10]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 11]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T> Generic for [T; 12]

Source§

type Repr = Sum<Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, Product<T, One>>>>>>>>>>>>, Zero>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Source§

impl<T, E> Generic for Result<T, E>

Source§

type Repr = Sum<Product<E, One>, Sum<Product<T, One>, Zero>>

Source§

fn into_repr(self) -> Self::Repr

Source§

fn from_repr(repr: Self::Repr) -> Self

Source§

fn as_repr(&self) -> <Self::Repr as Repr>::Ref<'_>

Source§

fn as_mut_repr(&mut self) -> <Self::Repr as Repr>::Mut<'_>

Implementors§