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/// `#[gen(...)]` attributes to automatically generate trait implementations
12/// for interoperability with the inner type:
13///
14/// ```rust
15/// wrapper_lite::wrapper!(
16/// #[gen(AsRef)]
17/// #[derive(Debug, Clone, Copy)]
18/// pub struct Foobar([u8; 128]);
19/// );
20///
21/// wrapper_lite::wrapper!(
22/// #[gen(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 `#[gen(...)]` attribute
104///
105/// Instructs the macro to automatically generate implementations of
106/// commonly used traits for the wrapper struct.
107///
108/// #### `#[gen(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/// #[gen(Debug)]
116/// struct Wrapper<'a>(&'a str);
117/// );
118///
119/// assert_eq!(format!("{:?}", Wrapper { inner: "168" }), "\"168\"");
120/// ```
121///
122/// #### `#[gen(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/// #[gen(DebugName)]
130/// struct Wrapper<'a>(&'a str);
131/// );
132///
133/// assert_eq!(format!("{:?}", Wrapper { inner: "168" }), "Wrapper");
134/// ```
135///
136/// #### `#[gen(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/// #[gen(Display)]
144/// struct Wrapper<'a>(&'a str);
145/// );
146///
147/// assert_eq!(format!("{}", Wrapper { inner: "168" }), "168");
148/// ```
149///
150/// #### `#[gen(AsRef)]` / `#[gen(AsMut)]` / `#[gen(Borrow)]` / `#[gen(BorrowMut)]` / `#[gen(Deref)]` / `#[gen(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/// #[gen(AsRef)]
161/// #[gen(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/// #[gen(AsMut)]
179/// #[gen(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/// #[gen(Borrow)]
199/// #[gen(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/// #[gen(BorrowMut)]
219/// #[gen(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/// #[gen(Deref)]
248/// struct WrapperA([u8; 42]);
249/// );
250///
251/// wrapper_lite::wrapper!(
252/// #[gen(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/// #[gen(DerefMut)]
272/// struct WrapperA([u8; 42]);
273/// );
274///
275/// wrapper_lite::wrapper!(
276/// #[gen(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 `#[gen(AsRef)]` or `#[gen(AsMut)]` is specified, we will also generate
301/// associated method `as_inner` or `as_inner_mut` that returns a (mutable)
302/// reference to the inner value. These methods will have the same visibility as
303/// the wrapper struct. Additionally, since mutable references are not allowed
304/// in constant functions before Rust 1.83, and this library's MSRV is 1.56, by
305/// default the associated method `as_inner_mut` we generate is not a const
306/// method, but we support the `#[gen([const] AsMut)]` syntax to make
307/// `as_inner_mut` a const method like `as_inner` as well.
308///
309/// ```rust
310/// wrapper_lite::wrapper!(
311/// #[gen([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 `#[gen(BorrowMut)]` or `#[gen(DerefMut)]` is specified, the
321/// corresponding [`Borrow`] or [`Deref`] implementation is generated
322/// automatically. Combining them with an explicit `#[gen(Borrow)]` or
323/// `#[gen(Deref)]` therefore results in a duplicate implementation error:
324///
325/// ```rust,compile_fail
326/// wrapper_lite::wrapper!(
327/// #[gen(Borrow)]
328/// #[gen(BorrowMut)]
329/// pub struct Wrapper<P>(pub(crate) P);
330/// );
331/// ```
332///
333/// ```rust,compile_fail
334/// wrapper_lite::wrapper!(
335/// #[gen(Deref)]
336/// #[gen(DerefMut)]
337/// pub struct Wrapper<P>(pub(crate) P);
338/// );
339/// ```
340///
341/// #### `#[gen(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/// #[gen(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/// [`Debug`]: core::fmt::Debug
438/// [`Display`]: core::fmt::Display
439/// [`AsRef`]: core::convert::AsRef
440/// [`AsMut`]: core::convert::AsMut
441/// [`Borrow`]: core::borrow::Borrow
442/// [`BorrowMut`]: core::borrow::BorrowMut
443/// [`Deref`]: core::ops::Deref
444/// [`DerefMut`]: core::ops::DerefMut
445macro_rules! wrapper {
446 ($($tt:tt)*) => {
447 $crate::__wrapper!($($tt)*);
448 };
449}
450
451#[macro_export]
452#[doc(hidden)]
453macro_rules! __wrapper {
454 // ! Filters out `#[repr(...)]` attributes.
455 (
456 @parse
457 [
458 $($repr:tt)*
459 ]
460 [
461 $($bound:tt)*
462 ]
463 [
464 $($gen:tt)*
465 ]
466 [
467 $($meta:tt)*
468 ]
469 #[repr($($r:tt)+)]
470 $($rest:tt)*
471 ) => {
472 $crate::__wrapper! {
473 @parse
474 [
475 #[repr($($r)+)]
476 $($repr)*
477 ]
478 [
479 $($bound)*
480 ]
481 [
482 $($gen)*
483 ]
484 [
485 $($meta)*
486 ]
487 $($rest)*
488 }
489 };
490
491 // ! Filters out `#[bound(...)]` attributes.
492 (
493 @parse
494 [
495 $($repr:tt)*
496 ]
497 [
498 $($bound:tt)*
499 ]
500 [
501 $($gen:tt)*
502 ]
503 [
504 $($meta:tt)*
505 ]
506 #[bound($($b:tt)+)]
507 $($rest:tt)*
508 ) => {
509 $crate::__wrapper! {
510 @parse
511 [
512 $($repr)*
513 ]
514 [
515 $($b)+,
516 $($bound)*
517 ]
518 [
519 $($gen)*
520 ]
521 [
522 $($meta)*
523 ]
524 $($rest)*
525 }
526 };
527
528 // ! Filters out `#[gen(...)]` attributes.
529 (
530 @parse
531 [
532 $($repr:tt)*
533 ]
534 [
535 $($bound:tt)*
536 ]
537 [
538 $($gen:tt)*
539 ]
540 [
541 $($meta:tt)*
542 ]
543 #[gen($($g:tt)+)]
544 $($rest:tt)*
545 ) => {
546 $crate::__wrapper! {
547 @parse
548 [
549 $($repr)*
550 ]
551 [
552 $($bound)*
553 ]
554 [
555 $($gen)*
556 #[gen($($g)+)]
557 ]
558 [
559 $($meta)*
560 ]
561 $($rest)*
562 }
563 };
564
565 // ! Filters out `#[wrapper_impl(...)]` attributes.
566 (
567 @parse
568 [
569 $($repr:tt)*
570 ]
571 [
572 $($bound:tt)*
573 ]
574 [
575 $($gen:tt)*
576 ]
577 [
578 $($meta:tt)*
579 ]
580 #[wrapper_impl($($g:tt)+)]
581 $($rest:tt)*
582 ) => {
583 $crate::__wrapper! {
584 @parse
585 [
586 $($repr)*
587 ]
588 [
589 $($bound)*
590 ]
591 [
592 $($gen)*
593 #[gen($($g)+)]
594 ]
595 [
596 $($meta)*
597 ]
598 $($rest)*
599 }
600 };
601
602 // ! Collects the other attributes.
603 (
604 @parse
605 [
606 $($repr:tt)*
607 ]
608 [
609 $($bound:tt)*
610 ]
611 [
612 $($gen:tt)*
613 ]
614 [
615 $($meta:tt)*
616 ]
617 #[$($m:tt)+]
618 $($rest:tt)*
619 ) => {
620 $crate::__wrapper! {
621 @parse
622 [
623 $($repr)*
624 ]
625 [
626 $($bound)*
627 ]
628 [
629 $($gen)*
630 ]
631 [
632 $($meta)*
633 #[$($m)+]
634 ]
635 $($rest)*
636 }
637 };
638
639 // ! All attributes have been parsed.
640 (
641 @parse
642 [
643 $($repr:tt)*
644 ]
645 [
646 $($bound:tt)*
647 ]
648 [
649 $($gen:tt)*
650 ]
651 [
652 $($meta:tt)*
653 ]
654 $($rest:tt)*
655 ) => {
656 $crate::__wrapper! {
657 @emit
658 [
659 $($repr)*
660 ]
661 [
662 $($bound)*
663 ]
664 [
665 $($gen)*
666 ]
667 [
668 $($meta)*
669 ]
670 $($rest)*
671 }
672 };
673
674 // ! Format: `$vis struct $name($ivis $ity);`
675 (
676 @emit
677 [
678 $($repr:tt)*
679 ]
680 [
681 $($bound:tt)*
682 ]
683 [
684 $($gen:tt)*
685 ]
686 [
687 $($meta:tt)*
688 ]
689 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? ($ivis:vis $ity:ty $(,)?);
690 ) => {
691 $crate::__wrapper! {
692 @emit
693 [
694 $($repr)*
695 ]
696 [
697 $($bound)*
698 ]
699 [
700 $($gen)*
701 ]
702 [
703 $($meta)*
704 ]
705 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
706 /// The inner value being wrapped in this wrapper type.
707 $ivis inner: $ity,
708 }
709 }
710 };
711
712 // ! Format: `$vis struct $name { $inner: $ity, ... }`
713 (
714 @emit
715 [
716 $($repr:tt)*
717 ]
718 [
719 $($bound:tt)*
720 ]
721 [
722 $($gen:tt)*
723 ]
724 [
725 $($meta:tt)*
726 ]
727 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
728 $(#[$($imeta:tt)*])*
729 $ivis:vis $inner:ident: $ity:ty
730 $(
731 ,
732 #[default($($fdefault:tt)*)]
733 $(#[$($fmeta:tt)*])*
734 $fvis:vis $field:ident: $fty:ty
735 )*
736 $(,)?
737 }
738 ) => {
739 $crate::__wrapper_type! {
740 $($repr)*
741 $($meta)*
742 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)?
743 where
744 $($bound)*
745 {
746 $(#[$($imeta)*])*
747 $ivis $inner: $ity,
748 $(
749 $(#[$($fmeta)*])*
750 $fvis $field: $fty,
751 )*
752 }
753 }
754
755 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
756 where
757 $($bound)*
758 {
759 #[allow(dead_code)]
760 #[inline]
761 #[doc = concat!(" Wraps the given inner value with [`", stringify!($name), "`]")]
762 #[doc = ""]
763 #[doc = " The other fields will be each set to the specified default value."]
764 $ivis const fn from_inner($inner: $ity) -> Self {
765 Self {
766 $inner,
767 $(
768 $field: $($fdefault)*,
769 )*
770 }
771 }
772 }
773
774 $crate::__wrapper! {
775 @gen
776 [
777 $($bound)*
778 ]
779 [
780 $($gen)*
781 ]
782 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
783 $(#[$($imeta)*])*
784 $ivis $inner: $ity,
785 $(
786 #[default($($fdefault)*)]
787 $(#[$($fmeta)*])*
788 $fvis $field: $fty,
789 )*
790 }
791 }
792 };
793 (
794 @emit
795 [
796 $($repr:tt)*
797 ]
798 [
799 $($bound:tt)*
800 ]
801 [
802 $($gen:tt)*
803 ]
804 [
805 $($meta:tt)*
806 ]
807 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
808 $(#[$($imeta:tt)*])*
809 $ivis:vis $inner:ident: $ity:ty
810 $(
811 ,
812 $(#[$($fmeta:tt)*])*
813 $fvis:vis $field:ident: $fty:ty
814 )*
815 $(,)?
816 }
817 ) => {
818 $crate::__wrapper_type! {
819 $($repr)*
820 $($meta)*
821 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)?
822 where
823 $($bound)*
824 {
825 $(#[$($imeta)*])*
826 $ivis $inner: $ity,
827 $(
828 $(#[$($fmeta)*])*
829 $fvis $field: $fty,
830 )*
831 }
832 }
833
834 $crate::__wrapper! {
835 @gen
836 [
837 $($bound)*
838 ]
839 [
840 $($gen)*
841 ]
842 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
843 $(#[$($imeta)*])*
844 $ivis $inner: $ity,
845 $(
846 $(#[$($fmeta)*])*
847 $fvis $field: $fty,
848 )*
849 }
850 }
851 };
852
853 // ! Generating implementation.
854 (
855 @gen
856 [
857 $($bound:tt)*
858 ]
859 [
860 ]
861 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
862 $(#[$($imeta:tt)*])*
863 $ivis:vis $inner:ident: $ity:ty
864 $(
865 ,
866 $(#[$($fmeta:tt)*])*
867 $fvis:vis $field:ident: $fty:ty
868 )*
869 $(,)?
870 }
871 ) => {};
872 (
873 @gen
874 [
875 $($bound:tt)*
876 ]
877 [
878 #[gen(Debug)]
879 $($gen:tt)*
880 ]
881 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
882 $(#[$($imeta:tt)*])*
883 $ivis:vis $inner:ident: $ity:ty
884 $(
885 ,
886 $(#[$($fmeta:tt)*])*
887 $fvis:vis $field:ident: $fty:ty
888 )*
889 $(,)?
890 }
891 ) => {
892 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
893 where
894 $ity: ::core::fmt::Debug,
895 $($bound)*
896 {
897 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
898 ::core::fmt::Debug::fmt(&self.$inner, f)
899 }
900 }
901
902 $crate::__wrapper! {
903 @gen
904 [
905 $($bound)*
906 ]
907 [
908 $($gen)*
909 ]
910 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
911 $(#[$($imeta)*])*
912 $ivis $inner: $ity,
913 $(
914 $(#[$($fmeta)*])*
915 $fvis $field: $fty,
916 )*
917 }
918 }
919 };
920 (
921 @gen
922 [
923 $($bound:tt)*
924 ]
925 [
926 #[gen(DebugName)]
927 $($gen:tt)*
928 ]
929 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
930 $(#[$($imeta:tt)*])*
931 $ivis:vis $inner:ident: $ity:ty
932 $(
933 ,
934 $(#[$($fmeta:tt)*])*
935 $fvis:vis $field:ident: $fty:ty
936 )*
937 $(,)?
938 }
939 ) => {
940 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
941 where
942 $($bound)*
943 {
944 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
945 f.debug_struct(stringify!($name)).finish()
946 }
947 }
948
949 $crate::__wrapper! {
950 @gen
951 [
952 $($bound)*
953 ]
954 [
955 $($gen)*
956 ]
957 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
958 $(#[$($imeta)*])*
959 $ivis $inner: $ity,
960 $(
961 $(#[$($fmeta)*])*
962 $fvis $field: $fty,
963 )*
964 }
965 }
966 };
967 (
968 @gen
969 [
970 $($bound:tt)*
971 ]
972 [
973 #[gen(Display)]
974 $($gen:tt)*
975 ]
976 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
977 $(#[$($imeta:tt)*])*
978 $ivis:vis $inner:ident: $ity:ty
979 $(
980 ,
981 $(#[$($fmeta:tt)*])*
982 $fvis:vis $field:ident: $fty:ty
983 )*
984 $(,)?
985 }
986 ) => {
987 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::fmt::Display for $name$(<$($lt),+>)?
988 where
989 $ity: ::core::fmt::Display,
990 $($bound)*
991 {
992 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
993 ::core::fmt::Display::fmt(&self.$inner, f)
994 }
995 }
996
997 $crate::__wrapper! {
998 @gen
999 [
1000 $($bound)*
1001 ]
1002 [
1003 $($gen)*
1004 ]
1005 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1006 $(#[$($imeta)*])*
1007 $ivis $inner: $ity,
1008 $(
1009 $(#[$($fmeta)*])*
1010 $fvis $field: $fty,
1011 )*
1012 }
1013 }
1014 };
1015 (
1016 @gen
1017 [
1018 $($bound:tt)*
1019 ]
1020 [
1021 #[gen(AsRef)]
1022 $($gen:tt)*
1023 ]
1024 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1025 $(#[$($imeta:tt)*])*
1026 $ivis:vis $inner:ident: $ity:ty
1027 $(
1028 ,
1029 $(#[$($fmeta:tt)*])*
1030 $fvis:vis $field:ident: $fty:ty
1031 )*
1032 $(,)?
1033 }
1034 ) => {
1035 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
1036 where
1037 $($bound)*
1038 {
1039 #[allow(dead_code)]
1040 #[inline]
1041 #[doc = concat!(" Returns a reference to the inner value of the [`", stringify!($name), "`].")]
1042 $vis const fn as_inner(&self) -> &$ity {
1043 &self.$inner
1044 }
1045 }
1046
1047 $crate::__wrapper! {
1048 @gen
1049 [
1050 $($bound)*
1051 ]
1052 [
1053 #[gen(AsRef<$ity>)]
1054 $($gen)*
1055 ]
1056 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1057 $(#[$($imeta)*])*
1058 $ivis $inner: $ity,
1059 $(
1060 $(#[$($fmeta)*])*
1061 $fvis $field: $fty,
1062 )*
1063 }
1064 }
1065 };
1066 (
1067 @gen
1068 [
1069 $($bound:tt)*
1070 ]
1071 [
1072 #[gen(AsRef<$tty:ty>)]
1073 $($gen:tt)*
1074 ]
1075 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1076 $(#[$($imeta:tt)*])*
1077 $ivis:vis $inner:ident: $ity:ty
1078 $(
1079 ,
1080 $(#[$($fmeta:tt)*])*
1081 $fvis:vis $field:ident: $fty:ty
1082 )*
1083 $(,)?
1084 }
1085 ) => {
1086 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::AsRef<$tty> for $name$(<$($lt),+>)?
1087 where
1088 $($bound)*
1089 {
1090 fn as_ref(&self) -> &$tty {
1091 &self.$inner
1092 }
1093 }
1094
1095 $crate::__wrapper! {
1096 @gen
1097 [
1098 $($bound)*
1099 ]
1100 [
1101 $($gen)*
1102 ]
1103 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1104 $(#[$($imeta)*])*
1105 $ivis $inner: $ity,
1106 $(
1107 $(#[$($fmeta)*])*
1108 $fvis $field: $fty,
1109 )*
1110 }
1111 }
1112 };
1113 (
1114 @gen
1115 [
1116 $($bound:tt)*
1117 ]
1118 [
1119 #[gen($([$cst:tt])?AsMut)]
1120 $($gen:tt)*
1121 ]
1122 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1123 $(#[$($imeta:tt)*])*
1124 $ivis:vis $inner:ident: $ity:ty
1125 $(
1126 ,
1127 $(#[$($fmeta:tt)*])*
1128 $fvis:vis $field:ident: $fty:ty
1129 )*
1130 $(,)?
1131 }
1132 ) => {
1133 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
1134 where
1135 $($bound)*
1136 {
1137 #[allow(dead_code)]
1138 #[inline]
1139 #[doc = concat!(" Returns a mutable reference to the inner value of [`", stringify!($name), "`].")]
1140 $vis $($cst)? fn as_inner_mut(&mut self) -> &mut $ity {
1141 &mut self.$inner
1142 }
1143 }
1144
1145 $crate::__wrapper! {
1146 @gen
1147 [
1148 $($bound)*
1149 ]
1150 [
1151 #[gen(AsMut<$ity>)]
1152 $($gen)*
1153 ]
1154 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1155 $(#[$($imeta)*])*
1156 $ivis $inner: $ity,
1157 $(
1158 $(#[$($fmeta)*])*
1159 $fvis $field: $fty,
1160 )*
1161 }
1162 }
1163 };
1164 (
1165 @gen
1166 [
1167 $($bound:tt)*
1168 ]
1169 [
1170 #[gen(AsMut<$tty:ty>)]
1171 $($gen:tt)*
1172 ]
1173 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1174 $(#[$($imeta:tt)*])*
1175 $ivis:vis $inner:ident: $ity:ty
1176 $(
1177 ,
1178 $(#[$($fmeta:tt)*])*
1179 $fvis:vis $field:ident: $fty:ty
1180 )*
1181 $(,)?
1182 }
1183 ) => {
1184 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::AsMut<$tty> for $name$(<$($lt),+>)?
1185 where
1186 $($bound)*
1187 {
1188 fn as_mut(&mut self) -> &mut $tty {
1189 &mut self.$inner
1190 }
1191 }
1192
1193 $crate::__wrapper! {
1194 @gen
1195 [
1196 $($bound)*
1197 ]
1198 [
1199 $($gen)*
1200 ]
1201 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1202 $(#[$($imeta)*])*
1203 $ivis $inner: $ity,
1204 $(
1205 $(#[$($fmeta)*])*
1206 $fvis $field: $fty,
1207 )*
1208 }
1209 }
1210 };
1211 (
1212 @gen
1213 [
1214 $($bound:tt)*
1215 ]
1216 [
1217 #[gen(Borrow)]
1218 $($gen:tt)*
1219 ]
1220 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1221 $(#[$($imeta:tt)*])*
1222 $ivis:vis $inner:ident: $ity:ty
1223 $(
1224 ,
1225 $(#[$($fmeta:tt)*])*
1226 $fvis:vis $field:ident: $fty:ty
1227 )*
1228 $(,)?
1229 }
1230 ) => {
1231 $crate::__wrapper! {
1232 @gen
1233 [
1234 $($bound)*
1235 ]
1236 [
1237 #[gen(Borrow<$ity>)]
1238 $($gen)*
1239 ]
1240 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1241 $(#[$($imeta)*])*
1242 $ivis $inner: $ity,
1243 $(
1244 $(#[$($fmeta)*])*
1245 $fvis $field: $fty,
1246 )*
1247 }
1248 }
1249 };
1250 (
1251 @gen
1252 [
1253 $($bound:tt)*
1254 ]
1255 [
1256 #[gen(Borrow<$tty:ty>)]
1257 $($gen:tt)*
1258 ]
1259 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1260 $(#[$($imeta:tt)*])*
1261 $ivis:vis $inner:ident: $ity:ty
1262 $(
1263 ,
1264 $(#[$($fmeta:tt)*])*
1265 $fvis:vis $field:ident: $fty:ty
1266 )*
1267 $(,)?
1268 }
1269 ) => {
1270 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::Borrow<$tty> for $name$(<$($lt),+>)?
1271 where
1272 $($bound)*
1273 {
1274 fn borrow(&self) -> &$tty {
1275 &self.$inner
1276 }
1277 }
1278
1279 $crate::__wrapper! {
1280 @gen
1281 [
1282 $($bound)*
1283 ]
1284 [
1285 $($gen)*
1286 ]
1287 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1288 $(#[$($imeta)*])*
1289 $ivis $inner: $ity,
1290 $(
1291 $(#[$($fmeta)*])*
1292 $fvis $field: $fty,
1293 )*
1294 }
1295 }
1296 };
1297 (
1298 @gen
1299 [
1300 $($bound:tt)*
1301 ]
1302 [
1303 #[gen(BorrowMut)]
1304 $($gen:tt)*
1305 ]
1306 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1307 $(#[$($imeta:tt)*])*
1308 $ivis:vis $inner:ident: $ity:ty
1309 $(
1310 ,
1311 $(#[$($fmeta:tt)*])*
1312 $fvis:vis $field:ident: $fty:ty
1313 )*
1314 $(,)?
1315 }
1316 ) => {
1317 $crate::__wrapper! {
1318 @gen
1319 [
1320 $($bound)*
1321 ]
1322 [
1323 #[gen(BorrowMut<$ity>)]
1324 $($gen)*
1325 ]
1326 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1327 $(#[$($imeta)*])*
1328 $ivis $inner: $ity,
1329 $(
1330 $(#[$($fmeta)*])*
1331 $fvis $field: $fty,
1332 )*
1333 }
1334 }
1335 };
1336 (
1337 @gen
1338 [
1339 $($bound:tt)*
1340 ]
1341 [
1342 #[gen(BorrowMut<$tty:ty>)]
1343 $($gen:tt)*
1344 ]
1345 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1346 $(#[$($imeta:tt)*])*
1347 $ivis:vis $inner:ident: $ity:ty
1348 $(
1349 ,
1350 $(#[$($fmeta:tt)*])*
1351 $fvis:vis $field:ident: $fty:ty
1352 )*
1353 $(,)?
1354 }
1355 ) => {
1356 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::Borrow<$tty> for $name$(<$($lt),+>)?
1357 where
1358 $($bound)*
1359 {
1360 fn borrow(&self) -> &$tty {
1361 &self.$inner
1362 }
1363 }
1364
1365 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::borrow::BorrowMut<$tty> for $name$(<$($lt),+>)?
1366 where
1367 $($bound)*
1368 {
1369 fn borrow_mut(&mut self) -> &mut $tty {
1370 &mut self.$inner
1371 }
1372 }
1373
1374 $crate::__wrapper! {
1375 @gen
1376 [
1377 $($bound)*
1378 ]
1379 [
1380 $($gen)*
1381 ]
1382 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1383 $(#[$($imeta)*])*
1384 $ivis $inner: $ity,
1385 $(
1386 $(#[$($fmeta)*])*
1387 $fvis $field: $fty,
1388 )*
1389 }
1390 }
1391 };
1392 (
1393 @gen
1394 [
1395 $($bound:tt)*
1396 ]
1397 [
1398 #[gen(Deref)]
1399 $($gen:tt)*
1400 ]
1401 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1402 $(#[$($imeta:tt)*])*
1403 $ivis:vis $inner:ident: $ity:ty
1404 $(
1405 ,
1406 $(#[$($fmeta:tt)*])*
1407 $fvis:vis $field:ident: $fty:ty
1408 )*
1409 $(,)?
1410 }
1411 ) => {
1412 $crate::__wrapper! {
1413 @gen
1414 [
1415 $($bound)*
1416 ]
1417 [
1418 #[gen(Deref<$ity>)]
1419 $($gen)*
1420 ]
1421 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1422 $(#[$($imeta)*])*
1423 $ivis $inner: $ity,
1424 $(
1425 $(#[$($fmeta)*])*
1426 $fvis $field: $fty,
1427 )*
1428 }
1429 }
1430 };
1431 (
1432 @gen
1433 [
1434 $($bound:tt)*
1435 ]
1436 [
1437 #[gen(Deref<$tty:ty>)]
1438 $($gen:tt)*
1439 ]
1440 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1441 $(#[$($imeta:tt)*])*
1442 $ivis:vis $inner:ident: $ity:ty
1443 $(
1444 ,
1445 $(#[$($fmeta:tt)*])*
1446 $fvis:vis $field:ident: $fty:ty
1447 )*
1448 $(,)?
1449 }
1450 ) => {
1451 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)?
1452 where
1453 $($bound)*
1454 {
1455 type Target = $tty;
1456
1457 fn deref(&self) -> &$tty {
1458 &self.$inner
1459 }
1460 }
1461
1462 $crate::__wrapper! {
1463 @gen
1464 [
1465 $($bound)*
1466 ]
1467 [
1468 $($gen)*
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 @gen
1482 [
1483 $($bound:tt)*
1484 ]
1485 [
1486 #[gen(DerefMut)]
1487 $($gen:tt)*
1488 ]
1489 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1490 $(#[$($imeta:tt)*])*
1491 $ivis:vis $inner:ident: $ity:ty
1492 $(
1493 ,
1494 $(#[$($fmeta:tt)*])*
1495 $fvis:vis $field:ident: $fty:ty
1496 )*
1497 $(,)?
1498 }
1499 ) => {
1500 $crate::__wrapper! {
1501 @gen
1502 [
1503 $($bound)*
1504 ]
1505 [
1506 #[gen(DerefMut<$ity>)]
1507 $($gen)*
1508 ]
1509 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1510 $(#[$($imeta)*])*
1511 $ivis $inner: $ity,
1512 $(
1513 $(#[$($fmeta)*])*
1514 $fvis $field: $fty,
1515 )*
1516 }
1517 }
1518 };
1519 (
1520 @gen
1521 [
1522 $($bound:tt)*
1523 ]
1524 [
1525 #[gen(DerefMut<$tty:ty>)]
1526 $($gen:tt)*
1527 ]
1528 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1529 $(#[$($imeta:tt)*])*
1530 $ivis:vis $inner:ident: $ity:ty
1531 $(
1532 ,
1533 $(#[$($fmeta:tt)*])*
1534 $fvis:vis $field:ident: $fty:ty
1535 )*
1536 $(,)?
1537 }
1538 ) => {
1539 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)?
1540 where
1541 $($bound)*
1542 {
1543 type Target = $tty;
1544
1545 fn deref(&self) -> &$tty {
1546 &self.$inner
1547 }
1548 }
1549
1550 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::ops::DerefMut for $name$(<$($lt),+>)?
1551 where
1552 $($bound)*
1553 {
1554 fn deref_mut(&mut self) -> &mut $tty {
1555 &mut self.$inner
1556 }
1557 }
1558
1559 $crate::__wrapper! {
1560 @gen
1561 [
1562 $($bound)*
1563 ]
1564 [
1565 $($gen)*
1566 ]
1567 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1568 $(#[$($imeta)*])*
1569 $ivis $inner: $ity,
1570 $(
1571 $(#[$($fmeta)*])*
1572 $fvis $field: $fty,
1573 )*
1574 }
1575 }
1576 };
1577 (
1578 @gen
1579 [
1580 $($bound:tt)*
1581 ]
1582 [
1583 #[gen(From)]
1584 $($gen:tt)*
1585 ]
1586 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1587 $(#[$($imeta:tt)*])*
1588 $ivis:vis $inner:ident: $ity:ty
1589 $(
1590 ,
1591 #[default($($fdefault:tt)*)]
1592 $(#[$($fmeta:tt)*])*
1593 $fvis:vis $field:ident: $fty:ty
1594 )*
1595 $(,)?
1596 }
1597 ) => {
1598 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? $name$(<$($lt),+>)?
1599 where
1600 $($bound)*
1601 {
1602 #[allow(dead_code)]
1603 #[doc = concat!(" Wraps the given inner value with [`", stringify!($name), "`]")]
1604 #[doc = ""]
1605 #[doc = " The other fields will be each set to the specified default value."]
1606 $vis const fn from($inner: $ity) -> Self {
1607 Self {
1608 $inner,
1609 $(
1610 $field: $($fdefault)*,
1611 )*
1612 }
1613 }
1614 }
1615
1616 impl$(<$($lt$(:$blt$(+$mblt)*)?),+>)? ::core::convert::From<$ity> for $name$(<$($lt),+>)?
1617 where
1618 $($bound)*
1619 {
1620 fn from($inner: $ity) -> Self {
1621 Self {
1622 $inner,
1623 $(
1624 $field: $($fdefault)*,
1625 )*
1626 }
1627 }
1628 }
1629
1630 $crate::__wrapper! {
1631 @gen
1632 [
1633 $($bound)*
1634 ]
1635 [
1636 $($gen)*
1637 ]
1638 $vis struct $name$(<$($lt$(:$blt$(+$mblt)*)?$(=$default)?),+>)? {
1639 $(#[$($imeta)*])*
1640 $ivis $inner: $ity,
1641 $(
1642 #[default($($fdefault)*)]
1643 $(#[$($fmeta)*])*
1644 $fvis $field: $fty,
1645 )*
1646 }
1647 }
1648 };
1649 (
1650 @gen
1651 [
1652 $($bound:tt)*
1653 ]
1654 [
1655 #[gen(From)]
1656 $($gen:tt)*
1657 ]
1658 $vis:vis struct $name:ident$(<$($lt:tt$(:$blt:tt$(+$mblt:tt)*)?$(=$default:ty)?),+>)? {
1659 $(#[$($imeta:tt)*])*
1660 $ivis:vis $inner:ident: $ity:ty
1661 $(
1662 ,
1663 $(#[$($fmeta:tt)*])*
1664 $fvis:vis $field:ident: $fty:ty
1665 )*
1666 $(,)?
1667 }
1668 ) => {
1669 compile_error!(
1670 "Fields except `inner` should be each provided a default value for implementing `From`."
1671 );
1672 };
1673
1674 // ! Catch-all for unsupported syntax.
1675 (@ $($other:tt)*) => {
1676 compile_error!(
1677 "Incorrect or unsupported syntax, please refer to `wrapper!`'s documentation."
1678 );
1679 };
1680
1681 // ! Entry point.
1682 ($($input:tt)*) => {
1683 $crate::__wrapper! {
1684 @parse [] [] [] [] $($input)*
1685 }
1686 };
1687}
1688
1689#[macro_export]
1690#[doc(hidden)]
1691macro_rules! __wrapper_type {
1692 (
1693 #[repr(align(cache))]
1694 $($tt:tt)*
1695 ) => {
1696 // Starting from Intel's Sandy Bridge, spatial prefetcher is now pulling pairs of 64-byte cache
1697 // lines at a time, so we have to align to 128 bytes rather than 64.
1698 //
1699 // Sources:
1700 // - https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf
1701 // - https://github.com/facebook/folly/blob/1b5288e6eea6df074758f877c849b6e73bbb9fbb/folly/lang/Align.h#L107
1702 //
1703 // aarch64/arm64ec's big.LITTLE architecture has asymmetric cores and "big" cores have 128-byte cache line size.
1704 //
1705 // Sources:
1706 // - https://www.mono-project.com/news/2016/09/12/arm64-icache/
1707 //
1708 // powerpc64 has 128-byte cache line size.
1709 //
1710 // Sources:
1711 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_ppc64x.go#L9
1712 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/powerpc/include/asm/cache.h#L26
1713 #[cfg_attr(
1714 any(
1715 target_arch = "x86_64",
1716 target_arch = "aarch64",
1717 target_arch = "arm64ec",
1718 target_arch = "powerpc64",
1719 ),
1720 repr(align(128))
1721 )]
1722 // arm, mips, mips64, sparc, and hexagon have 32-byte cache line size.
1723 //
1724 // Sources:
1725 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_arm.go#L7
1726 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips.go#L7
1727 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mipsle.go#L7
1728 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_mips64x.go#L9
1729 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L17
1730 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/hexagon/include/asm/cache.h#L12
1731 #[cfg_attr(
1732 any(
1733 target_arch = "arm",
1734 target_arch = "mips",
1735 target_arch = "mips32r6",
1736 target_arch = "mips64",
1737 target_arch = "mips64r6",
1738 target_arch = "sparc",
1739 target_arch = "hexagon",
1740 ),
1741 repr(align(32))
1742 )]
1743 // m68k has 16-byte cache line size.
1744 //
1745 // Sources:
1746 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/m68k/include/asm/cache.h#L9
1747 #[cfg_attr(target_arch = "m68k", repr(align(16)))]
1748 // s390x has 256-byte cache line size.
1749 //
1750 // Sources:
1751 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_s390x.go#L7
1752 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/s390/include/asm/cache.h#L13
1753 #[cfg_attr(target_arch = "s390x", repr(align(256)))]
1754 // x86, wasm, riscv, and sparc64 have 64-byte cache line size.
1755 //
1756 // Sources:
1757 // - https://github.com/golang/go/blob/dda2991c2ea0c5914714469c4defc2562a907230/src/internal/cpu/cpu_x86.go#L9
1758 // - https://github.com/golang/go/blob/3dd58676054223962cd915bb0934d1f9f489d4d2/src/internal/cpu/cpu_wasm.go#L7
1759 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/riscv/include/asm/cache.h#L10
1760 // - https://github.com/torvalds/linux/blob/3516bd729358a2a9b090c1905bd2a3fa926e24c6/arch/sparc/include/asm/cache.h#L19
1761 //
1762 // All others are assumed to have 64-byte cache line size.
1763 #[cfg_attr(
1764 not(any(
1765 target_arch = "x86_64",
1766 target_arch = "aarch64",
1767 target_arch = "arm64ec",
1768 target_arch = "powerpc64",
1769 target_arch = "arm",
1770 target_arch = "mips",
1771 target_arch = "mips32r6",
1772 target_arch = "mips64",
1773 target_arch = "mips64r6",
1774 target_arch = "sparc",
1775 target_arch = "hexagon",
1776 target_arch = "m68k",
1777 target_arch = "s390x",
1778 )),
1779 repr(align(64))
1780 )]
1781 $($tt)*
1782 };
1783 ($($tt:tt)*) => {
1784 $($tt)*
1785 };
1786}
1787
1788#[deprecated(since = "0.5.0", note = "Use `wrapper!` instead")]
1789#[macro_export]
1790#[doc(hidden)]
1791macro_rules! general_wrapper {
1792 ($($tt:tt)+) => {
1793 $crate::wrapper! {
1794 #[gen(AsRef)]
1795 #[gen(Borrow)]
1796 #[gen(From)]
1797 $($tt)+
1798 }
1799 };
1800}
1801
1802#[deprecated(since = "0.5.0", note = "Use `wrapper!` instead")]
1803#[macro_export]
1804#[doc(hidden)]
1805macro_rules! aligned {
1806 ($($tt:tt)+) => {
1807 $crate::wrapper! {
1808 #[repr(align(cache))]
1809 $($tt)+
1810 }
1811 };
1812}
1813
1814wrapper!(
1815 #[gen(AsRef)]
1816 #[gen(AsMut)]
1817 #[gen(From)]
1818 #[allow(dead_code)]
1819 struct TestConstFromFromInnerAsInner<T> {
1820 inner: T,
1821 }
1822);