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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! This module holds the machinery behind `Generic`.
//!
//! It contains the `Generic` trait and some helper methods for using the
//! `Generic` trait without having to use universal function call syntax.
//!
//! # Examples
//!
//! ```rust
//! use frunk::Generic;
//!
//! # fn main() {
//! #[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
//! # }
/// 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
///
/// ```rust
/// use frunk::Generic;
///
/// # fn main() {
/// #[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
/// # }
/// ```
/// Given a generic representation `Repr` of a `Dst`, returns `Dst`.
/// Given a value of type `Src`, returns its generic representation `Repr`.
/// Converts one type `Src` into another type `Dst` assuming they have the same
/// representation type `Repr`.
/// Maps a value of a given type `Origin` using a function on
/// the representation type `Repr` of `Origin`.
/// Maps a value of a given type `Origin` using a function on
/// a type `Inter` which has the same representation type of `Origin`.
///
/// Note that the compiler will have a hard time inferring the type variable
/// `Inter`. Thus, using `map_inter` is mostly effective if the type is
/// constrained by the input function or by the body of a lambda.