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