wrapper_lite/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3
4#[macro_export]
5/// Helper macro for creating a wrapper over any type (new-type idiom).
6///
7/// This is a shortcut for using the [`wrapper!`] macro with the most common
8/// impls (`AsRef`, `Borrow`, `From`).
9///
10/// ```rust
11/// wrapper_lite::general_wrapper!(
12///     #[derive(Debug, Clone, Copy)]
13///     pub struct ExampleWrapper<'a, P>(pub(crate) &'a P);
14/// );
15/// ```
16///
17/// This is equivalent to using the `wrapper!` macro with the following
18/// attributes:
19///
20/// ```rust
21/// wrapper_lite::wrapper!(
22///     #[wrapper_impl(AsRef)]
23///     #[wrapper_impl(Borrow)]
24///     #[wrapper_impl(From)]
25///     #[derive(Debug, Clone, Copy)]
26///     pub struct ExampleWrapper<'a, P>(pub(crate) &'a P);
27/// );
28/// ```
29///
30/// You can certainly add attributes like `#[wrapper_impl(Deref)]` other than
31/// the preset ones. See [`wrapper!`] for more details.
32///
33/// ```rust
34/// wrapper_lite::general_wrapper!(
35///     #[wrapper_impl(Deref)]
36///     #[derive(Debug, Clone, Copy)]
37///     pub struct ExampleWrapper<'a, P>(pub(crate) &'a P);
38/// );
39/// ```
40macro_rules! general_wrapper {
41    ($($tt:tt)+) => {
42        $crate::wrapper! {
43            #[wrapper_impl(AsRef)]
44            #[wrapper_impl(Borrow)]
45            #[wrapper_impl(From)]
46            $($tt)+
47        }
48    };
49}
50
51#[macro_export]
52/// Helper macro for creating a wrapper over any type (new-type idiom).
53///
54/// # Usage
55///
56/// It's worth noting that `wrapper! { ... }` is almost equivalent to
57/// `wrapper!( ... );` but lacking cargo-fmt support. We recommend using the
58/// latter.
59///
60/// ## Usage: basic
61///
62/// ```rust
63/// wrapper_lite::wrapper!(
64///     #[wrapper_impl(AsRef)]
65///     #[wrapper_impl(AsMut)]
66///     // #[wrapper_impl(Borrow)]
67///     #[wrapper_impl(BorrowMut)]
68///     // #[wrapper_impl(Deref)]
69///     #[wrapper_impl(DerefMut)]
70///     #[wrapper_impl(From)]
71///     #[derive(Debug, Clone, Copy)]
72///     pub struct ExampleWrapper<'a, P>(pub(crate) &'a P);
73/// );
74/// ```
75///
76/// Generates const accessor methods for wrapper types implementing `AsRef` and
77/// `AsMut` traits.
78///
79/// For types implementing `AsRef`, this creates a const method `as_inner` that
80/// returns a reference to the wrapped value. For types implementing `AsMut`,
81/// this creates a const method `as_inner_mut` that returns a mutable reference
82/// to the wrapped value.
83///
84/// Additionally generates a const constructor method `const_from` for the
85/// wrapper type, using the same visibility as the inner field. When the `From`
86/// trait is implemented, also generates a public const method `from`.
87///
88/// ## Usage: advanced
89///
90/// You can also create a wrapper type with a struct with multiple fields,
91/// especially when some lifetime markers or generics markers are too complex,
92/// or you need some custom fields.
93///
94/// Here's an complex example:
95///
96/// ```rust
97/// wrapper_lite::wrapper!(
98///     #[wrapper_impl(AsMut)]
99///     #[wrapper_impl(AsRef)]
100///     // #[wrapper_impl(Borrow)]
101///     #[wrapper_impl(BorrowMut)]
102///     //     #[wrapper_impl(Deref)]
103///     #[wrapper_impl(DerefMut)]
104///     #[wrapper_impl(From)]
105///     #[wrapper_impl(Debug)]
106///     #[derive(Clone, Copy, PartialEq, Eq)]
107///     #[repr(transparent)]
108///     pub struct ExampleWrapperComplex<'a, 'b, P> {
109///         inner: P,
110///         _a: ::core::marker::PhantomData<&'a ()>,
111///         _b: ::core::marker::PhantomData<&'b ()>,
112///     }
113/// );
114/// ```
115///
116/// There're some limitations:
117///
118/// - The inner field must be named as `inner`.
119/// - When no default value is specified, the wrapper type will not implement
120///   the `From` trait. Will also not generate the `const_from` method.
121/// - Does not automatically apply `repr(transparent)` attribute, since the
122///   macro doesn't know if other fields were zero-sized types (ZST).
123///
124/// ## Special attributes
125///
126/// ### `Debug` and `DebugName`
127///
128/// We offer `Debug` and `DebugName` attributes to control how the wrapper type
129/// is printed when using the `Debug` trait, instead of `#[derive(Debug)]`.
130///
131/// - `#[wrapper_impl(Debug)]`: transparently implements the `Debug` trait if
132///   the inner type implements it. The debug output is the same as the inner
133///   one.
134/// - `#[wrapper_impl(DebugName)]`: implements the `Debug` trait, but only
135///   prints the name of the wrapper type.
136///
137/// ```rust
138/// wrapper_lite::wrapper!(
139///     #[wrapper_impl(Debug)]
140///     #[derive(Clone, Copy)]
141///     pub struct ExampleWrapperDebug<'a, P>(&'a P);
142/// );
143///
144/// wrapper_lite::wrapper!(
145///     #[wrapper_impl(DebugName)]
146///     #[derive(Clone, Copy)]
147///     pub struct ExampleWrapperDebugName<'a, P>(&'a P);
148/// );
149///
150/// let data = "Hello".to_string();
151///
152/// // Here we transparently print the debug output of the inner type.
153/// assert_eq!(
154///     format!("{:?}", ExampleWrapperDebug { inner: &data }),
155///     "\"Hello\""
156/// );
157/// // Here we only print the name of the wrapper type.
158/// assert_eq!(
159///     format!("{:?}", ExampleWrapperDebugName { inner: &data }),
160///     "ExampleWrapperDebugName"
161/// );
162/// ```
163///
164/// ### `ConstAsMut`
165///
166/// Like `AsMut`, but instead generates a const version of `as_inner_mut` method
167/// (stable since Rust 1.83.0+).
168///
169/// ```rust,no_run
170/// wrapper_lite::wrapper!(
171///     #[wrapper_impl(ConstAsMut)]
172///     #[derive(Debug, Clone, Copy)]
173///     pub struct ExampleWrapper<P>(pub(crate) P);
174/// );
175///
176/// const fn const_fn_example<P>(w: &mut ExampleWrapper<P>) -> &mut P {
177///     w.as_inner_mut()
178/// }
179/// ```
180///
181/// ## Notes
182///
183/// - The `wrapper_impl` attribute must be on top of any other attributes.
184/// - For `BorrowMut` and `DerefMut`, the macro will automatically implement the
185///   corresponding `Borrow` and `Deref` traits, so the following two examples
186///   will fail to compile:
187///
188///   ```rust,compile_fail
189///   wrapper_lite::wrapper!(
190///       #[wrapper_impl(Borrow)]
191///       #[wrapper_impl(BorrowMut)]
192///       pub struct ExampleWrapper<P>(pub(crate) P);
193///   );
194///   ```
195///
196///   ```rust,compile_fail
197///   wrapper_lite::wrapper!(
198///       #[wrapper_impl(Deref)]
199///       #[wrapper_impl(DerefMut)]
200///       pub struct ExampleWrapper<P>(pub(crate) P);
201///   );
202///   ```
203macro_rules! wrapper {
204    // To filter out the `wrapper_impl` attribute and extract the inner type.
205    (
206        @INTERNAL IMPL
207        #[wrapper_impl(AsRef)]
208        $($tt:tt)*
209    ) => {
210        $crate::wrapper! {
211            @INTERNAL IMPL
212            $($tt)*
213        }
214    };
215    (
216        @INTERNAL IMPL
217        #[wrapper_impl(AsMut)]
218        $($tt:tt)*
219    ) => {
220        $crate::wrapper! {
221            @INTERNAL IMPL
222            $($tt)*
223        }
224    };
225    (
226        @INTERNAL IMPL
227        #[wrapper_impl(ConstAsMut)]
228        $($tt:tt)*
229    ) => {
230        $crate::wrapper! {
231            @INTERNAL IMPL
232            $($tt)*
233        }
234    };
235    (
236        @INTERNAL IMPL
237        #[wrapper_impl(Borrow)]
238        $($tt:tt)*
239    ) => {
240        $crate::wrapper! {
241            @INTERNAL IMPL
242            $($tt)*
243        }
244    };
245    (
246        @INTERNAL IMPL
247        #[wrapper_impl(BorrowMut)]
248        $($tt:tt)*
249    ) => {
250        $crate::wrapper! {
251            @INTERNAL IMPL
252            $($tt)*
253        }
254    };
255    (
256        @INTERNAL IMPL
257        #[wrapper_impl(Deref)]
258        $($tt:tt)*
259    ) => {
260        $crate::wrapper! {
261            @INTERNAL IMPL
262            $($tt)*
263        }
264    };
265    (
266        @INTERNAL IMPL
267        #[wrapper_impl(DerefMut)]
268        $($tt:tt)*
269    ) => {
270        $crate::wrapper! {
271            @INTERNAL IMPL
272            $($tt)*
273        }
274    };
275    (
276        @INTERNAL IMPL
277        #[wrapper_impl(From)]
278        $($tt:tt)*
279    ) => {
280        $crate::wrapper! {
281            @INTERNAL IMPL
282            $($tt)*
283        }
284    };
285    (
286        @INTERNAL IMPL
287        #[wrapper_impl(Debug)]
288        $($tt:tt)*
289    ) => {
290        $crate::wrapper! {
291            @INTERNAL IMPL
292            $($tt)*
293        }
294    };
295    (
296        @INTERNAL IMPL
297        #[wrapper_impl(DebugName)]
298        $($tt:tt)*
299    ) => {
300        $crate::wrapper! {
301            @INTERNAL IMPL
302            $($tt)*
303        }
304    };
305
306    // The actual implementation of the wrapper type: `pub Name<...>(...)`
307    (
308        @INTERNAL IMPL
309        $(#[$outer:meta])*
310        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? ($inner_vis:vis $inner_ty:ty) $(;)?
311    ) => {
312        $(#[$outer])*
313        #[repr(transparent)]
314        $vis struct $name$(<$($lt),+>)? {
315            /// Inner value
316            $inner_vis inner: $inner_ty,
317        }
318
319        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
320            #[inline(always)]
321            #[doc = concat!("Creates a new instance of [`", stringify!($name), "`]")]
322            $inner_vis const fn const_from(inner: $inner_ty) -> Self {
323                Self {
324                    inner,
325                }
326            }
327        }
328    };
329
330    // The actual implementation of the wrapper type: `pub struct Name<...> { ... }`, with field initial value provided, make `const_from` const.
331    (
332        @INTERNAL IMPL
333        $(#[$outer:meta])*
334        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
335            $(#[$field_inner_meta:meta])*
336            $inner_vis:vis inner: $inner_ty:ty
337            $(
338                ,
339                $(#[$field_meta:meta])*
340                $field_vis:vis $field:ident: $field_ty:ty = $field_default: expr
341            )*
342            $(,)?
343        }
344    ) => {
345        $(#[$outer])*
346        $vis struct $name$(<$($lt),+>)? {
347            $(#[$field_inner_meta])*
348            $inner_vis inner: $inner_ty,
349            $(
350                $(#[$field_meta])*
351                $field_vis $field: $field_ty
352            ),*
353        }
354
355        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
356            #[inline(always)]
357            #[doc = concat!("Creates a new instance of [`", stringify!($name), "`]")]
358            $inner_vis const fn const_from(inner: $inner_ty) -> Self {
359                Self {
360                    inner,
361                    $(
362                        $field: $field_default,
363                    )*
364                }
365            }
366        }
367    };
368
369    // The actual implementation of the wrapper type with fields: `pub struct Name<...> { ... }`
370    (
371        @INTERNAL IMPL
372        $(#[$outer:meta])*
373        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
374            $(#[$field_inner_meta:meta])*
375            $inner_vis:vis inner: $inner_ty:ty
376            $(
377                ,
378                $(#[$field_meta:meta])*
379                $field_vis:vis $field:ident: $field_ty:ty
380            )*
381            $(,)?
382        }
383    ) => {
384        $(#[$outer])*
385        $vis struct $name$(<$($lt),+>)? {
386            $(#[$field_inner_meta])*
387            $inner_vis inner: $inner_ty
388            $(
389                ,
390                $(#[$field_meta])*
391                $field_vis $field: $field_ty
392            )*
393        }
394    };
395
396    // Extract wrapper impl for `AsRef` trait.
397    (
398        @INTERNAL WRAPPER_IMPL
399        #[wrapper_impl(AsRef)]
400        $($tt:tt)*
401    ) => {
402        $crate::wrapper! {
403            @INTERNAL WRAPPER_IMPL_AS_REF
404            $($tt)*
405        }
406
407        $crate::wrapper! {
408            @INTERNAL WRAPPER_IMPL
409            $($tt)*
410        }
411    };
412
413    // Extract wrapper impl for `AsMut` trait.
414    (
415        @INTERNAL WRAPPER_IMPL
416        #[wrapper_impl(AsMut)]
417        $($tt:tt)*
418    ) => {
419        $crate::wrapper! {
420            @INTERNAL WRAPPER_IMPL_AS_MUT
421            $($tt)*
422        }
423
424        $crate::wrapper! {
425            @INTERNAL WRAPPER_IMPL
426            $($tt)*
427        }
428    };
429    (
430        @INTERNAL WRAPPER_IMPL
431        #[wrapper_impl(ConstAsMut)]
432        $($tt:tt)*
433    ) => {
434        $crate::wrapper! {
435            @INTERNAL WRAPPER_IMPL_CONST_AS_MUT
436            $($tt)*
437        }
438
439        $crate::wrapper! {
440            @INTERNAL WRAPPER_IMPL
441            $($tt)*
442        }
443    };
444
445    // Extract wrapper impl for `Borrow` trait.
446    (
447        @INTERNAL WRAPPER_IMPL
448        #[wrapper_impl(Borrow)]
449        $($tt:tt)*
450    ) => {
451        $crate::wrapper! {
452            @INTERNAL WRAPPER_IMPL_BORROW
453            $($tt)*
454        }
455
456        $crate::wrapper! {
457            @INTERNAL WRAPPER_IMPL
458            $($tt)*
459        }
460    };
461
462    // Extract wrapper impl for `BorrowMut` trait.
463    (
464        @INTERNAL WRAPPER_IMPL
465        #[wrapper_impl(BorrowMut)]
466        $($tt:tt)*
467    ) => {
468        $crate::wrapper! {
469            @INTERNAL WRAPPER_IMPL_BORROW
470            $($tt)*
471        }
472
473        $crate::wrapper! {
474            @INTERNAL WRAPPER_IMPL_BORROW_MUT
475            $($tt)*
476        }
477
478        $crate::wrapper! {
479            @INTERNAL WRAPPER_IMPL
480            $($tt)*
481        }
482    };
483
484    // Extract wrapper impl for `Debug` trait.
485    (
486        @INTERNAL WRAPPER_IMPL
487        #[wrapper_impl(Debug)]
488        $($tt:tt)*
489    ) => {
490        $crate::wrapper! {
491            @INTERNAL WRAPPER_IMPL_DEBUG
492            $($tt)*
493        }
494
495        $crate::wrapper! {
496            @INTERNAL WRAPPER_IMPL
497            $($tt)*
498        }
499    };
500
501    // Extract wrapper impl for `Debug` trait  printing its name only.
502    (
503        @INTERNAL WRAPPER_IMPL
504        #[wrapper_impl(DebugName)]
505        $($tt:tt)*
506    ) => {
507        $crate::wrapper! {
508            @INTERNAL WRAPPER_IMPL_DEBUG_NAME
509            $($tt)*
510        }
511
512        $crate::wrapper! {
513            @INTERNAL WRAPPER_IMPL
514            $($tt)*
515        }
516    };
517
518    // Extract wrapper impl for `DerefMut` trait (and `Deref`).
519    (
520        @INTERNAL WRAPPER_IMPL
521        #[wrapper_impl(DerefMut)]
522        $($tt:tt)*
523    ) => {
524        $crate::wrapper! {
525            @INTERNAL WRAPPER_IMPL_DEREF
526            $($tt)*
527        }
528
529        $crate::wrapper! {
530            @INTERNAL WRAPPER_IMPL_DEREF_MUT
531            $($tt)*
532        }
533
534        $crate::wrapper! {
535            @INTERNAL WRAPPER_IMPL
536            $($tt)*
537        }
538    };
539
540    // Extract wrapper impl for `Deref` trait.
541    (
542        @INTERNAL WRAPPER_IMPL
543        #[wrapper_impl(Deref)]
544        $($tt:tt)*
545    ) => {
546        $crate::wrapper! {
547            @INTERNAL WRAPPER_IMPL_DEREF
548            $($tt)*
549        }
550
551        $crate::wrapper! {
552            @INTERNAL WRAPPER_IMPL
553            $($tt)*
554        }
555    };
556
557    // Extract wrapper impl for `From` trait.
558    (
559        @INTERNAL WRAPPER_IMPL
560        #[wrapper_impl(From)]
561        $($tt:tt)*
562    ) => {
563        $crate::wrapper! {
564            @INTERNAL WRAPPER_IMPL_FROM
565            $($tt)*
566        }
567
568        $crate::wrapper! {
569            @INTERNAL WRAPPER_IMPL
570            $($tt)*
571        }
572    };
573
574    // ================ Impl `AsRef` trait for the wrapper type. ================
575    (
576        @INTERNAL WRAPPER_IMPL_AS_REF
577        $(#[$meta:meta])*
578        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
579        ($inner_vis:vis $inner_ty:ty)
580        $($tt:tt)*
581    ) => {
582        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsRef<$inner_ty> for $name$(<$($lt),+>)? {
583            fn as_ref(&self) -> &$inner_ty {
584                &self.inner
585            }
586        }
587
588        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
589            /// Returns a reference to the inner value.
590            #[inline(always)]
591            pub const fn as_inner(&self) -> &$inner_ty {
592                &self.inner
593            }
594        }
595    };
596    (
597        @INTERNAL WRAPPER_IMPL_AS_REF
598        $(#[$meta:meta])*
599        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
600            $(#[$field_inner_meta:meta])*
601            $inner_vis:vis inner: $inner_ty:ty
602            $(
603                ,
604                $(#[$field_meta:meta])*
605                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
606            )*
607            $(,)?
608        }
609    ) => {
610        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsRef<$inner_ty> for $name$(<$($lt),+>)? {
611            fn as_ref(&self) -> &$inner_ty {
612                &self.inner
613            }
614        }
615
616        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
617            /// Returns a reference to the inner value.
618            #[inline(always)]
619            pub const fn as_inner(&self) -> &$inner_ty {
620                &self.inner
621            }
622        }
623    };
624    // ================ Impl `AsRef` trait for the wrapper type. ================
625
626
627    // ================ Impl `AsMut` trait for the wrapper type. ================
628    (
629        @INTERNAL WRAPPER_IMPL_AS_MUT
630        $(#[$meta:meta])*
631        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
632        ($inner_vis:vis $inner_ty:ty)
633        $($tt:tt)*
634    ) => {
635        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsMut<$inner_ty> for $name$(<$($lt),+>)? {
636            fn as_mut(&mut self) -> &mut $inner_ty {
637                &mut self.inner
638            }
639        }
640
641        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
642            #[inline(always)]
643            /// Returns a mutable reference to the inner value.
644            pub fn as_inner_mut(&mut self) -> &mut $inner_ty {
645                &mut self.inner
646            }
647        }
648    };
649    (
650        @INTERNAL WRAPPER_IMPL_AS_MUT
651        $(#[$meta:meta])*
652        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
653            $(#[$field_inner_meta:meta])*
654            $inner_vis:vis inner: $inner_ty:ty
655            $(
656                ,
657                $(#[$field_meta:meta])*
658                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
659            )*
660            $(,)?
661        }
662    ) => {
663        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsMut<$inner_ty> for $name$(<$($lt),+>)? {
664            #[inline(always)]
665            fn as_mut(&mut self) -> &mut $inner_ty {
666                &mut self.inner
667            }
668        }
669
670        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
671            #[inline(always)]
672            /// Returns a mutable reference to the inner value.
673            pub fn as_inner_mut(&mut self) -> &mut $inner_ty {
674                &mut self.inner
675            }
676        }
677    };
678
679    // Const version
680    (
681        @INTERNAL WRAPPER_IMPL_CONST_AS_MUT
682        $(#[$meta:meta])*
683        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
684        ($inner_vis:vis $inner_ty:ty)
685        $($tt:tt)*
686    ) => {
687        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsMut<$inner_ty> for $name$(<$($lt),+>)? {
688            fn as_mut(&mut self) -> &mut $inner_ty {
689                &mut self.inner
690            }
691        }
692
693        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
694            #[inline(always)]
695            /// Returns a mutable reference to the inner value.
696            pub const fn as_inner_mut(&mut self) -> &mut $inner_ty {
697                &mut self.inner
698            }
699        }
700    };
701    (
702        @INTERNAL WRAPPER_IMPL_CONST_AS_MUT
703        $(#[$meta:meta])*
704        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
705            $(#[$field_inner_meta:meta])*
706            $inner_vis:vis inner: $inner_ty:ty
707            $(
708                ,
709                $(#[$field_meta:meta])*
710                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
711            )*
712            $(,)?
713        }
714    ) => {
715        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsMut<$inner_ty> for $name$(<$($lt),+>)? {
716            #[inline(always)]
717            fn as_mut(&mut self) -> &mut $inner_ty {
718                &mut self.inner
719            }
720        }
721
722        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
723            #[inline(always)]
724            /// Returns a mutable reference to the inner value.
725            pub const fn as_inner_mut(&mut self) -> &mut $inner_ty {
726                &mut self.inner
727            }
728        }
729    };
730    // ================ Impl `AsMut` trait for the wrapper type. ================
731
732    // ================ Impl `Borrow` trait for the wrapper type. ================
733    (
734        @INTERNAL WRAPPER_IMPL_BORROW
735        $(#[$meta:meta])*
736        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
737        ($inner_vis:vis $inner_ty:ty)
738        $($tt:tt)*
739    ) => {
740        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::borrow::Borrow<$inner_ty> for $name$(<$($lt),+>)? {
741            fn borrow(&self) -> &$inner_ty {
742                &self.inner
743            }
744        }
745    };
746    (
747        @INTERNAL WRAPPER_IMPL_BORROW
748        $(#[$meta:meta])*
749        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
750            $(#[$field_inner_meta:meta])*
751            $inner_vis:vis inner: $inner_ty:ty
752            $(
753                ,
754                $(#[$field_meta:meta])*
755                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
756            )*
757            $(,)?
758        }
759    ) => {
760        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::borrow::Borrow<$inner_ty> for $name$(<$($lt),+>)? {
761            fn borrow(&self) -> &$inner_ty {
762                &self.inner
763            }
764        }
765    };
766    // ================ Impl `Borrow` trait for the wrapper type. ================
767
768    // ================ Impl `BorrowMut` trait for the wrapper type. ================
769    (
770        @INTERNAL WRAPPER_IMPL_BORROW_MUT
771        $(#[$meta:meta])*
772        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
773        ($inner_vis:vis $inner_ty:ty)
774        $($tt:tt)*
775    ) => {
776        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::borrow::BorrowMut<$inner_ty> for $name$(<$($lt),+>)? {
777            fn borrow_mut(&mut self) -> &mut $inner_ty {
778                &mut self.inner
779            }
780        }
781    };
782    (
783        @INTERNAL WRAPPER_IMPL_BORROW_MUT
784        $(#[$meta:meta])*
785        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
786            $(#[$field_inner_meta:meta])*
787            $inner_vis:vis inner: $inner_ty:ty
788            $(
789                ,
790                $(#[$field_meta:meta])*
791                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
792            )*
793            $(,)?
794        }
795    ) => {
796        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::borrow::BorrowMut<$inner_ty> for $name$(<$($lt),+>)? {
797            fn borrow_mut(&mut self) -> &mut $inner_ty {
798                &mut self.inner
799            }
800        }
801    };
802    // ================ Impl `Borrow` trait for the wrapper type. ================
803
804    // ================ Impl `Debug` trait for the wrapper type. ================
805    (
806        @INTERNAL WRAPPER_IMPL_DEBUG
807        $(#[$meta:meta])*
808        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
809        ($inner_vis:vis $inner_ty:ty)
810        $($tt:tt)*
811    ) => {
812        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
813        where
814            $inner_ty: ::core::fmt::Debug,
815        {
816            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
817                self.inner.fmt(f)
818            }
819        }
820    };
821    (
822        @INTERNAL WRAPPER_IMPL_DEBUG
823        $(#[$meta:meta])*
824        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
825            $(#[$field_inner_meta:meta])*
826            $inner_vis:vis inner: $inner_ty:ty
827            $(
828                ,
829                $(#[$field_meta:meta])*
830                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
831            )*
832            $(,)?
833        }
834    ) => {
835        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
836        where
837            $inner_ty: ::core::fmt::Debug,
838        {
839            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
840                self.inner.fmt(f)
841            }
842        }
843    };
844    // ================ Impl `Debug` trait for the wrapper type. ================
845
846    // ================ Impl `DebugName` trait for the wrapper type. ================
847    (
848        @INTERNAL WRAPPER_IMPL_DEBUG_NAME
849        $(#[$meta:meta])*
850        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
851        ($inner_vis:vis $inner_ty:ty)
852        $($tt:tt)*
853    ) => {
854        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)? {
855            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
856                f.debug_struct(stringify!($name)).finish()
857            }
858        }
859    };
860    (
861        @INTERNAL WRAPPER_IMPL_DEBUG_NAME
862        $(#[$meta:meta])*
863        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
864            $(#[$field_inner_meta:meta])*
865            $inner_vis:vis inner: $inner_ty:ty
866            $(
867                ,
868                $(#[$field_meta:meta])*
869                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
870            )*
871            $(,)?
872        }
873    ) => {
874        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)? {
875            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
876                f.debug_struct(stringify!($name)).finish()
877            }
878        }
879    };
880    // ================ Impl `DebugName` trait for the wrapper type. ================
881
882    // ================ Impl `DerefMut` traits for the wrapper type. ================
883    (
884        @INTERNAL WRAPPER_IMPL_DEREF_MUT
885        $(#[$meta:meta])*
886        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
887        ($inner_vis:vis $inner_ty:ty)
888        $($tt:tt)*
889    ) => {
890        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::DerefMut for $name$(<$($lt),+>)? {
891            fn deref_mut(&mut self) -> &mut Self::Target {
892                &mut self.inner
893            }
894        }
895    };
896    (
897        @INTERNAL WRAPPER_IMPL_DEREF_MUT
898        $(#[$meta:meta])*
899        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
900            $(#[$field_inner_meta:meta])*
901            $inner_vis:vis inner: $inner_ty:ty
902            $(
903                ,
904                $(#[$field_meta:meta])*
905                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
906            )*
907            $(,)?
908        }
909    ) => {
910        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::DerefMut for $name$(<$($lt),+>)? {
911            fn deref_mut(&mut self) -> &mut Self::Target {
912                &mut self.inner
913            }
914        }
915    };
916    // ================ Impl `DerefMut` traits for the wrapper type. ================
917
918    // ================ Impl `Deref` trait for the wrapper type. ================
919    (
920        @INTERNAL WRAPPER_IMPL_DEREF
921        $(#[$meta:meta])*
922        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
923        ($inner_vis:vis $inner_ty:ty)
924        $($tt:tt)*
925    ) => {
926        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)? {
927            type Target = $inner_ty;
928
929            fn deref(&self) -> &Self::Target {
930                &self.inner
931            }
932        }
933    };
934    (
935        @INTERNAL WRAPPER_IMPL_DEREF
936        $(#[$meta:meta])*
937        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
938            $(#[$field_inner_meta:meta])*
939            $inner_vis:vis inner: $inner_ty:ty
940            $(
941                ,
942                $(#[$field_meta:meta])*
943                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
944            )*
945            $(,)?
946        }
947    ) => {
948        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)? {
949            type Target = $inner_ty;
950
951            fn deref(&self) -> &Self::Target {
952                &self.inner
953            }
954        }
955    };
956    // ================ Impl `Deref` trait for the wrapper type. ================
957
958    // ================ Impl `From` trait for the wrapper type. ================
959    (
960        @INTERNAL WRAPPER_IMPL_FROM
961        $(#[$meta:meta])*
962        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? $(;)?
963        ($inner_vis:vis $inner_ty:ty)
964        $($tt:tt)*
965    ) => {
966        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::From<$inner_ty> for $name$(<$($lt),+>)? {
967            fn from(inner: $inner_ty) -> Self {
968                Self::const_from(inner)
969            }
970        }
971
972        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
973            /// Creates a new instance of the wrapper type from the inner value.
974            #[allow(unreachable_pub)]
975            #[inline(always)]
976            pub const fn from(inner: $inner_ty) -> Self {
977                Self::const_from(inner)
978            }
979        }
980    };
981    (
982        @INTERNAL WRAPPER_IMPL_FROM
983        $(#[$meta:meta])*
984        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
985            $(#[$field_inner_meta:meta])*
986            $inner_vis:vis inner: $inner_ty:ty
987            $(
988                ,
989                $(#[$field_meta:meta])*
990                $field_vis:vis $field:ident: $field_ty:ty = $field_default:expr
991            )*
992            $(,)?
993        }
994    ) => {
995        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::From<$inner_ty> for $name$(<$($lt),+>)? {
996            fn from(inner: $inner_ty) -> Self {
997                Self::const_from(inner)
998            }
999        }
1000
1001        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
1002            /// Creates a new instance of the wrapper type from the inner value.
1003            #[allow(unreachable_pub)]
1004            #[inline(always)]
1005            pub const fn from(inner: $inner_ty) -> Self {
1006                Self::const_from(inner)
1007            }
1008        }
1009    };
1010    (@INTERNAL WRAPPER_IMPL_FROM $($tt:tt)*) => {};
1011    // ================ Impl `From` trait for the wrapper type. ================
1012
1013    // No other wrapper_impl meta
1014    (@INTERNAL WRAPPER_IMPL $($tt:tt)*) => {};
1015
1016    // Catch-all for invalid usage of the macro.
1017    (@INTERNAL $($tt:tt)*) => {
1018        compile_error!(
1019            "Invalid usage of `wrapper!` macro. @INTERNAL \
1020            Please refer to the documentation for the correct syntax."
1021        );
1022    };
1023
1024    // Core macro for the wrapper type.
1025    ($($tt:tt)*) => {
1026        $crate::wrapper!(@INTERNAL IMPL $($tt)*);
1027        $crate::wrapper!(@INTERNAL WRAPPER_IMPL $($tt)*);
1028    };
1029}