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
//! [](https://gitlab.com/mexus/fields-converter/commits/master)
//! [](https://crates.io/crates/clone-fields)
//! [](https://docs.rs/clone-fields)
//!
//! [[Release docs]](https://docs.rs/clone-fields/)
//!
//! [[Master docs]](https://mexus.gitlab.io/clone-fields/clone_fields/)
//!
//! Fields-wise types cloning. Nothing more, nothing less.
//!
//! ```
//! use clone_fields::{CloneFields, CloneInto, CloneFrom};
//!
//! // PartialEq and Debug traits are only required for `assert` macros in the example.
//! #[derive(PartialEq, Debug, CloneFields)]
//! #[destinations("External")]
//! struct Original<'a, T> {
//! field1: &'a i64,
//! field2: T,
//! nested: OriginalNested,
//! }
//!
//! #[derive(PartialEq, Debug, CloneFields)]
//! #[destinations("ExternalNested", "ExternalNested2")]
//! struct OriginalNested {
//! value: i32,
//! }
//!
//! // S2 might be a *foreign* type, i.e. declared in a different crate.
//! struct External<'a, T> {
//! field1: &'a i64,
//! field2: T,
//! nested: ExternalNested,
//! }
//!
//! struct ExternalNested {
//! value: i32,
//! }
//!
//! // This struct only exists for the sake of using `destinations` attribute with more than one
//! // type :)
//! struct ExternalNested2 {
//! value: i32,
//! }
//!
//! fn main() {
//! let obj: Original<_> = Original {
//! field1: &0,
//! field2: String::from("lol"),
//! nested: OriginalNested { value: 15 }
//! };
//! let cloned: External<_> = obj.clone_into();
//! assert_eq!(obj.field1, cloned.field1);
//! assert_eq!(obj.field2, cloned.field2);
//! assert_eq!(obj.nested.value, cloned.nested.value);
//! let cloned2 = Original::clone_from(&cloned);
//! assert_eq!(cloned.field1, cloned2.field1);
//! assert_eq!(cloned.field2, cloned2.field2);
//! assert_eq!(obj, cloned2);
//! }
//! ```
extern crate fields_converter_derive;
pub use CloneFields;
/// A trait to clone a type into another type field-by-field.
///
/// It is automatically implemented for any type pair for which `CloneFrom` is implemented, i.e.
/// `T1: CloneFrom<T2>` => `T2: CloneInto<T1>`.
///
/// Refer to [clone-fields-derive](https://docs.rs/clone-fields-derive) docs for info on how to
/// automatically derive this trait. Yes, you typically don't need to do it manually.
/// Construct a type from another by cloning its fields.
///
/// It is automatically implemented for any clonable type, i.e. `CloneFrom<T> for T` where
/// `T: Clone`.