Trait frunk_core::generic::Generic [−][src]
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:
forall x : Self. x == Generic::from(Generic::into(x))
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
#[macro_use] extern crate frunk; #[macro_use] extern crate frunk_core; #[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); // doneRun
Associated Types
Loading content...Required methods
fn into(self) -> Self::Repr
[src]
Convert a value to its representation type Repr
.
fn from(repr: Self::Repr) -> Self
[src]
Convert a value’s representation type Repr
to the value’s type.
Provided methods
fn convert_from<Src>(src: Src) -> Self where
Self: Sized,
Src: Generic<Repr = Self::Repr>,
[src]
Self: Sized,
Src: Generic<Repr = Self::Repr>,
Convert a value to another type provided that they have the same representation type.
fn map_repr<Mapper>(self, mapper: Mapper) -> Self where
Self: Sized,
Mapper: FnOnce(Self::Repr) -> Self::Repr,
[src]
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
.
fn map_inter<Inter, Mapper>(self, mapper: Mapper) -> Self where
Self: Sized,
Inter: Generic<Repr = Self::Repr>,
Mapper: FnOnce(Inter) -> Inter,
[src]
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
.