wrapper_lite/lib.rs
1#![doc = include_str!("../README.md")]
2#![no_std]
3
4#[macro_export]
5/// Helper macro for integrating with the new-type idiom in Rust.
6///
7/// ## Basic usage
8///
9/// Just write the wrapper type definition using Rust's struct syntax, and then
10/// add `#[wrapper(...)]` attributes to generate implementations of traits for
11/// the wrapper type automatically.
12///
13/// ```rust
14/// wrapper_lite::wrapper!(
15/// #[wrapper(AsRef)]
16/// #[derive(Debug, Clone, Copy)]
17/// pub struct Foobar([u8; 128]);
18/// );
19///
20/// wrapper_lite::wrapper!(
21/// #[wrapper(AsRef)]
22/// #[derive(Debug, Clone, Copy)]
23/// pub struct Barfoo {
24/// inner: [u8; 128],
25/// }
26/// );
27/// ```
28///
29/// This macro supports both tuple struct syntax and braced struct syntax. For
30/// braced struct syntax, the first field will be treated as the "inner" field.
31///
32/// ### Associated constructor method: `from_inner`
33///
34/// This macro will generate a const associated constructor method `from_inner`
35/// when applicable, which takes an instance of the inner type and returns an
36/// instance of the wrapper struct.
37///
38/// ```rust,compile_fail
39/// mod inner {
40/// wrapper_lite::wrapper!(
41/// pub(crate) struct Foobar<'a>(&'a str);
42/// );
43/// }
44///
45/// const _: () = {
46/// let _ = inner::FooBar::from_inner("Hello");
47/// };
48/// ```
49///
50/// ```rust
51/// mod inner {
52/// wrapper_lite::wrapper!(
53/// pub(crate) struct Foobar<'a>(pub(crate) &'a str);
54/// );
55/// }
56///
57/// const _: () = {
58/// let _ = inner::Foobar::from_inner("Hello");
59/// };
60/// ```
61///
62/// ### The `#[default(...)]` attribute
63///
64/// When there are other fields in the wrapper struct, it is not possible to
65/// construct an instance of the wrapper struct from the inner value alone.
66///
67/// ```rust,compile_fail
68/// wrapper_lite::wrapper!(
69/// struct Foobar {
70/// inner_field: &'static str,
71/// other_field1: u8,
72/// other_field2: u8,
73/// }
74/// );
75///
76/// const _: () = {
77/// // ❌ Oh, we don't know what value to use for `other_field`!
78/// let _ = Foobar::from_inner("Hello");
79/// };
80/// ```
81///
82/// The `#[default(...)]` attribute allows specifying default values for those
83/// other fields to work around this issue.
84///
85/// ```rust
86/// wrapper_lite::wrapper!(
87/// struct Foobar {
88/// inner_field: &'static str,
89/// #[default(6)]
90/// other_field1: u8,
91/// #[default(8)]
92/// other_field2: u8,
93/// }
94/// );
95///
96/// const _: () = {
97/// let foobar = Foobar::from_inner("Hello");
98/// assert!(foobar.other_field1 == 6);
99/// assert!(foobar.other_field2 == 8);
100/// };
101/// ```
102///
103/// Both the method `from_inner` and the [`From`] trait implementation
104/// benefit from this.
105///
106/// ### The `#[wrapper(...)]` attribute
107///
108/// Instructs the macro to generate implementations of some commonly used traits
109/// for the wrapper struct.
110///
111/// #### `#[wrapper(Debug)]`
112///
113/// Implements trait [`Debug`] for the wrapper struct if the inner type
114/// implements it.
115///
116/// ```rust
117/// wrapper_lite::wrapper!(
118/// #[wrapper(Debug)]
119/// struct Wrapper<'a>(&'a str);
120/// );
121///
122/// assert_eq!(format!("{:?}", Wrapper { inner: "168" }), "\"168\"");
123/// ```
124///
125/// #### `#[wrapper(DebugName)]`
126///
127/// Implements trait [`Debug`] for the wrapper struct, but only prints the name
128/// of the wrapper struct.
129///
130/// ```rust
131/// wrapper_lite::wrapper!(
132/// #[wrapper(DebugName)]
133/// struct Wrapper<'a>(&'a str);
134/// );
135///
136/// assert_eq!(format!("{:?}", Wrapper { inner: "168" }), "Wrapper");
137/// ```
138///
139/// #### `#[wrapper(Display)]`
140///
141/// Implements trait [`Display`] for the wrapper struct if the inner type
142/// implements it.
143///
144/// ```rust
145/// wrapper_lite::wrapper!(
146/// #[wrapper(Display)]
147/// struct Wrapper<'a>(&'a str);
148/// );
149///
150/// assert_eq!(format!("{}", Wrapper { inner: "168" }), "168");
151/// ```
152///
153/// #### `#[wrapper(DisplayName)]`
154///
155/// Implements trait [`Display`] for the wrapper struct, but only prints the
156/// name of the wrapper struct.
157///
158/// ```rust
159/// wrapper_lite::wrapper!(
160/// #[wrapper(DisplayName)]
161/// struct Wrapper<'a>(&'a str);
162/// );
163///
164/// assert_eq!(format!("{}", Wrapper { inner: "168" }), "Wrapper");
165/// ```
166///
167/// #### `#[wrapper(AsRef)]` / `#[wrapper(AsMut)]` / `#[wrapper(Borrow)]` / `#[wrapper(BorrowMut)]` / `#[wrapper(Deref)]` / `#[wrapper(DerefMut)]`
168///
169/// Implements trait [`AsRef`] / [`AsMut`] / [`Borrow`] / [`BorrowMut`] /
170/// [`Deref`] / [`DerefMut`] for the wrapper struct.
171///
172/// The *target* type can be omitted (defaults to the type of the inner field,
173/// denoted as `U`), or it can be explicitly specified (denoted as `T`),
174/// provided that `&U` can be automatically coerced to `&T` via deref coercions.
175///
176/// ```rust
177/// wrapper_lite::wrapper!(
178/// #[wrapper(AsRef)]
179/// #[wrapper(AsRef<[u8]>)]
180/// struct Wrapper([u8; 42]);
181/// );
182///
183/// const fn assert_as_ref<T, U>()
184/// where
185/// T: AsRef<U>,
186/// U: ?Sized,
187/// {
188/// }
189///
190/// assert_as_ref::<Wrapper, [u8; 42]>();
191/// assert_as_ref::<Wrapper, [u8]>();
192/// ```
193///
194/// ```rust
195/// wrapper_lite::wrapper!(
196/// #[wrapper(AsMut)]
197/// #[wrapper(AsMut<[u8]>)]
198/// struct Wrapper([u8; 42]);
199/// );
200///
201/// const fn assert_as_mut<T, U>()
202/// where
203/// T: AsMut<U>,
204/// U: ?Sized,
205/// {
206/// }
207///
208/// assert_as_mut::<Wrapper, [u8; 42]>();
209/// assert_as_mut::<Wrapper, [u8]>();
210/// ```
211///
212/// ```rust
213/// use core::borrow::Borrow;
214///
215/// wrapper_lite::wrapper!(
216/// #[wrapper(Borrow)]
217/// #[wrapper(Borrow<[u8]>)]
218/// struct Wrapper([u8; 42]);
219/// );
220///
221/// const fn assert_borrow<T, U>()
222/// where
223/// T: Borrow<U>,
224/// U: ?Sized,
225/// {
226/// }
227///
228/// assert_borrow::<Wrapper, [u8; 42]>();
229/// assert_borrow::<Wrapper, [u8]>();
230/// ```
231///
232/// ```rust
233/// use core::borrow::{Borrow, BorrowMut};
234///
235/// wrapper_lite::wrapper!(
236/// #[wrapper(BorrowMut)]
237/// #[wrapper(BorrowMut<[u8]>)]
238/// struct Wrapper([u8; 42]);
239/// );
240///
241/// const fn assert_borrow<T, U>()
242/// where
243/// T: Borrow<U>,
244/// U: ?Sized,
245/// {
246/// }
247///
248/// const fn assert_borrow_mut<T, U>()
249/// where
250/// T: BorrowMut<U>,
251/// U: ?Sized,
252/// {
253/// }
254///
255/// assert_borrow::<Wrapper, [u8; 42]>();
256/// assert_borrow_mut::<Wrapper, [u8; 42]>();
257/// assert_borrow::<Wrapper, [u8]>();
258/// assert_borrow_mut::<Wrapper, [u8]>();
259/// ```
260///
261/// ```rust
262/// use core::ops::Deref;
263///
264/// wrapper_lite::wrapper!(
265/// #[wrapper(Deref)]
266/// struct WrapperA([u8; 42]);
267/// );
268///
269/// wrapper_lite::wrapper!(
270/// #[wrapper(Deref<[u8]>)]
271/// struct WrapperB([u8; 42]);
272/// );
273///
274/// const fn assert_deref<T, U>()
275/// where
276/// T: Deref<Target = U>,
277/// U: ?Sized,
278/// {
279/// }
280///
281/// assert_deref::<WrapperA, [u8; 42]>();
282/// assert_deref::<WrapperB, [u8]>();
283/// ```
284///
285/// ```rust
286/// use core::ops::{Deref, DerefMut};
287///
288/// wrapper_lite::wrapper!(
289/// #[wrapper(DerefMut)]
290/// struct WrapperA([u8; 42]);
291/// );
292///
293/// wrapper_lite::wrapper!(
294/// #[wrapper(DerefMut<[u8]>)]
295/// struct WrapperB([u8; 42]);
296/// );
297///
298/// const fn assert_deref<T, U>()
299/// where
300/// T: Deref<Target = U>,
301/// U: ?Sized,
302/// {
303/// }
304///
305/// const fn assert_deref_mut<T, U>()
306/// where
307/// T: DerefMut<Target = U>,
308/// U: ?Sized,
309/// {
310/// }
311///
312/// assert_deref::<WrapperA, [u8; 42]>();
313/// assert_deref_mut::<WrapperA, [u8; 42]>();
314/// assert_deref::<WrapperB, [u8]>();
315/// assert_deref_mut::<WrapperB, [u8]>();
316/// ```
317///
318/// For `#[wrapper(AsRef)]` or `#[wrapper(AsMut)]`, we will also generate an
319/// associated method `as_inner` or `as_inner_mut` with the same visibility as
320/// the wrapper struct. Since `&mut` in const was stabilized only as of Rust
321/// 1.83, and this crate's MSRV is 1.56, the `as_inner_mut` method is not const
322/// by default.
323///
324/// We add limited support for syntax `#[wrapper([const] Trait)]` as a temporary
325/// workaround, and currently the only supported trait is `AsMut`. Once const
326/// traits implementations are stabilized, this syntax may be further expanded
327/// in its usage, but that seems a long way off.
328///
329/// ```rust
330/// wrapper_lite::wrapper!(
331/// #[wrapper([const] AsMut)]
332/// pub struct Wrapper<P>(pub(crate) P);
333/// );
334///
335/// const fn test_const_as_inner_mut<P>(wrapper: &mut Wrapper<P>) {
336/// let _ = wrapper.as_inner_mut();
337/// }
338/// ```
339///
340/// When `#[wrapper(BorrowMut)]` or `#[wrapper(DerefMut)]` is specified, the
341/// corresponding [`Borrow`] or [`Deref`] implementation is generated
342/// automatically. Combining them with an explicit `#[wrapper(Borrow)]` or
343/// `#[wrapper(Deref)]` therefore results in a duplicate implementation error:
344///
345/// ```rust,compile_fail
346/// wrapper_lite::wrapper!(
347/// #[wrapper(Borrow)]
348/// #[wrapper(BorrowMut)]
349/// pub struct Wrapper<P>(pub(crate) P);
350/// );
351/// ```
352///
353/// ```rust,compile_fail
354/// wrapper_lite::wrapper!(
355/// #[wrapper(Deref)]
356/// #[wrapper(DerefMut)]
357/// pub struct Wrapper<P>(pub(crate) P);
358/// );
359/// ```
360///
361/// #### `#[wrapper(From)]`
362///
363/// Implements trait [`From`] for the wrapper struct, allowing it to be
364/// constructed from the inner type.
365///
366/// ```rust
367/// wrapper_lite::wrapper!(
368/// #[wrapper(From)]
369/// pub struct Wrapper<P>(pub(crate) P);
370/// );
371///
372/// const fn assert_from<T, U>()
373/// where
374/// T: From<U>,
375/// {
376/// }
377///
378/// const _: () = {
379/// assert_from::<Wrapper<()>, ()>();
380/// };
381/// ```
382///
383/// ## Advanced usage
384///
385/// ### `#[repr(align(cache))]`
386///
387/// You can use `#[repr(align(cache))]` to pad and align the wrapper type to the
388/// length of a cache line. This is useful for performance optimization in
389/// certain scenarios.
390///
391/// ```rust
392/// use core::mem::align_of;
393///
394/// wrapper_lite::wrapper!(
395/// #[repr(align(cache))]
396/// pub struct Foobar(u8);
397/// );
398///
399/// #[cfg(target_arch = "x86_64")]
400/// const _: () = {
401/// assert!(align_of::<Foobar>() == 128);
402/// };
403/// ```
404///
405/// ## Notes
406///
407/// ### `wrapper! { ... }` vs. `wrapper!( ... );`
408///
409/// The parenthesized form is preferred, as it's better supported by rustfmt.
410///
411/// ### Limitation on specifying multiple traits in a single `#[wrapper(...)]`
412///
413/// Like `derive` attribute, only tokens that `$($trait:ident),+` can represent
414/// can be recognized as a list of traits to be implemented:
415///
416/// ```rust,compile_fail
417/// wrapper_lite::wrapper!(
418/// // 👇 Oh, `AsRef<[T]>` is not an ident
419/// #[wrapper(Debug, From, AsRef, AsRef<[T]>)]
420/// pub struct Foobar<T>(pub [T; 16]);
421/// );
422/// ```
423///
424/// Instead:
425///
426/// ```rust
427/// wrapper_lite::wrapper!(
428/// #[wrapper(Debug, From, AsRef)]
429/// // 👇 Move to a dedicated `wrapper` attr.
430/// #[wrapper(AsRef<[T]>)]
431/// pub struct Foobar<T>(pub [T; 16]);
432/// );
433/// ```
434///
435/// ### Limitation on generic parameters
436///
437/// Due to the inherent limitations of declarative macros, some complex syntax
438/// related to generic parameters is not supported.
439///
440/// - A trait bound that lexically contains multiple tokens, like `?Sized`, is
441/// **not** supported:
442///
443/// ```rust,compile_fail
444/// wrapper_lite::wrapper!(
445/// // 👇 '?' + 'Sized', 2 tokens
446/// pub struct HolyMolyCow<'a, P: ?Sized>(&'a P);
447/// );
448/// ```
449///
450/// - A trailing comma after the last generic parameter is **not** supported:
451///
452/// ```rust,compile_fail
453/// wrapper_lite::wrapper!(
454/// // 👇
455/// pub struct HolyMolyCow<'a, P: Default,>(&'a P);
456/// );
457/// ```
458///
459/// - `where` clauses are **not** supported:
460///
461/// ```rust,compile_fail
462/// wrapper_lite::wrapper!(
463/// pub struct HolyMolyCow<'a, P>(&'a P)
464/// where
465/// P: ?Sized + Clone;
466/// );
467/// ```
468///
469/// The `#[bound(...)]` attribute serves as a partial workaround for the
470/// limitations above, at the cost of lacking rustfmt support for the content
471/// specified in `#[bound(...)]`.
472///
473/// ```rust
474/// wrapper_lite::wrapper!(
475/// #[bound(P: ?Sized + Clone)]
476/// pub struct HolyMolyCow<'a, P = ::core::marker::PhantomData<()>>(&'a P);
477/// );
478/// ```
479///
480/// ### Limitation on conditional compilation attribute `#[cfg(...)]`
481///
482/// This macro can correctly handle conditional compilation attribute
483/// `#[cfg(...)]` at the struct level. However, due to the inherent complexity
484/// limitations of declarative macros, `#[cfg(...)]` on struct fields are not
485/// supported now. This will affect scenarios involving the construction of
486/// wrapper types, such as implementing the `From` trait, etc.
487///
488/// [`Debug`]: core::fmt::Debug
489/// [`Display`]: core::fmt::Display
490/// [`AsRef`]: core::convert::AsRef
491/// [`AsMut`]: core::convert::AsMut
492/// [`Borrow`]: core::borrow::Borrow
493/// [`BorrowMut`]: core::borrow::BorrowMut
494/// [`Deref`]: core::ops::Deref
495/// [`DerefMut`]: core::ops::DerefMut
496macro_rules! wrapper {
497 ($($tt:tt)*) => {
498 $crate::__wrapper!($($tt)*);
499 };
500}
501
502#[macro_export]
503#[doc(hidden)]
504macro_rules! __wrapper {
505 // ! Filters out `#[cfg(...)]` attributes.
506 (
507 @parse
508 [
509 $($cfg:tt)*
510 ]
511 [
512 $($repr:tt)*
513 ]
514 [
515 $($bound:tt)*
516 ]
517 [
518 $($wrapper:tt)*
519 ]
520 [
521 $($meta:tt)*
522 ]
523 #[cfg($($c:tt)+)]
524 $($rest:tt)*
525 ) => {
526 $crate::__wrapper! {
527 @parse
528 [
529 $($cfg)*
530 #[cfg($($c)+)]
531 ]
532 [
533 $($repr)*
534 ]
535 [
536 $($bound)*
537 ]
538 [
539 $($wrapper)*
540 ]
541 [
542 $($meta)*
543 ]
544 $($rest)*
545 }
546 };
547
548 // ! Filters out `#[repr(...)]` attributes.
549 (
550 @parse
551 [
552 $($cfg:tt)*
553 ]
554 [
555 $($repr:tt)*
556 ]
557 [
558 $($bound:tt)*
559 ]
560 [
561 $($wrapper:tt)*
562 ]
563 [
564 $($meta:tt)*
565 ]
566 #[repr($($r:tt)+)]
567 $($rest:tt)*
568 ) => {
569 $crate::__wrapper! {
570 @parse
571 [
572 $($cfg)*
573 ]
574 [
575 $($repr)*
576 #[repr($($r)+)]
577 ]
578 [
579 $($bound)*
580 ]
581 [
582 $($wrapper)*
583 ]
584 [
585 $($meta)*
586 ]
587 $($rest)*
588 }
589 };
590
591 // ! Filters out `#[bound(...)]` attributes.
592 (
593 @parse
594 [
595 $($cfg:tt)*
596 ]
597 [
598 $($repr:tt)*
599 ]
600 [
601 $($bound:tt)*
602 ]
603 [
604 $($wrapper:tt)*
605 ]
606 [
607 $($meta:tt)*
608 ]
609 #[bound($($b:tt)+)]
610 $($rest:tt)*
611 ) => {
612 $crate::__wrapper! {
613 @parse
614 [
615 $($cfg)*
616 ]
617 [
618 $($repr)*
619 ]
620 [
621 $($b)+,
622 $($bound)*
623 ]
624 [
625 $($wrapper)*
626 ]
627 [
628 $($meta)*
629 ]
630 $($rest)*
631 }
632 };
633
634 // ! Filters out `#[wrapper(...)]` attributes.
635 (
636 @parse
637 [
638 $($cfg:tt)*
639 ]
640 [
641 $($repr:tt)*
642 ]
643 [
644 $($bound:tt)*
645 ]
646 [
647 $($wrapper:tt)*
648 ]
649 [
650 $($meta:tt)*
651 ]
652 #[wrapper($($ident:ident),+)]
653 $($rest:tt)*
654 ) => {
655 $crate::__wrapper! {
656 @parse
657 [
658 $($cfg)*
659 ]
660 [
661 $($repr)*
662 ]
663 [
664 $($bound)*
665 ]
666 [
667 $($wrapper)*
668 $(
669 #[wrapper($ident)]
670 )+
671 ]
672 [
673 $($meta)*
674 ]
675 $($rest)*
676 }
677 };
678
679 // ! Filters out `#[wrapper(...)]` attributes.
680 (
681 @parse
682 [
683 $($cfg:tt)*
684 ]
685 [
686 $($repr:tt)*
687 ]
688 [
689 $($bound:tt)*
690 ]
691 [
692 $($wrapper:tt)*
693 ]
694 [
695 $($meta:tt)*
696 ]
697 #[wrapper($($g:tt)+)]
698 $($rest:tt)*
699 ) => {
700 $crate::__wrapper! {
701 @parse
702 [
703 $($cfg)*
704 ]
705 [
706 $($repr)*
707 ]
708 [
709 $($bound)*
710 ]
711 [
712 $($wrapper)*
713 #[wrapper($($g)+)]
714 ]
715 [
716 $($meta)*
717 ]
718 $($rest)*
719 }
720 };
721
722 // ! Collects the other attributes.
723 (
724 @parse
725 [
726 $($cfg:tt)*
727 ]
728 [
729 $($repr:tt)*
730 ]
731 [
732 $($bound:tt)*
733 ]
734 [
735 $($wrapper:tt)*
736 ]
737 [
738 $($meta:tt)*
739 ]
740 #[$($m:tt)+]
741 $($rest:tt)*
742 ) => {
743 $crate::__wrapper! {
744 @parse
745 [
746 $($cfg)*
747 ]
748 [
749 $($repr)*
750 ]
751 [
752 $($bound)*
753 ]
754 [
755 $($wrapper)*
756 ]
757 [
758 $($meta)*
759 #[$($m)+]
760 ]
761 $($rest)*
762 }
763 };
764
765 // ! All attributes have been parsed.
766 (
767 @parse
768 [
769 $($cfg:tt)*
770 ]
771 [
772 $($repr:tt)*
773 ]
774 [
775 $($bound:tt)*
776 ]
777 [
778 $($wrapper:tt)*
779 ]
780 [
781 $($meta:tt)*
782 ]
783 $($rest:tt)*
784 ) => {
785 $crate::__wrapper! {
786 @emit
787 [
788 $($cfg)*
789 ]
790 [
791 $($repr)*
792 ]
793 [
794 $($bound)*
795 ]
796 [
797 $($wrapper)*
798 ]
799 [
800 $($meta)*
801 ]
802 $($rest)*
803 }
804 };
805
806 // ! Format: `$vis struct $name($ivis $ity);`
807 (
808 @emit
809 [
810 $($cfg:tt)*
811 ]
812 [
813 $($repr:tt)*
814 ]
815 [
816 $($bound:tt)*
817 ]
818 [
819 $($wrapper:tt)*
820 ]
821 [
822 $($meta:tt)*
823 ]
824 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? ($ivis:vis $ity:ty $(,)?);
825 ) => {
826 $crate::__wrapper! {
827 @emit
828 [
829 $($cfg)*
830 ]
831 [
832 $($repr)*
833 ]
834 [
835 $($bound)*
836 ]
837 [
838 $($wrapper)*
839 ]
840 [
841 $($meta)*
842 ]
843 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
844 /// The inner value being wrapped in this wrapper type.
845 $ivis inner: $ity,
846 }
847 }
848 };
849
850 // ! Format: `$vis struct $name { $inner: $ity, ... }`
851 (
852 @emit
853 [
854 $($cfg:tt)*
855 ]
856 [
857 $($repr:tt)*
858 ]
859 [
860 $($bound:tt)*
861 ]
862 [
863 $($wrapper:tt)*
864 ]
865 [
866 $($meta:tt)*
867 ]
868 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
869 $(#[$($imeta:tt)*])*
870 $ivis:vis $inner:ident: $ity:ty
871 $(
872 ,
873 #[default($($fdefault:tt)*)]
874 $(#[$($fmeta:tt)*])*
875 $fvis:vis $field:ident: $fty:ty
876 )*
877 $(,)?
878 }
879 ) => {
880 $crate::__wrapper_type! {
881 $($cfg)*
882 $($repr)*
883 $($meta)*
884 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)?
885 where
886 $($bound)*
887 {
888 $(#[$($imeta)*])*
889 $ivis $inner: $ity,
890 $(
891 $(#[$($fmeta)*])*
892 $fvis $field: $fty,
893 )*
894 }
895 }
896
897 $($cfg)*
898 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
899 where
900 $($bound)*
901 {
902 #[allow(dead_code)]
903 #[inline]
904 #[doc = concat!("Wraps the given inner value with [`", stringify!($name), "`]")]
905 #[doc = ""]
906 #[doc = "The other fields (if exist) will each be set to the specified default value."]
907 $ivis const fn from_inner($inner: $ity) -> Self {
908 Self {
909 $inner,
910 $(
911 $field: $($fdefault)*,
912 )*
913 }
914 }
915 }
916
917 $crate::__wrapper! {
918 @gen
919 [
920 $($cfg)*
921 ]
922 [
923 $($bound)*
924 ]
925 [
926 $($wrapper)*
927 ]
928 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
929 $(#[$($imeta)*])*
930 $ivis $inner: $ity,
931 $(
932 #[default($($fdefault)*)]
933 $(#[$($fmeta)*])*
934 $fvis $field: $fty,
935 )*
936 }
937 }
938 };
939
940 (
941 @emit
942 [
943 $($cfg:tt)*
944 ]
945 [
946 $($repr:tt)*
947 ]
948 [
949 $($bound:tt)*
950 ]
951 [
952 $($wrapper:tt)*
953 ]
954 [
955 $($meta:tt)*
956 ]
957 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
958 $(#[$($imeta:tt)*])*
959 $ivis:vis $inner:ident: $ity:ty
960 $(
961 ,
962 $(#[$($fmeta:tt)*])*
963 $fvis:vis $field:ident: $fty:ty
964 )*
965 $(,)?
966 }
967 ) => {
968 $crate::__wrapper_type! {
969 $($cfg)*
970 $($repr)*
971 $($meta)*
972 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)?
973 where
974 $($bound)*
975 {
976 $(#[$($imeta)*])*
977 $ivis $inner: $ity,
978 $(
979 $(#[$($fmeta)*])*
980 $fvis $field: $fty,
981 )*
982 }
983 }
984
985 $crate::__wrapper! {
986 @gen
987 [
988 $($cfg)*
989 ]
990 [
991 $($bound)*
992 ]
993 [
994 $($wrapper)*
995 ]
996 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
997 $(#[$($imeta)*])*
998 $ivis $inner: $ity,
999 $(
1000 $(#[$($fmeta)*])*
1001 $fvis $field: $fty,
1002 )*
1003 }
1004 }
1005 };
1006
1007 // ! Generating implementation.
1008
1009 (
1010 @gen
1011 [
1012 $($cfg:tt)*
1013 ]
1014 [
1015 $($bound:tt)*
1016 ]
1017 [
1018 ]
1019 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1020 $(#[$($imeta:tt)*])*
1021 $ivis:vis $inner:ident: $ity:ty
1022 $(
1023 ,
1024 $(#[$($fmeta:tt)*])*
1025 $fvis:vis $field:ident: $fty:ty
1026 )*
1027 $(,)?
1028 }
1029 ) => {};
1030
1031 (
1032 @gen
1033 [
1034 $($cfg:tt)*
1035 ]
1036 [
1037 $($bound:tt)*
1038 ]
1039 [
1040 #[wrapper(Debug)]
1041 $($wrapper:tt)*
1042 ]
1043 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1044 $(#[$($imeta:tt)*])*
1045 $ivis:vis $inner:ident: $ity:ty
1046 $(
1047 ,
1048 $(#[$($fmeta:tt)*])*
1049 $fvis:vis $field:ident: $fty:ty
1050 )*
1051 $(,)?
1052 }
1053 ) => {
1054 $($cfg)*
1055 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
1056 where
1057 $ity: ::core::fmt::Debug,
1058 $($bound)*
1059 {
1060 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
1061 ::core::fmt::Debug::fmt(&self.$inner, f)
1062 }
1063 }
1064
1065 $crate::__wrapper! {
1066 @gen
1067 [
1068 $($cfg)*
1069 ]
1070 [
1071 $($bound)*
1072 ]
1073 [
1074 $($wrapper)*
1075 ]
1076 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1077 $(#[$($imeta)*])*
1078 $ivis $inner: $ity,
1079 $(
1080 $(#[$($fmeta)*])*
1081 $fvis $field: $fty,
1082 )*
1083 }
1084 }
1085 };
1086
1087 (
1088 @gen
1089 [
1090 $($cfg:tt)*
1091 ]
1092 [
1093 $($bound:tt)*
1094 ]
1095 [
1096 #[wrapper(DebugName)]
1097 $($wrapper:tt)*
1098 ]
1099 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1100 $(#[$($imeta:tt)*])*
1101 $ivis:vis $inner:ident: $ity:ty
1102 $(
1103 ,
1104 $(#[$($fmeta:tt)*])*
1105 $fvis:vis $field:ident: $fty:ty
1106 )*
1107 $(,)?
1108 }
1109 ) => {
1110 $($cfg)*
1111 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
1112 where
1113 $($bound)*
1114 {
1115 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
1116 f.debug_struct(stringify!($name)).finish()
1117 }
1118 }
1119
1120 $crate::__wrapper! {
1121 @gen
1122 [
1123 $($cfg)*
1124 ]
1125 [
1126 $($bound)*
1127 ]
1128 [
1129 $($wrapper)*
1130 ]
1131 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1132 $(#[$($imeta)*])*
1133 $ivis $inner: $ity,
1134 $(
1135 $(#[$($fmeta)*])*
1136 $fvis $field: $fty,
1137 )*
1138 }
1139 }
1140 };
1141
1142 (
1143 @gen
1144 [
1145 $($cfg:tt)*
1146 ]
1147 [
1148 $($bound:tt)*
1149 ]
1150 [
1151 #[wrapper(Display)]
1152 $($wrapper:tt)*
1153 ]
1154 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1155 $(#[$($imeta:tt)*])*
1156 $ivis:vis $inner:ident: $ity:ty
1157 $(
1158 ,
1159 $(#[$($fmeta:tt)*])*
1160 $fvis:vis $field:ident: $fty:ty
1161 )*
1162 $(,)?
1163 }
1164 ) => {
1165 $($cfg)*
1166 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::fmt::Display for $name$(<$($lt),+>)?
1167 where
1168 $ity: ::core::fmt::Display,
1169 $($bound)*
1170 {
1171 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
1172 ::core::fmt::Display::fmt(&self.$inner, f)
1173 }
1174 }
1175
1176 $crate::__wrapper! {
1177 @gen
1178 [
1179 $($cfg)*
1180 ]
1181 [
1182 $($bound)*
1183 ]
1184 [
1185 $($wrapper)*
1186 ]
1187 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1188 $(#[$($imeta)*])*
1189 $ivis $inner: $ity,
1190 $(
1191 $(#[$($fmeta)*])*
1192 $fvis $field: $fty,
1193 )*
1194 }
1195 }
1196 };
1197
1198 (
1199 @gen
1200 [
1201 $($cfg:tt)*
1202 ]
1203 [
1204 $($bound:tt)*
1205 ]
1206 [
1207 #[wrapper(DisplayName)]
1208 $($wrapper:tt)*
1209 ]
1210 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1211 $(#[$($imeta:tt)*])*
1212 $ivis:vis $inner:ident: $ity:ty
1213 $(
1214 ,
1215 $(#[$($fmeta:tt)*])*
1216 $fvis:vis $field:ident: $fty:ty
1217 )*
1218 $(,)?
1219 }
1220 ) => {
1221 $($cfg)*
1222 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::fmt::Display for $name$(<$($lt),+>)?
1223 where
1224 $ity: ::core::fmt::Display,
1225 $($bound)*
1226 {
1227 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
1228 ::core::fmt::Display::fmt(stringify!($name), f)
1229 }
1230 }
1231
1232 $crate::__wrapper! {
1233 @gen
1234 [
1235 $($cfg)*
1236 ]
1237 [
1238 $($bound)*
1239 ]
1240 [
1241 $($wrapper)*
1242 ]
1243 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1244 $(#[$($imeta)*])*
1245 $ivis $inner: $ity,
1246 $(
1247 $(#[$($fmeta)*])*
1248 $fvis $field: $fty,
1249 )*
1250 }
1251 }
1252 };
1253
1254 (
1255 @gen
1256 [
1257 $($cfg:tt)*
1258 ]
1259 [
1260 $($bound:tt)*
1261 ]
1262 [
1263 #[wrapper(AsRef)]
1264 $($wrapper:tt)*
1265 ]
1266 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1267 $(#[$($imeta:tt)*])*
1268 $ivis:vis $inner:ident: $ity:ty
1269 $(
1270 ,
1271 $(#[$($fmeta:tt)*])*
1272 $fvis:vis $field:ident: $fty:ty
1273 )*
1274 $(,)?
1275 }
1276 ) => {
1277 $($cfg)*
1278 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
1279 where
1280 $($bound)*
1281 {
1282 #[allow(dead_code)]
1283 #[inline]
1284 #[doc = concat!("Returns a reference to the inner value of the [`", stringify!($name), "`].")]
1285 $vis const fn as_inner(&self) -> &$ity {
1286 &self.$inner
1287 }
1288 }
1289
1290 $crate::__wrapper! {
1291 @gen
1292 [
1293 $($cfg)*
1294 ]
1295 [
1296 $($bound)*
1297 ]
1298 [
1299 #[wrapper(AsRef<$ity>)]
1300 $($wrapper)*
1301 ]
1302 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1303 $(#[$($imeta)*])*
1304 $ivis $inner: $ity,
1305 $(
1306 $(#[$($fmeta)*])*
1307 $fvis $field: $fty,
1308 )*
1309 }
1310 }
1311 };
1312
1313 (
1314 @gen
1315 [
1316 $($cfg:tt)*
1317 ]
1318 [
1319 $($bound:tt)*
1320 ]
1321 [
1322 #[wrapper(AsRef<$tty:ty>)]
1323 $($wrapper:tt)*
1324 ]
1325 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1326 $(#[$($imeta:tt)*])*
1327 $ivis:vis $inner:ident: $ity:ty
1328 $(
1329 ,
1330 $(#[$($fmeta:tt)*])*
1331 $fvis:vis $field:ident: $fty:ty
1332 )*
1333 $(,)?
1334 }
1335 ) => {
1336 $($cfg)*
1337 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::AsRef<$tty> for $name$(<$($lt),+>)?
1338 where
1339 $($bound)*
1340 {
1341 fn as_ref(&self) -> &$tty {
1342 &self.$inner
1343 }
1344 }
1345
1346 $crate::__wrapper! {
1347 @gen
1348 [
1349 $($cfg)*
1350 ]
1351 [
1352 $($bound)*
1353 ]
1354 [
1355 $($wrapper)*
1356 ]
1357 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1358 $(#[$($imeta)*])*
1359 $ivis $inner: $ity,
1360 $(
1361 $(#[$($fmeta)*])*
1362 $fvis $field: $fty,
1363 )*
1364 }
1365 }
1366 };
1367
1368 (
1369 @gen
1370 [
1371 $($cfg:tt)*
1372 ]
1373 [
1374 $($bound:tt)*
1375 ]
1376 [
1377 #[wrapper($([$cst:tt])?AsMut)]
1378 $($wrapper:tt)*
1379 ]
1380 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1381 $(#[$($imeta:tt)*])*
1382 $ivis:vis $inner:ident: $ity:ty
1383 $(
1384 ,
1385 $(#[$($fmeta:tt)*])*
1386 $fvis:vis $field:ident: $fty:ty
1387 )*
1388 $(,)?
1389 }
1390 ) => {
1391 $($cfg)*
1392 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
1393 where
1394 $($bound)*
1395 {
1396 #[allow(dead_code)]
1397 #[inline]
1398 #[doc = concat!("Returns a mutable reference to the inner value of [`", stringify!($name), "`].")]
1399 $vis $($cst)? fn as_inner_mut(&mut self) -> &mut $ity {
1400 &mut self.$inner
1401 }
1402 }
1403
1404 $crate::__wrapper! {
1405 @gen
1406 [
1407 $($cfg)*
1408 ]
1409 [
1410 $($bound)*
1411 ]
1412 [
1413 #[wrapper(AsMut<$ity>)]
1414 $($wrapper)*
1415 ]
1416 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1417 $(#[$($imeta)*])*
1418 $ivis $inner: $ity,
1419 $(
1420 $(#[$($fmeta)*])*
1421 $fvis $field: $fty,
1422 )*
1423 }
1424 }
1425 };
1426
1427 (
1428 @gen
1429 [
1430 $($cfg:tt)*
1431 ]
1432 [
1433 $($bound:tt)*
1434 ]
1435 [
1436 #[wrapper(AsMut<$tty:ty>)]
1437 $($wrapper:tt)*
1438 ]
1439 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1440 $(#[$($imeta:tt)*])*
1441 $ivis:vis $inner:ident: $ity:ty
1442 $(
1443 ,
1444 $(#[$($fmeta:tt)*])*
1445 $fvis:vis $field:ident: $fty:ty
1446 )*
1447 $(,)?
1448 }
1449 ) => {
1450 $($cfg)*
1451 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::AsMut<$tty> for $name$(<$($lt),+>)?
1452 where
1453 $($bound)*
1454 {
1455 fn as_mut(&mut self) -> &mut $tty {
1456 &mut self.$inner
1457 }
1458 }
1459
1460 $crate::__wrapper! {
1461 @gen
1462 [
1463 $($cfg)*
1464 ]
1465 [
1466 $($bound)*
1467 ]
1468 [
1469 $($wrapper)*
1470 ]
1471 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1472 $(#[$($imeta)*])*
1473 $ivis $inner: $ity,
1474 $(
1475 $(#[$($fmeta)*])*
1476 $fvis $field: $fty,
1477 )*
1478 }
1479 }
1480 };
1481
1482 (
1483 @gen
1484 [
1485 $($cfg:tt)*
1486 ]
1487 [
1488 $($bound:tt)*
1489 ]
1490 [
1491 #[wrapper(Borrow)]
1492 $($wrapper:tt)*
1493 ]
1494 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1495 $(#[$($imeta:tt)*])*
1496 $ivis:vis $inner:ident: $ity:ty
1497 $(
1498 ,
1499 $(#[$($fmeta:tt)*])*
1500 $fvis:vis $field:ident: $fty:ty
1501 )*
1502 $(,)?
1503 }
1504 ) => {
1505 $crate::__wrapper! {
1506 @gen
1507 [
1508 $($cfg)*
1509 ]
1510 [
1511 $($bound)*
1512 ]
1513 [
1514 #[wrapper(Borrow<$ity>)]
1515 $($wrapper)*
1516 ]
1517 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1518 $(#[$($imeta)*])*
1519 $ivis $inner: $ity,
1520 $(
1521 $(#[$($fmeta)*])*
1522 $fvis $field: $fty,
1523 )*
1524 }
1525 }
1526 };
1527
1528 (
1529 @gen
1530 [
1531 $($cfg:tt)*
1532 ]
1533 [
1534 $($bound:tt)*
1535 ]
1536 [
1537 #[wrapper(Borrow<$tty:ty>)]
1538 $($wrapper:tt)*
1539 ]
1540 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1541 $(#[$($imeta:tt)*])*
1542 $ivis:vis $inner:ident: $ity:ty
1543 $(
1544 ,
1545 $(#[$($fmeta:tt)*])*
1546 $fvis:vis $field:ident: $fty:ty
1547 )*
1548 $(,)?
1549 }
1550 ) => {
1551 $($cfg)*
1552 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::Borrow<$tty> for $name$(<$($lt),+>)?
1553 where
1554 $($bound)*
1555 {
1556 fn borrow(&self) -> &$tty {
1557 &self.$inner
1558 }
1559 }
1560
1561 $crate::__wrapper! {
1562 @gen
1563 [
1564 $($cfg)*
1565 ]
1566 [
1567 $($bound)*
1568 ]
1569 [
1570 $($wrapper)*
1571 ]
1572 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1573 $(#[$($imeta)*])*
1574 $ivis $inner: $ity,
1575 $(
1576 $(#[$($fmeta)*])*
1577 $fvis $field: $fty,
1578 )*
1579 }
1580 }
1581 };
1582
1583 (
1584 @gen
1585 [
1586 $($cfg:tt)*
1587 ]
1588 [
1589 $($bound:tt)*
1590 ]
1591 [
1592 #[wrapper(BorrowMut)]
1593 $($wrapper:tt)*
1594 ]
1595 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1596 $(#[$($imeta:tt)*])*
1597 $ivis:vis $inner:ident: $ity:ty
1598 $(
1599 ,
1600 $(#[$($fmeta:tt)*])*
1601 $fvis:vis $field:ident: $fty:ty
1602 )*
1603 $(,)?
1604 }
1605 ) => {
1606 $crate::__wrapper! {
1607 @gen
1608 [
1609 $($cfg)*
1610 ]
1611 [
1612 $($bound)*
1613 ]
1614 [
1615 #[wrapper(BorrowMut<$ity>)]
1616 $($wrapper)*
1617 ]
1618 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1619 $(#[$($imeta)*])*
1620 $ivis $inner: $ity,
1621 $(
1622 $(#[$($fmeta)*])*
1623 $fvis $field: $fty,
1624 )*
1625 }
1626 }
1627 };
1628
1629 (
1630 @gen
1631 [
1632 $($cfg:tt)*
1633 ]
1634 [
1635 $($bound:tt)*
1636 ]
1637 [
1638 #[wrapper(BorrowMut<$tty:ty>)]
1639 $($wrapper:tt)*
1640 ]
1641 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1642 $(#[$($imeta:tt)*])*
1643 $ivis:vis $inner:ident: $ity:ty
1644 $(
1645 ,
1646 $(#[$($fmeta:tt)*])*
1647 $fvis:vis $field:ident: $fty:ty
1648 )*
1649 $(,)?
1650 }
1651 ) => {
1652 $($cfg)*
1653 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::Borrow<$tty> for $name$(<$($lt),+>)?
1654 where
1655 $($bound)*
1656 {
1657 fn borrow(&self) -> &$tty {
1658 &self.$inner
1659 }
1660 }
1661
1662 $($cfg)*
1663 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::BorrowMut<$tty> for $name$(<$($lt),+>)?
1664 where
1665 $($bound)*
1666 {
1667 fn borrow_mut(&mut self) -> &mut $tty {
1668 &mut self.$inner
1669 }
1670 }
1671
1672 $crate::__wrapper! {
1673 @gen
1674 [
1675 $($cfg)*
1676 ]
1677 [
1678 $($bound)*
1679 ]
1680 [
1681 $($wrapper)*
1682 ]
1683 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1684 $(#[$($imeta)*])*
1685 $ivis $inner: $ity,
1686 $(
1687 $(#[$($fmeta)*])*
1688 $fvis $field: $fty,
1689 )*
1690 }
1691 }
1692 };
1693
1694 (
1695 @gen
1696 [
1697 $($cfg:tt)*
1698 ]
1699 [
1700 $($bound:tt)*
1701 ]
1702 [
1703 #[wrapper(Deref)]
1704 $($wrapper:tt)*
1705 ]
1706 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1707 $(#[$($imeta:tt)*])*
1708 $ivis:vis $inner:ident: $ity:ty
1709 $(
1710 ,
1711 $(#[$($fmeta:tt)*])*
1712 $fvis:vis $field:ident: $fty:ty
1713 )*
1714 $(,)?
1715 }
1716 ) => {
1717 $crate::__wrapper! {
1718 @gen
1719 [
1720 $($cfg)*
1721 ]
1722 [
1723 $($bound)*
1724 ]
1725 [
1726 #[wrapper(Deref<$ity>)]
1727 $($wrapper)*
1728 ]
1729 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1730 $(#[$($imeta)*])*
1731 $ivis $inner: $ity,
1732 $(
1733 $(#[$($fmeta)*])*
1734 $fvis $field: $fty,
1735 )*
1736 }
1737 }
1738 };
1739
1740 (
1741 @gen
1742 [
1743 $($cfg:tt)*
1744 ]
1745 [
1746 $($bound:tt)*
1747 ]
1748 [
1749 #[wrapper(Deref<$tty:ty>)]
1750 $($wrapper:tt)*
1751 ]
1752 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1753 $(#[$($imeta:tt)*])*
1754 $ivis:vis $inner:ident: $ity:ty
1755 $(
1756 ,
1757 $(#[$($fmeta:tt)*])*
1758 $fvis:vis $field:ident: $fty:ty
1759 )*
1760 $(,)?
1761 }
1762 ) => {
1763 $($cfg)*
1764 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)?
1765 where
1766 $($bound)*
1767 {
1768 type Target = $tty;
1769
1770 fn deref(&self) -> &$tty {
1771 &self.$inner
1772 }
1773 }
1774
1775 $crate::__wrapper! {
1776 @gen
1777 [
1778 $($cfg)*
1779 ]
1780 [
1781 $($bound)*
1782 ]
1783 [
1784 $($wrapper)*
1785 ]
1786 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1787 $(#[$($imeta)*])*
1788 $ivis $inner: $ity,
1789 $(
1790 $(#[$($fmeta)*])*
1791 $fvis $field: $fty,
1792 )*
1793 }
1794 }
1795 };
1796
1797 (
1798 @gen
1799 [
1800 $($cfg:tt)*
1801 ]
1802 [
1803 $($bound:tt)*
1804 ]
1805 [
1806 #[wrapper(DerefMut)]
1807 $($wrapper:tt)*
1808 ]
1809 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1810 $(#[$($imeta:tt)*])*
1811 $ivis:vis $inner:ident: $ity:ty
1812 $(
1813 ,
1814 $(#[$($fmeta:tt)*])*
1815 $fvis:vis $field:ident: $fty:ty
1816 )*
1817 $(,)?
1818 }
1819 ) => {
1820 $crate::__wrapper! {
1821 @gen
1822 [
1823 $($cfg)*
1824 ]
1825 [
1826 $($bound)*
1827 ]
1828 [
1829 #[wrapper(DerefMut<$ity>)]
1830 $($wrapper)*
1831 ]
1832 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1833 $(#[$($imeta)*])*
1834 $ivis $inner: $ity,
1835 $(
1836 $(#[$($fmeta)*])*
1837 $fvis $field: $fty,
1838 )*
1839 }
1840 }
1841 };
1842
1843 (
1844 @gen
1845 [
1846 $($cfg:tt)*
1847 ]
1848 [
1849 $($bound:tt)*
1850 ]
1851 [
1852 #[wrapper(DerefMut<$tty:ty>)]
1853 $($wrapper:tt)*
1854 ]
1855 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1856 $(#[$($imeta:tt)*])*
1857 $ivis:vis $inner:ident: $ity:ty
1858 $(
1859 ,
1860 $(#[$($fmeta:tt)*])*
1861 $fvis:vis $field:ident: $fty:ty
1862 )*
1863 $(,)?
1864 }
1865 ) => {
1866 $($cfg)*
1867 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)?
1868 where
1869 $($bound)*
1870 {
1871 type Target = $tty;
1872
1873 fn deref(&self) -> &$tty {
1874 &self.$inner
1875 }
1876 }
1877
1878 $($cfg)*
1879 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::DerefMut for $name$(<$($lt),+>)?
1880 where
1881 $($bound)*
1882 {
1883 fn deref_mut(&mut self) -> &mut $tty {
1884 &mut self.$inner
1885 }
1886 }
1887
1888 $crate::__wrapper! {
1889 @gen
1890 [
1891 $($cfg)*
1892 ]
1893 [
1894 $($bound)*
1895 ]
1896 [
1897 $($wrapper)*
1898 ]
1899 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1900 $(#[$($imeta)*])*
1901 $ivis $inner: $ity,
1902 $(
1903 $(#[$($fmeta)*])*
1904 $fvis $field: $fty,
1905 )*
1906 }
1907 }
1908 };
1909
1910 (
1911 @gen
1912 [
1913 $($cfg:tt)*
1914 ]
1915 [
1916 $($bound:tt)*
1917 ]
1918 [
1919 #[wrapper(From)]
1920 $($wrapper:tt)*
1921 ]
1922 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1923 $(#[$($imeta:tt)*])*
1924 $ivis:vis $inner:ident: $ity:ty
1925 $(
1926 ,
1927 #[default($($fdefault:tt)*)]
1928 $(#[$($fmeta:tt)*])*
1929 $fvis:vis $field:ident: $fty:ty
1930 )*
1931 $(,)?
1932 }
1933 ) => {
1934 $($cfg)*
1935 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::From<$ity> for $name$(<$($lt),+>)?
1936 where
1937 $($bound)*
1938 {
1939 fn from($inner: $ity) -> Self {
1940 Self {
1941 $inner,
1942 $(
1943 $field: $($fdefault)*,
1944 )*
1945 }
1946 }
1947 }
1948
1949 $crate::__wrapper! {
1950 @gen
1951 [
1952 $($cfg)*
1953 ]
1954 [
1955 $($bound)*
1956 ]
1957 [
1958 $($wrapper)*
1959 ]
1960 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1961 $(#[$($imeta)*])*
1962 $ivis $inner: $ity,
1963 $(
1964 #[default($($fdefault)*)]
1965 $(#[$($fmeta)*])*
1966 $fvis $field: $fty,
1967 )*
1968 }
1969 }
1970 };
1971
1972 (
1973 @gen
1974 [
1975 $($cfg:tt)*
1976 ]
1977 [
1978 $($bound:tt)*
1979 ]
1980 [
1981 #[wrapper(From)]
1982 $($wrapper:tt)*
1983 ]
1984 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1985 $(#[$($imeta:tt)*])*
1986 $ivis:vis $inner:ident: $ity:ty
1987 $(
1988 ,
1989 $(#[$($fmeta:tt)*])*
1990 $fvis:vis $field:ident: $fty:ty
1991 )*
1992 $(,)?
1993 }
1994 ) => {
1995 compile_error!(
1996 "Fields except `inner` should be each provided a default value for implementing `From`."
1997 );
1998 };
1999
2000 // ! Catch-all for unsupported syntax.
2001 (@ $($other:tt)*) => {
2002 compile_error!(
2003 "Incorrect or unsupported syntax, please refer to `wrapper!`'s documentation."
2004 );
2005 };
2006
2007 // ! Entry point.
2008 ($($input:tt)*) => {
2009 $crate::__wrapper! {
2010 @parse [] [] [] [] [] $($input)*
2011 }
2012 };
2013}
2014
2015#[macro_export]
2016#[doc(hidden)]
2017macro_rules! __wrapper_type {
2018 (
2019 #[repr(align(cache))]
2020 $($tt:tt)*
2021 ) => {
2022 // Starting from Intel's Sandy Bridge, spatial prefetcher is now pulling pairs of 64-byte cache
2023 // lines at a time, so we have to align to 128 bytes rather than 64.
2024 //
2025 // Sources:
2026 // - https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
2027 // - https://github.com/facebook/folly/blob/1b5288e6eea6df074758f877c849b6e73bbb9fbb/folly/lang/Align.h#L107
2028 //
2029 // aarch64/arm64ec's big.LITTLE architecture has asymmetric cores and "big" cores have 128-byte cache line size.
2030 //
2031 // Sources:
2032 // - https://www.mono-project.com/news/2016/09/12/arm64-icache/
2033 //
2034 // powerpc64 has 128-byte cache line size.
2035 //
2036 // Sources:
2037 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_ppc64x.go#L9
2038 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/powerpc/include/asm/cache.h#L26
2039 #[cfg_attr(
2040 any(
2041 target_arch = "x86_64",
2042 target_arch = "aarch64",
2043 target_arch = "arm64ec",
2044 target_arch = "powerpc64",
2045 ),
2046 repr(align(128))
2047 )]
2048 // arm, mips, mips64, sparc, and hexagon have 32-byte cache line size.
2049 //
2050 // Sources:
2051 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_arm.go#L7
2052 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips.go#L7
2053 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mipsle.go#L7
2054 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips64x.go#L9
2055 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L17
2056 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/hexagon/include/asm/cache.h#L12
2057 #[cfg_attr(
2058 any(
2059 target_arch = "arm",
2060 target_arch = "mips",
2061 target_arch = "mips32r6",
2062 target_arch = "mips64",
2063 target_arch = "mips64r6",
2064 target_arch = "sparc",
2065 target_arch = "hexagon",
2066 ),
2067 repr(align(32))
2068 )]
2069 // m68k has 16-byte cache line size.
2070 //
2071 // Sources:
2072 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/m68k/include/asm/cache.h#L9
2073 #[cfg_attr(target_arch = "m68k", repr(align(16)))]
2074 // s390x has 256-byte cache line size.
2075 //
2076 // Sources:
2077 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_s390x.go#L7
2078 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/s390/include/asm/cache.h#L13
2079 #[cfg_attr(target_arch = "s390x", repr(align(256)))]
2080 // x86, wasm, riscv, and sparc64 have 64-byte cache line size.
2081 //
2082 // Sources:
2083 // - https://github.com/golang/go/blob/dda2991c2ea0c5914714469c4defc2562a907230/src/internal/cpu/cpu_x86.go#L9
2084 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_wasm.go#L7
2085 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/riscv/include/asm/cache.h#L10
2086 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L19
2087 //
2088 // All others are assumed to have 64-byte cache line size.
2089 #[cfg_attr(
2090 not(any(
2091 target_arch = "x86_64",
2092 target_arch = "aarch64",
2093 target_arch = "arm64ec",
2094 target_arch = "powerpc64",
2095 target_arch = "arm",
2096 target_arch = "mips",
2097 target_arch = "mips32r6",
2098 target_arch = "mips64",
2099 target_arch = "mips64r6",
2100 target_arch = "sparc",
2101 target_arch = "hexagon",
2102 target_arch = "m68k",
2103 target_arch = "s390x",
2104 )),
2105 repr(align(64))
2106 )]
2107 $($tt)*
2108 };
2109
2110 ($($tt:tt)*) => {
2111 $($tt)*
2112 };
2113}