Trait frunk::generic::Generic

source ·
pub trait Generic {
    type Repr;

    // Required methods
    fn into(self) -> Self::Repr;
    fn from(repr: Self::Repr) -> Self;

    // Provided methods
    fn convert_from<Src>(src: Src) -> Self
       where Self: Sized,
             Src: Generic<Repr = Self::Repr> { ... }
    fn map_repr<Mapper>(self, mapper: Mapper) -> Self
       where Self: Sized,
             Mapper: FnOnce(Self::Repr) -> Self::Repr { ... }
    fn map_inter<Inter, Mapper>(self, mapper: Mapper) -> Self
       where Self: Sized,
             Inter: Generic<Repr = Self::Repr>,
             Mapper: FnOnce(Inter) -> Inter { ... }
}
Expand description

A trait that converts from a type to a generic representation.

For the most part, you should be using the derivation that is available through frunk_derive to generate instances of this trait for your types.

§Laws

Any implementation of Generic must satisfy the following two laws:

  1. forall x : Self. x == Generic::from(Generic::into(x))
  2. forall y : Repr. y == Generic::into(Generic::from(y))

That is, from and into should make up an isomorphism between Self and the representation type Repr.

§Examples

use frunk::Generic;

#[derive(Generic)]
struct ApiPerson<'a> {
    FirstName: &'a str,
    LastName: &'a str,
    Age: usize,
}

#[derive(Generic)]
struct DomainPerson<'a> {
    first_name: &'a str,
    last_name: &'a str,
    age: usize,
}

let a_person = ApiPerson {
    FirstName: "Joe",
    LastName: "Blow",
    Age: 30,
};
let d_person: DomainPerson = frunk::convert_from(a_person); // done

Required Associated Types§

source

type Repr

The generic representation type.

Required Methods§

source

fn into(self) -> Self::Repr

Convert a value to its representation type Repr.

source

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

Convert a value’s representation type Repr to the value’s type.

Provided Methods§

source

fn convert_from<Src>(src: Src) -> Self
where Self: Sized, Src: Generic<Repr = Self::Repr>,

Convert a value to another type provided that they have the same representation type.

source

fn map_repr<Mapper>(self, mapper: Mapper) -> Self
where Self: Sized, Mapper: FnOnce(Self::Repr) -> Self::Repr,

Maps the given value of type Self by first transforming it to the representation type Repr, then applying a mapper function on Repr and finally transforming it back to a value of type Self.

source

fn map_inter<Inter, Mapper>(self, mapper: Mapper) -> Self
where Self: Sized, Inter: Generic<Repr = Self::Repr>, Mapper: FnOnce(Inter) -> Inter,

Maps the given value of type Self by first transforming it a type Inter that has the same representation type as Self, then applying a mapper function on Inter and finally transforming it back to a value of type Self.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Generic for ()

source§

type Repr = HNil

source§

fn into(self) -> <() as Generic>::Repr

source§

fn from(_: <() as Generic>::Repr)

source§

impl<T0> Generic for (T0,)

source§

type Repr = HCons<T0, HNil>

source§

fn into(self) -> <(T0,) as Generic>::Repr

source§

fn from(r: <(T0,) as Generic>::Repr) -> (T0,)

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl<T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0> Generic for (T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0)

source§

impl<T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0> Generic for (T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0)

source§

impl<T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0> Generic for (T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0)

source§

impl<T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0> Generic for (T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0)

source§

impl<T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0> Generic for (T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0)

source§

impl<T22, T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0> Generic for (T22, T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0)

source§

impl<T23, T22, T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0> Generic for (T23, T22, T21, T20, T19, T18, T17, T16, T15, T14, T13, T12, T11, T10, T9, T8, T7, T6, T5, T4, T3, T2, T1, T0)

Implementors§