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 $($bound)*
1225 {
1226 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
1227 ::core::fmt::Display::fmt(stringify!($name), f)
1228 }
1229 }
1230
1231 $crate::__wrapper! {
1232 @gen
1233 [
1234 $($cfg)*
1235 ]
1236 [
1237 $($bound)*
1238 ]
1239 [
1240 $($wrapper)*
1241 ]
1242 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1243 $(#[$($imeta)*])*
1244 $ivis $inner: $ity,
1245 $(
1246 $(#[$($fmeta)*])*
1247 $fvis $field: $fty,
1248 )*
1249 }
1250 }
1251 };
1252
1253 (
1254 @gen
1255 [
1256 $($cfg:tt)*
1257 ]
1258 [
1259 $($bound:tt)*
1260 ]
1261 [
1262 #[wrapper(AsRef)]
1263 $($wrapper:tt)*
1264 ]
1265 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1266 $(#[$($imeta:tt)*])*
1267 $ivis:vis $inner:ident: $ity:ty
1268 $(
1269 ,
1270 $(#[$($fmeta:tt)*])*
1271 $fvis:vis $field:ident: $fty:ty
1272 )*
1273 $(,)?
1274 }
1275 ) => {
1276 $($cfg)*
1277 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
1278 where
1279 $($bound)*
1280 {
1281 #[allow(dead_code)]
1282 #[inline]
1283 #[doc = concat!("Returns a reference to the inner value of the [`", stringify!($name), "`].")]
1284 $vis const fn as_inner(&self) -> &$ity {
1285 &self.$inner
1286 }
1287 }
1288
1289 $crate::__wrapper! {
1290 @gen
1291 [
1292 $($cfg)*
1293 ]
1294 [
1295 $($bound)*
1296 ]
1297 [
1298 #[wrapper(AsRef<$ity>)]
1299 $($wrapper)*
1300 ]
1301 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1302 $(#[$($imeta)*])*
1303 $ivis $inner: $ity,
1304 $(
1305 $(#[$($fmeta)*])*
1306 $fvis $field: $fty,
1307 )*
1308 }
1309 }
1310 };
1311
1312 (
1313 @gen
1314 [
1315 $($cfg:tt)*
1316 ]
1317 [
1318 $($bound:tt)*
1319 ]
1320 [
1321 #[wrapper(AsRef<$tty:ty>)]
1322 $($wrapper:tt)*
1323 ]
1324 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1325 $(#[$($imeta:tt)*])*
1326 $ivis:vis $inner:ident: $ity:ty
1327 $(
1328 ,
1329 $(#[$($fmeta:tt)*])*
1330 $fvis:vis $field:ident: $fty:ty
1331 )*
1332 $(,)?
1333 }
1334 ) => {
1335 $($cfg)*
1336 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::AsRef<$tty> for $name$(<$($lt),+>)?
1337 where
1338 $($bound)*
1339 {
1340 fn as_ref(&self) -> &$tty {
1341 &self.$inner
1342 }
1343 }
1344
1345 $crate::__wrapper! {
1346 @gen
1347 [
1348 $($cfg)*
1349 ]
1350 [
1351 $($bound)*
1352 ]
1353 [
1354 $($wrapper)*
1355 ]
1356 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1357 $(#[$($imeta)*])*
1358 $ivis $inner: $ity,
1359 $(
1360 $(#[$($fmeta)*])*
1361 $fvis $field: $fty,
1362 )*
1363 }
1364 }
1365 };
1366
1367 (
1368 @gen
1369 [
1370 $($cfg:tt)*
1371 ]
1372 [
1373 $($bound:tt)*
1374 ]
1375 [
1376 #[wrapper($([$cst:tt])?AsMut)]
1377 $($wrapper:tt)*
1378 ]
1379 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1380 $(#[$($imeta:tt)*])*
1381 $ivis:vis $inner:ident: $ity:ty
1382 $(
1383 ,
1384 $(#[$($fmeta:tt)*])*
1385 $fvis:vis $field:ident: $fty:ty
1386 )*
1387 $(,)?
1388 }
1389 ) => {
1390 $($cfg)*
1391 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
1392 where
1393 $($bound)*
1394 {
1395 #[allow(dead_code)]
1396 #[inline]
1397 #[doc = concat!("Returns a mutable reference to the inner value of [`", stringify!($name), "`].")]
1398 $vis $($cst)? fn as_inner_mut(&mut self) -> &mut $ity {
1399 &mut self.$inner
1400 }
1401 }
1402
1403 $crate::__wrapper! {
1404 @gen
1405 [
1406 $($cfg)*
1407 ]
1408 [
1409 $($bound)*
1410 ]
1411 [
1412 #[wrapper(AsMut<$ity>)]
1413 $($wrapper)*
1414 ]
1415 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1416 $(#[$($imeta)*])*
1417 $ivis $inner: $ity,
1418 $(
1419 $(#[$($fmeta)*])*
1420 $fvis $field: $fty,
1421 )*
1422 }
1423 }
1424 };
1425
1426 (
1427 @gen
1428 [
1429 $($cfg:tt)*
1430 ]
1431 [
1432 $($bound:tt)*
1433 ]
1434 [
1435 #[wrapper(AsMut<$tty:ty>)]
1436 $($wrapper:tt)*
1437 ]
1438 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1439 $(#[$($imeta:tt)*])*
1440 $ivis:vis $inner:ident: $ity:ty
1441 $(
1442 ,
1443 $(#[$($fmeta:tt)*])*
1444 $fvis:vis $field:ident: $fty:ty
1445 )*
1446 $(,)?
1447 }
1448 ) => {
1449 $($cfg)*
1450 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::AsMut<$tty> for $name$(<$($lt),+>)?
1451 where
1452 $($bound)*
1453 {
1454 fn as_mut(&mut self) -> &mut $tty {
1455 &mut self.$inner
1456 }
1457 }
1458
1459 $crate::__wrapper! {
1460 @gen
1461 [
1462 $($cfg)*
1463 ]
1464 [
1465 $($bound)*
1466 ]
1467 [
1468 $($wrapper)*
1469 ]
1470 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1471 $(#[$($imeta)*])*
1472 $ivis $inner: $ity,
1473 $(
1474 $(#[$($fmeta)*])*
1475 $fvis $field: $fty,
1476 )*
1477 }
1478 }
1479 };
1480
1481 (
1482 @gen
1483 [
1484 $($cfg:tt)*
1485 ]
1486 [
1487 $($bound:tt)*
1488 ]
1489 [
1490 #[wrapper(Borrow)]
1491 $($wrapper:tt)*
1492 ]
1493 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1494 $(#[$($imeta:tt)*])*
1495 $ivis:vis $inner:ident: $ity:ty
1496 $(
1497 ,
1498 $(#[$($fmeta:tt)*])*
1499 $fvis:vis $field:ident: $fty:ty
1500 )*
1501 $(,)?
1502 }
1503 ) => {
1504 $crate::__wrapper! {
1505 @gen
1506 [
1507 $($cfg)*
1508 ]
1509 [
1510 $($bound)*
1511 ]
1512 [
1513 #[wrapper(Borrow<$ity>)]
1514 $($wrapper)*
1515 ]
1516 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1517 $(#[$($imeta)*])*
1518 $ivis $inner: $ity,
1519 $(
1520 $(#[$($fmeta)*])*
1521 $fvis $field: $fty,
1522 )*
1523 }
1524 }
1525 };
1526
1527 (
1528 @gen
1529 [
1530 $($cfg:tt)*
1531 ]
1532 [
1533 $($bound:tt)*
1534 ]
1535 [
1536 #[wrapper(Borrow<$tty:ty>)]
1537 $($wrapper:tt)*
1538 ]
1539 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1540 $(#[$($imeta:tt)*])*
1541 $ivis:vis $inner:ident: $ity:ty
1542 $(
1543 ,
1544 $(#[$($fmeta:tt)*])*
1545 $fvis:vis $field:ident: $fty:ty
1546 )*
1547 $(,)?
1548 }
1549 ) => {
1550 $($cfg)*
1551 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::Borrow<$tty> for $name$(<$($lt),+>)?
1552 where
1553 $($bound)*
1554 {
1555 fn borrow(&self) -> &$tty {
1556 &self.$inner
1557 }
1558 }
1559
1560 $crate::__wrapper! {
1561 @gen
1562 [
1563 $($cfg)*
1564 ]
1565 [
1566 $($bound)*
1567 ]
1568 [
1569 $($wrapper)*
1570 ]
1571 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1572 $(#[$($imeta)*])*
1573 $ivis $inner: $ity,
1574 $(
1575 $(#[$($fmeta)*])*
1576 $fvis $field: $fty,
1577 )*
1578 }
1579 }
1580 };
1581
1582 (
1583 @gen
1584 [
1585 $($cfg:tt)*
1586 ]
1587 [
1588 $($bound:tt)*
1589 ]
1590 [
1591 #[wrapper(BorrowMut)]
1592 $($wrapper:tt)*
1593 ]
1594 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1595 $(#[$($imeta:tt)*])*
1596 $ivis:vis $inner:ident: $ity:ty
1597 $(
1598 ,
1599 $(#[$($fmeta:tt)*])*
1600 $fvis:vis $field:ident: $fty:ty
1601 )*
1602 $(,)?
1603 }
1604 ) => {
1605 $crate::__wrapper! {
1606 @gen
1607 [
1608 $($cfg)*
1609 ]
1610 [
1611 $($bound)*
1612 ]
1613 [
1614 #[wrapper(BorrowMut<$ity>)]
1615 $($wrapper)*
1616 ]
1617 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1618 $(#[$($imeta)*])*
1619 $ivis $inner: $ity,
1620 $(
1621 $(#[$($fmeta)*])*
1622 $fvis $field: $fty,
1623 )*
1624 }
1625 }
1626 };
1627
1628 (
1629 @gen
1630 [
1631 $($cfg:tt)*
1632 ]
1633 [
1634 $($bound:tt)*
1635 ]
1636 [
1637 #[wrapper(BorrowMut<$tty:ty>)]
1638 $($wrapper:tt)*
1639 ]
1640 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1641 $(#[$($imeta:tt)*])*
1642 $ivis:vis $inner:ident: $ity:ty
1643 $(
1644 ,
1645 $(#[$($fmeta:tt)*])*
1646 $fvis:vis $field:ident: $fty:ty
1647 )*
1648 $(,)?
1649 }
1650 ) => {
1651 $($cfg)*
1652 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::Borrow<$tty> for $name$(<$($lt),+>)?
1653 where
1654 $($bound)*
1655 {
1656 fn borrow(&self) -> &$tty {
1657 &self.$inner
1658 }
1659 }
1660
1661 $($cfg)*
1662 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::BorrowMut<$tty> for $name$(<$($lt),+>)?
1663 where
1664 $($bound)*
1665 {
1666 fn borrow_mut(&mut self) -> &mut $tty {
1667 &mut self.$inner
1668 }
1669 }
1670
1671 $crate::__wrapper! {
1672 @gen
1673 [
1674 $($cfg)*
1675 ]
1676 [
1677 $($bound)*
1678 ]
1679 [
1680 $($wrapper)*
1681 ]
1682 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1683 $(#[$($imeta)*])*
1684 $ivis $inner: $ity,
1685 $(
1686 $(#[$($fmeta)*])*
1687 $fvis $field: $fty,
1688 )*
1689 }
1690 }
1691 };
1692
1693 (
1694 @gen
1695 [
1696 $($cfg:tt)*
1697 ]
1698 [
1699 $($bound:tt)*
1700 ]
1701 [
1702 #[wrapper(Deref)]
1703 $($wrapper:tt)*
1704 ]
1705 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1706 $(#[$($imeta:tt)*])*
1707 $ivis:vis $inner:ident: $ity:ty
1708 $(
1709 ,
1710 $(#[$($fmeta:tt)*])*
1711 $fvis:vis $field:ident: $fty:ty
1712 )*
1713 $(,)?
1714 }
1715 ) => {
1716 $crate::__wrapper! {
1717 @gen
1718 [
1719 $($cfg)*
1720 ]
1721 [
1722 $($bound)*
1723 ]
1724 [
1725 #[wrapper(Deref<$ity>)]
1726 $($wrapper)*
1727 ]
1728 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1729 $(#[$($imeta)*])*
1730 $ivis $inner: $ity,
1731 $(
1732 $(#[$($fmeta)*])*
1733 $fvis $field: $fty,
1734 )*
1735 }
1736 }
1737 };
1738
1739 (
1740 @gen
1741 [
1742 $($cfg:tt)*
1743 ]
1744 [
1745 $($bound:tt)*
1746 ]
1747 [
1748 #[wrapper(Deref<$tty:ty>)]
1749 $($wrapper:tt)*
1750 ]
1751 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1752 $(#[$($imeta:tt)*])*
1753 $ivis:vis $inner:ident: $ity:ty
1754 $(
1755 ,
1756 $(#[$($fmeta:tt)*])*
1757 $fvis:vis $field:ident: $fty:ty
1758 )*
1759 $(,)?
1760 }
1761 ) => {
1762 $($cfg)*
1763 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)?
1764 where
1765 $($bound)*
1766 {
1767 type Target = $tty;
1768
1769 fn deref(&self) -> &$tty {
1770 &self.$inner
1771 }
1772 }
1773
1774 $crate::__wrapper! {
1775 @gen
1776 [
1777 $($cfg)*
1778 ]
1779 [
1780 $($bound)*
1781 ]
1782 [
1783 $($wrapper)*
1784 ]
1785 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1786 $(#[$($imeta)*])*
1787 $ivis $inner: $ity,
1788 $(
1789 $(#[$($fmeta)*])*
1790 $fvis $field: $fty,
1791 )*
1792 }
1793 }
1794 };
1795
1796 (
1797 @gen
1798 [
1799 $($cfg:tt)*
1800 ]
1801 [
1802 $($bound:tt)*
1803 ]
1804 [
1805 #[wrapper(DerefMut)]
1806 $($wrapper:tt)*
1807 ]
1808 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1809 $(#[$($imeta:tt)*])*
1810 $ivis:vis $inner:ident: $ity:ty
1811 $(
1812 ,
1813 $(#[$($fmeta:tt)*])*
1814 $fvis:vis $field:ident: $fty:ty
1815 )*
1816 $(,)?
1817 }
1818 ) => {
1819 $crate::__wrapper! {
1820 @gen
1821 [
1822 $($cfg)*
1823 ]
1824 [
1825 $($bound)*
1826 ]
1827 [
1828 #[wrapper(DerefMut<$ity>)]
1829 $($wrapper)*
1830 ]
1831 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1832 $(#[$($imeta)*])*
1833 $ivis $inner: $ity,
1834 $(
1835 $(#[$($fmeta)*])*
1836 $fvis $field: $fty,
1837 )*
1838 }
1839 }
1840 };
1841
1842 (
1843 @gen
1844 [
1845 $($cfg:tt)*
1846 ]
1847 [
1848 $($bound:tt)*
1849 ]
1850 [
1851 #[wrapper(DerefMut<$tty:ty>)]
1852 $($wrapper:tt)*
1853 ]
1854 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1855 $(#[$($imeta:tt)*])*
1856 $ivis:vis $inner:ident: $ity:ty
1857 $(
1858 ,
1859 $(#[$($fmeta:tt)*])*
1860 $fvis:vis $field:ident: $fty:ty
1861 )*
1862 $(,)?
1863 }
1864 ) => {
1865 $($cfg)*
1866 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)?
1867 where
1868 $($bound)*
1869 {
1870 type Target = $tty;
1871
1872 fn deref(&self) -> &$tty {
1873 &self.$inner
1874 }
1875 }
1876
1877 $($cfg)*
1878 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::DerefMut for $name$(<$($lt),+>)?
1879 where
1880 $($bound)*
1881 {
1882 fn deref_mut(&mut self) -> &mut $tty {
1883 &mut self.$inner
1884 }
1885 }
1886
1887 $crate::__wrapper! {
1888 @gen
1889 [
1890 $($cfg)*
1891 ]
1892 [
1893 $($bound)*
1894 ]
1895 [
1896 $($wrapper)*
1897 ]
1898 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1899 $(#[$($imeta)*])*
1900 $ivis $inner: $ity,
1901 $(
1902 $(#[$($fmeta)*])*
1903 $fvis $field: $fty,
1904 )*
1905 }
1906 }
1907 };
1908
1909 (
1910 @gen
1911 [
1912 $($cfg:tt)*
1913 ]
1914 [
1915 $($bound:tt)*
1916 ]
1917 [
1918 #[wrapper(From)]
1919 $($wrapper:tt)*
1920 ]
1921 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1922 $(#[$($imeta:tt)*])*
1923 $ivis:vis $inner:ident: $ity:ty
1924 $(
1925 ,
1926 #[default($($fdefault:tt)*)]
1927 $(#[$($fmeta:tt)*])*
1928 $fvis:vis $field:ident: $fty:ty
1929 )*
1930 $(,)?
1931 }
1932 ) => {
1933 $($cfg)*
1934 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::From<$ity> for $name$(<$($lt),+>)?
1935 where
1936 $($bound)*
1937 {
1938 fn from($inner: $ity) -> Self {
1939 Self {
1940 $inner,
1941 $(
1942 $field: $($fdefault)*,
1943 )*
1944 }
1945 }
1946 }
1947
1948 $crate::__wrapper! {
1949 @gen
1950 [
1951 $($cfg)*
1952 ]
1953 [
1954 $($bound)*
1955 ]
1956 [
1957 $($wrapper)*
1958 ]
1959 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1960 $(#[$($imeta)*])*
1961 $ivis $inner: $ity,
1962 $(
1963 #[default($($fdefault)*)]
1964 $(#[$($fmeta)*])*
1965 $fvis $field: $fty,
1966 )*
1967 }
1968 }
1969 };
1970
1971 (
1972 @gen
1973 [
1974 $($cfg:tt)*
1975 ]
1976 [
1977 $($bound:tt)*
1978 ]
1979 [
1980 #[wrapper(From)]
1981 $($wrapper:tt)*
1982 ]
1983 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1984 $(#[$($imeta:tt)*])*
1985 $ivis:vis $inner:ident: $ity:ty
1986 $(
1987 ,
1988 $(#[$($fmeta:tt)*])*
1989 $fvis:vis $field:ident: $fty:ty
1990 )*
1991 $(,)?
1992 }
1993 ) => {
1994 compile_error!(
1995 "Fields except `inner` should be each provided a default value for implementing `From`."
1996 );
1997 };
1998
1999 // ! Catch-all for unsupported syntax.
2000 (@ $($other:tt)*) => {
2001 compile_error!(
2002 "Incorrect or unsupported syntax, please refer to `wrapper!`'s documentation."
2003 );
2004 };
2005
2006 // ! Entry point.
2007 ($($input:tt)*) => {
2008 $crate::__wrapper! {
2009 @parse [] [] [] [] [] $($input)*
2010 }
2011 };
2012}
2013
2014#[macro_export]
2015#[doc(hidden)]
2016macro_rules! __wrapper_type {
2017 (
2018 #[repr(align(cache))]
2019 $($tt:tt)*
2020 ) => {
2021 // Starting from Intel's Sandy Bridge, spatial prefetcher is now pulling pairs of 64-byte cache
2022 // lines at a time, so we have to align to 128 bytes rather than 64.
2023 //
2024 // Sources:
2025 // - https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
2026 // - https://github.com/facebook/folly/blob/1b5288e6eea6df074758f877c849b6e73bbb9fbb/folly/lang/Align.h#L107
2027 //
2028 // aarch64/arm64ec's big.LITTLE architecture has asymmetric cores and "big" cores have 128-byte cache line size.
2029 //
2030 // Sources:
2031 // - https://www.mono-project.com/news/2016/09/12/arm64-icache/
2032 //
2033 // powerpc64 has 128-byte cache line size.
2034 //
2035 // Sources:
2036 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_ppc64x.go#L9
2037 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/powerpc/include/asm/cache.h#L26
2038 #[cfg_attr(
2039 any(
2040 target_arch = "x86_64",
2041 target_arch = "aarch64",
2042 target_arch = "arm64ec",
2043 target_arch = "powerpc64",
2044 ),
2045 repr(align(128))
2046 )]
2047 // arm, mips, mips64, sparc, and hexagon have 32-byte cache line size.
2048 //
2049 // Sources:
2050 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_arm.go#L7
2051 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips.go#L7
2052 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mipsle.go#L7
2053 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips64x.go#L9
2054 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L17
2055 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/hexagon/include/asm/cache.h#L12
2056 #[cfg_attr(
2057 any(
2058 target_arch = "arm",
2059 target_arch = "mips",
2060 target_arch = "mips32r6",
2061 target_arch = "mips64",
2062 target_arch = "mips64r6",
2063 target_arch = "sparc",
2064 target_arch = "hexagon",
2065 ),
2066 repr(align(32))
2067 )]
2068 // m68k has 16-byte cache line size.
2069 //
2070 // Sources:
2071 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/m68k/include/asm/cache.h#L9
2072 #[cfg_attr(target_arch = "m68k", repr(align(16)))]
2073 // s390x has 256-byte cache line size.
2074 //
2075 // Sources:
2076 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_s390x.go#L7
2077 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/s390/include/asm/cache.h#L13
2078 #[cfg_attr(target_arch = "s390x", repr(align(256)))]
2079 // x86, wasm, riscv, and sparc64 have 64-byte cache line size.
2080 //
2081 // Sources:
2082 // - https://github.com/golang/go/blob/dda2991c2ea0c5914714469c4defc2562a907230/src/internal/cpu/cpu_x86.go#L9
2083 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_wasm.go#L7
2084 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/riscv/include/asm/cache.h#L10
2085 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L19
2086 //
2087 // All others are assumed to have 64-byte cache line size.
2088 #[cfg_attr(
2089 not(any(
2090 target_arch = "x86_64",
2091 target_arch = "aarch64",
2092 target_arch = "arm64ec",
2093 target_arch = "powerpc64",
2094 target_arch = "arm",
2095 target_arch = "mips",
2096 target_arch = "mips32r6",
2097 target_arch = "mips64",
2098 target_arch = "mips64r6",
2099 target_arch = "sparc",
2100 target_arch = "hexagon",
2101 target_arch = "m68k",
2102 target_arch = "s390x",
2103 )),
2104 repr(align(64))
2105 )]
2106 $($tt)*
2107 };
2108
2109 ($($tt:tt)*) => {
2110 $($tt)*
2111 };
2112}