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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#[macro_use]
mod delegate_structural;
#[macro_use]
mod list;
#[macro_use]
mod field_paths;
#[macro_use]
mod tstr_macros;
#[macro_use]
mod impl_struct;
#[macro_use]
mod make_struct;
#[macro_use]
mod structural_alias;
#[macro_use]
mod enum_derivation;
#[macro_use]
mod switch;
#[macro_use]
mod type_level_internal;
/// Implements an infallible getter
#[doc(hidden)]
#[macro_export]
macro_rules! _private_impl_getter{
(
$(unsafe)?
impl[$($typarams:tt)*]
GetField <$field_name:tt : $field_ty:ty,$name_param:ty>
for $self_:ty
$( where[$($where_:tt)*] )?
)=>{
impl<$($typarams)*> $crate::FieldType<$name_param> for $self_
$( where $($where_)* )?
{
type Ty=$field_ty;
}
impl<$($typarams)*> $crate::GetField<$name_param> for $self_
$( where $($where_)* )?
{
fn get_field_(&self,_:$name_param)->&Self::Ty{
&self.$field_name
}
}
};
(
unsafe impl[$($typarams:tt)*]
GetFieldMut <$field_name:tt : $field_ty:ty,$name_param:ty>
for $self_:ty
$( where[$($where_:tt)*] )?
)=>{
$crate::_private_impl_getter!{
impl[$($typarams)*] GetField<$field_name:$field_ty,$name_param>
for $self_
$( where[$($where_)*] )?
}
unsafe impl<$($typarams)*> $crate::GetFieldMut<$name_param> for $self_
$( where $($where_)* )?
{
fn get_field_mut_(&mut self,_:$name_param)->&mut Self::Ty{
&mut self.$field_name
}
$crate::z_unsafe_impl_get_field_raw_mut!{
Self,
field_tstr=$field_name,
name_generic=$name_param,
}
}
};
(
$(unsafe)?
impl[$($typarams:tt)*]
IntoField <$field_name:tt : $field_ty:ty,$name_param:ty>
for $self_:ty
$( where[$($where_:tt)*] )?
)=>{
$crate::_private_impl_getter!{
impl[$($typarams)*]
GetField<$field_name:$field_ty,$name_param>
for $self_
$( where[$($where_)*] )?
}
impl<$($typarams)*> $crate::IntoField<$name_param> for $self_
$( where $($where_)* )?
{
fn into_field_(self,_:$name_param)->Self::Ty{
self.$field_name
}
$crate::z_impl_box_into_field_method!{field_tstr=$name_param}
}
};
(
unsafe impl[$($typarams:tt)*]
IntoFieldMut <$field_name:tt : $field_ty:ty,$name_param:ty>
for $self_:ty
$( where[$($where_:tt)*] )?
)=>{
$crate::_private_impl_getter!{
unsafe impl[$($typarams)*]
GetFieldMut<$field_name:$field_ty,$name_param>
for $self_
$( where[$($where_)*] )?
}
impl<$($typarams)*> $crate::IntoField<$name_param> for $self_
$( where $($where_)* )?
{
fn into_field_(
self,
_:$name_param,
)->Self::Ty{
self.$field_name
}
$crate::z_impl_box_into_field_method!{field_tstr=$name_param}
}
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! default_if {
(
$(#[$attr:meta])*
cfg($($cfg_attr:tt)*)
$($default_impl:tt)*
) => (
#[cfg($($cfg_attr)*)]
$(#[$attr])*
default $($default_impl)*
#[cfg(not($($cfg_attr)*))]
$(#[$attr])*
$($default_impl)*
)
}
/// For semi-manual implementors of the
/// [GetFieldMut](./field/trait.GetFieldMut.html)
/// trait for structs.
///
/// This implements the [GetFieldMut::get_field_raw_mut]
/// by returning a mutable pointer to a field,
/// and [GetFieldMut::get_field_raw_mut_fn] by returning
/// `get_field_raw_mut` as a function pointer.
///
/// # Safety
///
/// This is an unsafe macro,
/// because GetFieldMut requires no impl to borrow the same field mutably as any other,
/// otherwise this would cause undefined behavior because it would
/// create multiple mutable borrows to the same field.
///
/// # Example
///
/// For an example where this macro is used,
/// you can look at the
/// [manual implementation example of the GetFieldMut trait
/// ](./field/trait.GetFieldMut.html#manual-implementation-example)
#[macro_export]
macro_rules! z_unsafe_impl_get_field_raw_mut {
(
$Self:ident,
field_tstr=$field_name:tt,
name_generic=$name_param:ty,
) => {
unsafe fn get_field_raw_mut(
this: *mut (),
_: $name_param,
) -> *mut $crate::GetFieldType<$Self, $name_param> {
&mut (*(this as *mut $Self)).$field_name
as *mut $crate::GetFieldType<$Self, $name_param>
}
fn get_field_raw_mut_fn(
&self,
) -> $crate::field::GetFieldRawMutFn<$name_param, $crate::GetFieldType<$Self, $name_param>>
{
<$Self as $crate::field::GetFieldMut<$name_param>>::get_field_raw_mut
}
};
}
/// For creating a raw pointer of an enum field,as either `Some(NonNull<_>)` or `None`.
///
/// # Safety
///
/// The `$pointer` must be raw pointer to a valid(and fully initialized)
/// instance of the `$enum` type.
///
/// # Example
///
/// For an example of using this macro look at
/// [the manual implementation example for `GetVariantFieldMut`
/// ](./field/trait.GetVariantFieldMut.html#manual-impl-example)
#[macro_export]
macro_rules! z_raw_borrow_enum_field {
(
$pointer:expr,
$enum:ident::$variant:ident.$field:tt : $field_ty:ty
) => {{
match *$pointer {
$enum::$variant {
$field: ref mut field,
..
} => {
if false {
let _: *mut $field_ty = field;
}
let ptr = field as *mut $field_ty;
Some($crate::pmr::NonNull::new_unchecked(ptr))
}
#[allow(unreachable_patterns)]
_ => None,
}
}};
}
/// Implements the [`get_vfield_raw_mut_fn`] and [`get_vfield_raw_mut_unchecked_fn`]
/// methods from the [`GetVariantFieldMut`] trait.
///
/// # Safety
///
/// The `$Self` argument must be the `Self` type in the impl block.
///
/// # Example
///
/// For an example of using this macro look at
/// [the manual implementation example
/// ](./field/trait.GetVariantFieldMut.html#manual-impl-example)
/// for [`GetVariantFieldMut`]
///
/// [`GetVariantFieldMut`]: ./field/trait.GetVariantFieldMut.html
/// [`get_vfield_raw_mut_fn`]:
/// ./field/trait.GetVariantFieldMut.html#tymethod.get_vfield_raw_mut_fn
/// [`get_vfield_raw_mut_unchecked_fn`]:
/// ./field/trait.GetVariantFieldMut.html#tymethod.get_vfield_raw_mut_unchecked_fn
#[macro_export]
macro_rules! z_unsafe_impl_get_vfield_raw_mut_fn {
(
Self=$Self:ty,
variant_tstr=$variant_name:ty,
field_tstr=$field_name:ty,
field_type=$field_ty:ty,
) => {
fn get_vfield_raw_mut_fn(
&self,
) -> $crate::pmr::GetVFieldRawMutFn<$variant_name, $field_name, $field_ty> {
<$Self as
$crate::pmr::GetVariantFieldMut<$variant_name,$field_name>
>::get_vfield_raw_mut_
}
fn get_vfield_raw_mut_unchecked_fn(
&self,
) -> $crate::pmr::GetFieldRawMutFn<$field_name, $field_ty> {
<$Self as
$crate::pmr::GetVariantFieldMut<$variant_name, $field_name>
>::get_vfield_raw_mut_unchecked
}
};
}
/// For use in manual implementations of the [`IntoField`] trait.
///
/// Implements the [`IntoField::box_into_field_`] method
/// by delegating to the [`IntoField::into_field_`] method.
///
/// # Features
///
/// If you disable the default feature,and don't enable the "alloc" feature,
/// you must implement the [`IntoField::box_into_field_`] method using this macro,
/// since any other crate that enables structural's "alloc" feature
/// would then require every impl of [`IntoField`] to define the [`box_into_field_`] method.
///
/// # Example
///
/// For an example of using this macro look at
/// [implementation example for IntoField
/// ](./field/trait.IntoField.html#manual-implementation-example)
///
/// [`IntoField`]: ./field/trait.IntoField.html
/// [`IntoField::box_into_field_`]: ./field/trait.IntoField.html#tymethod.box_into_field_
/// [`box_into_field_`]: ./field/trait.IntoField.html#tymethod.box_into_field_
/// [`IntoField::into_field_`]: ./field/trait.IntoField.html#tymethod.into_field_
#[macro_export]
macro_rules! z_impl_box_into_field_method {
(
field_tstr= $field_name:ty
$( , field_type= $field_ty:ty )? $(,)*
) => {
$crate::z_impl_box_into_field_method_inner!{
field_tstr= $field_name
$( , field_type= $field_ty )?
}
};
}
#[macro_export]
#[doc(hidden)]
#[cfg(not(feature = "alloc"))]
macro_rules! z_impl_box_into_field_method_inner {
($($anything:tt)*) => {};
}
#[macro_export]
#[doc(hidden)]
#[cfg(feature = "alloc")]
macro_rules! z_impl_box_into_field_method_inner {
(field_tstr= $field_name:ty) => {
$crate::z_impl_box_into_field_method_inner! {
field_tstr= $field_name,
field_type= $crate::GetFieldType<Self,$field_name>,
}
};
(field_tstr= $field_name:ty, field_type= $field_ty:ty $(,)*) => {
#[inline(always)]
fn box_into_field_(self: $crate::pmr::Box<Self>, name: $field_name) -> $field_ty {
<Self as $crate::IntoField<$field_name>>::into_field_(*self, name)
}
};
}
/// For use in manual implementations of the [`IntoVariantField`] trait.
///
/// Implements the [`IntoVariantField::box_into_vfield_`] method
/// by delegating to the [`IntoVariantField::into_vfield_`] method.
///
/// # Features
///
/// If you disable the default feature,and don't enable the "alloc" feature,
/// you must implement the [`IntoVariantField::box_into_vfield_`] method using this macro,
/// since if other crates enable the "alloc" feature,
/// every `IntoVariantField` impl will be required to define the `box_ìnto_vfield_` method,
///
/// # Example
///
/// For an example of using this macro look at
/// [the implementation example for IntoVariantField
/// ](./field/trait.IntoVariantField.html#manual-impl-example)
///
/// [`IntoVariantField`]:
/// ./field/trait.IntoVariantField.html
///
/// [`IntoVariantField::box_into_vfield_`]:
/// ./field/trait.IntoVariantField.html#tymethod.box_into_vfield_
///
/// [`IntoVariantField::into_vfield_`]:
/// ./field/trait.IntoVariantField.html#tymethod.into_vfield_
#[macro_export]
macro_rules! z_impl_box_into_variant_field_method {
(
variant_tstr= $variant_name:ty,
field_tstr= $field_name:ty,
field_type=$field_ty:ty $(,)*
) => {
$crate::z_impl_box_into_variant_field_method_inner! {
variant_tstr= $variant_name,
field_tstr= $field_name,
field_type=$field_ty
}
};
}
#[doc(hidden)]
#[macro_export]
#[cfg(not(feature = "alloc"))]
macro_rules! z_impl_box_into_variant_field_method_inner {
($($anything:tt)*) => {};
}
#[doc(hidden)]
#[macro_export]
#[cfg(feature = "alloc")]
macro_rules! z_impl_box_into_variant_field_method_inner {
(
variant_tstr= $variant_name:ty,
field_tstr= $field_name:ty,
field_type=$field_ty:ty $(,)*
) => {
fn box_into_vfield_(
self: $crate::alloc::boxed::Box<Self>,
vname: $variant_name,
fname: $field_name,
) -> Option<$field_ty> {
<Self as $crate::IntoVariantField<$variant_name, $field_name>>::into_vfield_(
*self, vname, fname,
)
}
};
}
// Implements the Structural traits
#[doc(hidden)]
#[macro_export]
macro_rules! _private_impl_structural{
(
$(#[doc=$docs:literal])*
impl[$($typarams:tt)*] Structural for $self_:ty
where[$($where_:tt)*]
{
field_names=[$(
(
$field_name:tt,
$name_param_str:expr,
),
)*]
}
)=>{
$(#[doc=$docs])*
impl<$($typarams)*> $crate::Structural for $self_
where $($where_)*
{}
};
(
$(#[doc=$docs:literal])*
impl[$($typarams:tt)*] Structural for $self_:ty
where[$($where_:tt)*]
{
variants=[ $( $variant:ident ),* $(,)*]
}
)=>{
$(#[doc=$docs])*
impl<$($typarams)*> $crate::Structural for $self_
where $($where_)*
{}
};
}
// Implements the Structural and accessor traits for a struct
#[doc(hidden)]
#[macro_export]
macro_rules! _private_impl_getters_for_derive_struct{
(
$(#[doc=$docs:literal])*
impl $typarams:tt $self_:ty
where $where_preds:tt
{
$((
$getter_trait:ident<
$field_name:tt : $field_ty:ty,
$name_param_ty:ty,
$name_param_str:expr,
>
))*
}
)=>{
$crate::_private_impl_structural!{
$(#[doc=$docs])*
impl $typarams Structural for $self_
where $where_preds
{
field_names=[
$(
(
$field_name,
$name_param_str,
),
)*
]
}
}
$(
$crate::_private_impl_getter!{
unsafe impl $typarams
$getter_trait<$field_name : $field_ty,$name_param_ty>
for $self_
where $where_preds
}
)*
}
}
/// Asserts that the `$left` bounds are the same as the `$right` bounds
#[cfg(test)]
macro_rules! assert_equal_bounds {
(
trait $trait_:ident $([$($trait_params:tt)*])? ,
( $($left:tt)* ),
( $($right:tt)* )$(,)?
$( where[ $($where_preds:tt)* ] )?
) => (
trait $trait_< $($($trait_params)*)? >: $($left)*
where
$($($where_preds)*)?
{
const DUMMY:()=();
fn foo<_T>()
where
_T: ?Sized+$($left)*,
$($($where_preds)*)?;
}
impl<$($($trait_params)*)? _This> $trait_<$($($trait_params)*)?> for _This
where
_This: ?Sized+$($right)*,
$($($where_preds)*)?
{
fn foo<_T>()
where
_T:?Sized+$($right)*,
$($($where_preds)*)?
{}
}
)
}
#[doc(hidden)]
#[macro_export]
macro_rules! try_fe {
( $expr:expr ) => {
match $expr {
Ok(x) => x,
Err(e) => return Err($crate::field::IntoFieldErr::into_field_err(e)),
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! map_of {
( $expr:expr ) => {
match $expr {
Ok(x) => Ok(x),
Err(_) => Err($crate::field::FailedAccess),
}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! ok_or_of {
( $expr:expr ) => {
match $expr {
Some(x) => Ok(x),
None => Err($crate::field::FailedAccess),
}
};
}
/// Using this to test implemented traits.
#[cfg(test)]
macro_rules! declare_querying_trait {
(
trait $trait_name:ident $([$($params:tt)*])?
$( implements[ $($supertraits:tt)* ] )?
$( where[ $($where_:tt)* ] )?
fn $impls_fn:ident;
) => (
trait $trait_name<$($($params)*)?>:Sized{
type Impls:crate::pmr::Boolean;
fn $impls_fn(self)->Self::Impls{
<Self::Impls as crate::pmr::MarkerType>::MTVAL
}
}
impl<$($($params)*)? __This> $trait_name<$($($params)*)?>
for crate::pmr::PhantomData<__This>
where
$( __This:$($supertraits)*, )?
$( $($where_)* )?
{
type Impls=crate::pmr::True;
}
impl<$($($params)*)? __This> $trait_name<$($($params)*)?>
for &'_ crate::pmr::PhantomData<__This>
{
type Impls=crate::pmr::False;
}
)
}