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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/// Obtain `&MaybeUninit<_>` references to fields of a struct wrapped in `MaybeUninit<_>`.
///
/// This must be used in an `unsafe` block or function when accessing fields of unions.
///
/// ## Syntax
/// ```
/// # use core::mem::MaybeUninit;
/// # use project_uninit::project_uninit;
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Person { name: Name, age: u32, id: (usize, usize) }
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Name { first: &'static str, last: &'static str }
/// # let mut bob = MaybeUninit::new(Person {
/// # name: Name { first: "Bob1", last: "Jones1" },
/// # age: 34, id: (111, 222),
/// # });
/// // Access a single field:
/// let age: &MaybeUninit<u32> = project_uninit!(bob => age);
///
/// // Access multiple fields:
/// let (age, id): (&MaybeUninit<_>, &MaybeUninit<_>) = project_uninit!(
/// bob => { age, id }
/// );
///
/// // Access fields of fields:
/// let first: &MaybeUninit<&str> = project_uninit!(bob => name => first);
///
/// // Access fields of tuples (also works for tuple structs):
/// let id0: &MaybeUninit<usize> = project_uninit!(bob => id => 0);
///
/// // Access multiple fields, including nested fields:
/// let (first, last, age, id0, id1) = project_uninit!(bob => {
/// name => first,
/// name => last,
/// age,
/// id => 0,
/// id => 1,
/// });
/// ```
///
/// # Example
/// ```
/// use core::mem::MaybeUninit;
/// use project_uninit::project_uninit;
///
/// #[derive(PartialEq, Eq, Debug)]
/// struct Person { name: Name, age: u32, id: (usize, usize) }
/// #[derive(PartialEq, Eq, Debug)]
/// struct Name { first: &'static str, last: &'static str }
///
/// let alice = MaybeUninit::new(Person {
/// name: Name { first: "Alice", last: "Smith" },
/// age: 22,
/// id: (123, 456),
/// });
///
/// let first = project_uninit!(alice => name => first);
/// assert_eq!(unsafe { first.assume_init() }, "Alice");
///
/// let (last, age, id1) = project_uninit!(alice => {
/// name => last,
/// age,
/// id => 1,
/// });
///
/// assert_eq!(unsafe { last.assume_init() }, "Smith");
/// assert_eq!(unsafe { age.assume_init() }, 22);
/// assert_eq!(unsafe { id1.assume_init() }, 456);
/// ```
///
) => ;
// project a single field
=> ;
}
/// Obtain `&mut MaybeUninit<_>` references to fields of a struct wrapped in `MaybeUninit<_>`.
///
/// This statically ensures that multiple references to the same value are not returned.
///
/// This must be used in an `unsafe` block or function when accessing fields of unions.
///
/// ## Syntax
/// ```
/// # use core::mem::MaybeUninit;
/// # use project_uninit::project_uninit_mut;
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Person { name: Name, age: u32, id: (usize, usize) }
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Name { first: &'static str, last: &'static str }
/// # let mut bob = MaybeUninit::new(Person {
/// # name: Name { first: "Bob1", last: "Jones1" },
/// # age: 34, id: (111, 222),
/// # });
/// // Access a single field:
/// let age: &mut MaybeUninit<u32> = project_uninit_mut!(bob => age);
///
/// // Access multiple fields:
/// let (age, id): (&mut MaybeUninit<_>, &mut MaybeUninit<_>) = project_uninit_mut!(
/// bob => { age, id }
/// );
///
/// // Access fields of fields:
/// let first: &mut MaybeUninit<&str> = project_uninit_mut!(bob => name => first);
///
/// // Access fields of tuples (also works for tuple structs):
/// let id0: &mut MaybeUninit<usize> = project_uninit_mut!(bob => id => 0);
///
/// // Access multiple fields, including nested fields:
/// let (first, last, age, id0, id1) = project_uninit_mut!(bob => {
/// name => first,
/// name => last,
/// age,
/// id => 0,
/// id => 1,
/// });
/// ```
///
/// # Example
/// ```
/// use core::mem::MaybeUninit;
/// use project_uninit::project_uninit_mut;
///
/// #[derive(PartialEq, Eq, Debug)]
/// struct Person { name: Name, age: u32, id: (usize, usize) }
/// #[derive(PartialEq, Eq, Debug)]
/// struct Name { first: &'static str, last: &'static str }
///
/// let mut alice = MaybeUninit::<Person>::uninit();
///
/// let first = project_uninit_mut!(alice => name => first);
/// *first = MaybeUninit::new("Alice");
///
/// let (last, age, id0, id1) = project_uninit_mut!(alice => {
/// name => last,
/// age,
/// id => 0,
/// id => 1,
/// });
///
/// *last = MaybeUninit::new("Smith");
/// *age = MaybeUninit::new(22);
/// *id0 = MaybeUninit::new(123);
/// *id1 = MaybeUninit::new(456);
///
/// assert_eq!(unsafe { alice.assume_init() }, Person {
/// name: Name { first: "Alice", last: "Smith" },
/// age: 22,
/// id: (123, 456),
/// });
/// ```
///
) => ;
// project a single field
=> ;
}
/// **Unsafe:** Given a `*const` pointer to a struct, obtain `*const` pointers to one or more of its fields.
///
/// This does **not** statically check whether multiple pointers to the same data are returned.
/// This must be used in an `unsafe` block or function.
///
/// ## Usage
/// ```
/// # use core::mem::MaybeUninit;
/// # use project_uninit::project_ptr;
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Person { name: Name, age: u32, id: (usize, usize) }
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Name { first: &'static str, last: &'static str }
///
/// let bob = Person {
/// name: Name { first: "Bob", last: "Jones" },
/// age: 35,
/// id: (111, 222),
/// };
/// let bob_ptr: *const Person = &bob;
///
/// unsafe {
/// // Pointer to a single field:
/// let age: *const u32 = project_ptr!(bob_ptr => age);
/// assert_eq!(*age, 35);
///
/// // Pointers to multiple fields:
/// let (first, name, id0): (*const &str, *const Name, *const usize) = project_ptr!(
/// bob_ptr => { name => first, name, id => 0 }
/// );
/// assert_eq!(*first, "Bob");
/// assert_eq!(*name, Name { first: "Bob", last: "Jones" });
/// assert_eq!(*id0, 111);
/// }
///
/// ```
) => ;
// project a single field
=> ;
}
/// **Unsafe:** Given a `*mut` pointer to a struct, obtain `*mut` pointers to one or more of its fields.
///
/// This does **not** statically check whether multiple pointers to the same data are returned.
/// This must be used in an `unsafe` block or function.
///
/// ## Usage
/// ```
/// # use core::mem::MaybeUninit;
/// # use project_uninit::project_ptr_mut;
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Person { name: Name, age: u32, id: (usize, usize) }
/// # #[derive(PartialEq, Eq, Debug)]
/// # struct Name { first: &'static str, last: &'static str }
///
/// let mut bob = Person {
/// name: Name { first: "Bob", last: "Jones" },
/// age: 35,
/// id: (111, 222),
/// };
/// let bob_ptr: *mut Person = &mut bob;
///
/// unsafe {
/// // Pointer to a single field:
/// let age: *mut u32 = project_ptr_mut!(bob_ptr => age);
/// *age = 36;
///
/// // Pointers to multiple fields:
/// let (first, last, id0): (*mut &str, *mut &str, *mut usize) = project_ptr_mut!(
/// bob_ptr => { name => first, name => last, id => 0 }
/// );
/// *first = "Robert";
/// *last = "Johns";
/// *id0 = 444;
/// assert_eq!(bob, Person {
/// name: Name { first: "Robert", last: "Johns" },
/// age: 36,
/// id: (444, 222),
/// });
/// }
/// ```
) => ;
// project a single field
=> ;
}
///```compile_fail
/// use project_uninit::project_uninit_mut;
/// use core::mem::MaybeUninit;
/// struct Foo { a: i32, b: u32 }
/// let mut x = MaybeUninit::<Foo>::uninit();
/// let (a, b, a2) = project_uninit_mut!(x => { a, b, a });
///```
///```compile_fail
/// use project_uninit::{project_uninit, project_uninit_mut};
/// use core::mem::MaybeUninit;
/// struct Foo { a: i32, b: u32 }
/// let mut x = MaybeUninit::<Foo>::uninit();
/// let a = project_uninit!(x => a);
/// let (a2, b) = project_uninit_mut!(x => { a, b });
/// let aa = a;
///```