1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/// 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 typeclass for your types. /// /// I would highly recommend you check out `derivation_tests.rs` to see how to actually use /// this trait in real life. Since frunk_derive depends on this trait, I can't actually /// pull it in as a dependency here (otherwise the dependency would be circular) and show /// how to use it in a proper doc test. /// /// ```rust,ignore /// #[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 { /// first_name: "Joe", /// last_name: "Blow", /// age: 30, /// }; /// let d_person: DomainPerson = convert_from(a_person); // done /// ``` pub trait Generic<Repr> { /// Go from something to Repr fn into(self) -> Repr; /// Go from Repr to something fn from(r: Repr) -> Self; } /// Given a generic Representation of an A, returns A pub fn from_generic<A, Repr>(gen: Repr) -> A where A: Generic<Repr> { <A as Generic<Repr>>::from(gen) } /// Given an A, returns its generic Representation pub fn into_generic<A, Repr>(a: A) -> Repr where A: Generic<Repr> { <A as Generic<Repr>>::into(a) } /// Converts one type into another assuming they have the same generic Representation pub fn convert_from<A, B, Repr>(a: A) -> B where A: Generic<Repr>, B: Generic<Repr> { let repr = <A as Generic<Repr>>::into(a); <B as Generic<Repr>>::from(repr) }