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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//! Items for moving out fields out of a type,and dropping it afterwards.
pub use ;
/////////////////////////////////////////////////////////////////////////////////
/// For use in macros,
/// to add code that runs before before and after the macro-generated
/// [`DropFields::drop_fields`] code.
///
/// # Safety
///
/// ### Implementing `pre_drop`
///
/// In the implementation of `pre_drop`,
/// you must not read any fields that could have been moved
/// (using the `move_out_*field` methods from `Into*Field`).
///
/// ### Implementing `post_drop`
///
/// In the implementation of `post_drop`,you must only read Copy fields
/// (because all non-Copy fields were dropped).
///
/// # Example
///
/// This example demonstrates how you can use `PrePostDropFields`,
/// as well as `DropFields::pre_move`.
///
/// ```rust
/// use structural::{fp, StructuralExt, Structural};
/// use structural::field::PrePostDropFields;
///
/// {
/// let mut vector=Vec::new();
/// drop(WithDropLogic{
/// vector: &mut vector,
/// x: 0,
/// y: 0,
/// z: 0
/// });
/// // The vector was written to in WithDropLogic's impl of `Drop::drop`
/// assert_eq!(vector, [1000]);
/// }
/// {
/// let mut vector=Vec::new();
/// let this=WithDropLogic{
/// vector: &mut vector,
/// x: 3,
/// y: 5,
/// z: 8
/// };
/// assert_eq!(into_xyz(this), (3, 5, 8));
///
/// // The order in which things happen is:
/// // - `DropFields::pre_move`pushes 2001 into `vector`
/// // - The x,y,and z fields are moved out
/// // - `PrePostDropFields::pre_drop`: pushes 2002 into `vector`
/// // - The non-moved-out fields are dropped,
/// // in this case only the `vector` field wasn't moved out,
/// // because there's no accessor impl to get it.
/// // - `PrePostDropFields::post_drop`: pushes 2002 into `vector`
/// // - The fields are returned
/// assert_eq!(vector, [2001,2002,2003]);
/// }
/// {
/// let this=Variables{
/// x: 13,
/// y: 21,
/// z: 34,
/// };
/// assert_eq!(into_xyz(this), (13, 21, 34));
/// }
///
/// // The `Variables_SI` trait was generated by the `Structural` derive on `Variables`,
/// // aliasing its accessor trait impls.
/// fn into_xyz(this: impl Variables_SI)->(u32,u32,u32) {
/// this.into_fields(fp!(x,y,z))
/// }
///
///
/// #[derive(Structural)]
/// struct Variables{
/// pub x: u32,
/// pub y: u32,
/// pub z: u32,
/// }
///
/// #[derive(Structural)]
/// # #[struc(no_trait)]
/// #[struc(pre_move="WithDropLogic::drop_")]
/// #[struc(pre_post_drop_fields)]
/// struct WithDropLogic<'a>{
/// vector: &'a mut Vec<u32>,
/// pub x: u32,
/// pub y: u32,
/// pub z: u32,
/// }
///
/// impl WithDropLogic<'_>{
/// fn drop_(&mut self){
/// self.vector.push(2001);
/// }
/// }
///
/// unsafe impl PrePostDropFields for WithDropLogic<'_> {
/// unsafe fn pre_drop(this: *mut Self) {
/// let Self{ref mut vector,..}= *this;
/// vector.push(2002);
/// }
///
/// unsafe fn post_drop(this: *mut Self) {
/// let Self{ref mut vector,..}= *this;
/// vector.push(2003);
/// }
/// }
///
///
/// impl Drop for WithDropLogic<'_>{
/// fn drop(&mut self){
/// self.vector.push(1000);
/// }
/// }
///
/// ```
///
pub unsafe
/////////////////////////////////////////////////////////////////////////////////
/// Defines how the type is dropped,
/// after some fields were moved out
/// with the [`IntoField::move_out_field_`] or [`IntoVariantField::move_out_vfield_`] methods.
///
/// # Safety
///
/// Implementors must drop the fields that were not moved out.
///
/// To check whether a field with an `Into*Field` implementation was moved out
/// you must call `moved.is_moved_out()`,
/// and pass the same `FieldBit` argument that was used to mark the fields as moved out
/// in its `Into*Field` implementation.
/// If `is_moved_out` returns false, then you must drop the field.
///
/// # Before move method
///
/// You can define drop logic similar to `Drop::drop` with `pre_move`.
///
/// All fields that have accessor impls must still be usable after `pre_move` is called.
///
/// # Example
///
/// For an example of implementing this trait you can look at:
///
/// - for structs:
/// [The manual implementation example](../trait.IntoField.html#manual-implementation-example)
/// for [`IntoField`].
///
/// - for enums:
/// [The manual implementation example
/// ](../trait.IntoVariantField.html#manual-implementation-example)
/// for [`IntoVariantField`].
///
///
/// [`IntoField::move_out_field_`]: ../trait.IntoField.html#tymethod.move_out_field_
///
/// [`IntoVariantField::move_out_vfield_`]:
/// ../trait.IntoVariantField.html#tymethod.move_out_vfield_
///
/// [`IntoField`]: ../trait.IntoField.html
///
/// [`IntoVariantField`]: ../trait.IntoVariantField.html
pub unsafe
/////////////////////////////////////////////////////////////////////////////////
/// Which fields have been moved out of a value.
///
/// This is used to implement converting a value into multiple fields by value.
///
/// # Example
///
/// This is a toy example,
/// for more realistic examples,you can look
/// at
/// [the manual implementation example of `IntoField`
/// ](../trait.IntoField.html#manual-implementation-example)
/// ,or
/// [the manual implementation example of `IntoVariantField`
/// ](../trait.IntoVariantField.html#manual-implementation-example).
///
/// ```rust
/// use structural::field::{MovedOutFields, FieldBit};
///
/// // This is how `FieldBit` is constructed in the derive macro.
/// const FIELD_A_BIT: FieldBit= FieldBit::new(0);
/// const FIELD_B_BIT: FieldBit= FieldBit::new(1);
///
/// let mut moved = MovedOutFields::new();
///
/// // A `MovedOutFields` considers no field moved when constructed.
/// assert!( !moved.is_moved_out(FIELD_A_BIT) );
/// assert!( !moved.is_moved_out(FIELD_B_BIT) );
///
/// // `set_moved_out` is called in the
/// // `IntoField::move_out_field_`/`IntoVariantField::move_out_vfield_` implementation.
/// moved.set_moved_out(FIELD_A_BIT);
/// moved.set_moved_out(FIELD_B_BIT);
///
/// // `is_moved_out` is called in the `DropFields::drop_fields` implementation,
/// //
/// // If `is_moved_out` returns false, then a field has not been moved out,
/// // and must be dropped.
/// assert!( moved.is_moved_out(FIELD_A_BIT) );
/// assert!( moved.is_moved_out(FIELD_B_BIT) );
///
/// ```
;
/////////////////////////////////////////////////////////////////////////////////
/// Represents the index for a field in [`MovedOutFields`].
///
/// [`MovedOutFields`]: ./struct.MovedOutFields.html
;