Trait frunk_core::labelled::LabelledGeneric
source · pub trait LabelledGeneric {
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 Src: LabelledGeneric<Repr = Self::Repr>,
Self: Sized { ... }
fn sculpted_convert_from<A, Indices>(a: A) -> Self
where A: LabelledGeneric,
Self: Sized,
<A as LabelledGeneric>::Repr: Sculptor<<Self as LabelledGeneric>::Repr, Indices> { ... }
fn transform_from<Src, Indices>(src: Src) -> Self
where Src: LabelledGeneric,
Self: Sized,
<Src as LabelledGeneric>::Repr: Sculptor<<Self as LabelledGeneric>::Repr, Indices> { ... }
}
Expand description
A trait that converts from a type to a labelled generic representation.
LabelledGeneric
s allow us to have completely type-safe,
boilerplate free conversions between different structs.
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.
§Examples
use frunk::LabelledGeneric;
#[derive(LabelledGeneric)]
struct NewUser<'a> {
first_name: &'a str,
last_name: &'a str,
age: usize,
}
// Notice that the fields are mismatched in terms of ordering
#[derive(LabelledGeneric)]
struct SavedUser<'a> {
last_name: &'a str,
age: usize,
first_name: &'a str,
}
let n_user = NewUser {
first_name: "Joe",
last_name: "Blow",
age: 30,
};
// transform_from automagically sculpts the labelled generic
// representation of the source object to that of the target type
let s_user: SavedUser = frunk::transform_from(n_user); // done
Required Associated Types§
Required Methods§
Provided Methods§
sourcefn convert_from<Src>(src: Src) -> Self
fn convert_from<Src>(src: Src) -> Self
Convert from one type to another using a type with the same labelled generic representation
sourcefn sculpted_convert_from<A, Indices>(a: A) -> Selfwhere
A: LabelledGeneric,
Self: Sized,
<A as LabelledGeneric>::Repr: Sculptor<<Self as LabelledGeneric>::Repr, Indices>,
👎Deprecated: obsolete, transform_from instead
fn sculpted_convert_from<A, Indices>(a: A) -> Selfwhere
A: LabelledGeneric,
Self: Sized,
<A as LabelledGeneric>::Repr: Sculptor<<Self as LabelledGeneric>::Repr, Indices>,
Converts from another type A into Self assuming that A and Self have labelled generic representations that can be sculpted into each other.
Note that this method tosses away the “remainder” of the sculpted representation. In other words, anything that is not needed from A gets tossed out.
sourcefn transform_from<Src, Indices>(src: Src) -> Selfwhere
Src: LabelledGeneric,
Self: Sized,
<Src as LabelledGeneric>::Repr: Sculptor<<Self as LabelledGeneric>::Repr, Indices>,
fn transform_from<Src, Indices>(src: Src) -> Selfwhere
Src: LabelledGeneric,
Self: Sized,
<Src as LabelledGeneric>::Repr: Sculptor<<Self as LabelledGeneric>::Repr, Indices>,
Converts from another type Src
into Self
assuming that Src
and
Self
have labelled generic representations that can be sculpted into
each other.
Note that this method tosses away the “remainder” of the sculpted
representation. In other words, anything that is not needed from Src
gets tossed out.