wrapper_lite/
lib.rs

1//! Helper macro for creating a wrapper over any type (new-type idiom).
2
3#![no_std]
4
5#[macro_export]
6/// Helper macro for creating a wrapper over any type (new-type idiom).
7///
8/// This is a shortcut for using the `wrapper!` macro with the most common impls
9/// (`AsRef`, `Borrow`, `From`).
10///
11/// ```rust
12/// # use wrapper_lite::*;
13/// general_wrapper! {
14///     #[derive(Debug, Clone, Copy)]
15///     pub ExampleWrapper<'a, P>(pub(crate) &'a P)
16/// }
17/// ```
18///
19/// This is equivalent to using the `wrapper!` macro with the following
20/// attributes:
21///
22/// ```rust
23/// # use wrapper_lite::*;
24/// wrapper! {
25///     #[wrapper_impl(AsRef)]
26///     #[wrapper_impl(Borrow)]
27///     #[wrapper_impl(From)]
28///     #[derive(Debug, Clone, Copy)]
29///     pub ExampleWrapper<'a, P>(pub(crate) &'a P)
30/// }
31/// ```
32///
33/// You can certainly add attributes like `#[wrapper_impl(Deref)]` to implement
34/// other traits based on the preset ones.
35///
36/// ```rust
37/// # use wrapper_lite::*;
38/// general_wrapper! {
39///     #[wrapper_impl(Deref)]
40///     #[derive(Debug, Clone, Copy)]
41///     pub ExampleWrapper<'a, P>(pub(crate) &'a P)
42/// }
43/// ```
44///
45/// See [`wrapper!`] for more details.
46macro_rules! general_wrapper {
47    ($($tt:tt)+) => {
48        $crate::wrapper! {
49            #[wrapper_impl(AsRef)]
50            #[wrapper_impl(Borrow)]
51            #[wrapper_impl(From)]
52            $($tt)+
53        }
54    };
55}
56
57#[macro_export]
58/// Helper macro for creating a wrapper over any type (new-type idom).
59///
60/// # Usage: basic
61///
62/// ```rust
63/// # use wrapper_lite::*;
64/// wrapper! {
65///     #[wrapper_impl(AsRef)]
66///     #[wrapper_impl(AsMut)]
67///     #[wrapper_impl(Borrow)]
68///     // #[wrapper_impl(Deref)]
69///     #[wrapper_impl(DerefMut)]
70///     #[wrapper_impl(From)]
71///     #[derive(Debug, Clone, Copy)]
72///     pub 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 needed.
92///
93/// ```rust
94/// # use wrapper_lite::*;
95/// wrapper! {
96///     #[wrapper_impl(AsMut)]
97///     #[wrapper_impl(AsRef)]
98///     #[wrapper_impl(Borrow)]
99///     #[wrapper_impl(DerefMut)]
100///     #[wrapper_impl(From)]
101///     #[derive(Debug)]
102///     #[repr(transparent)]
103///     pub struct ExampleWrapperComplex<'a, 'b, P> {
104///         inner: P,
105///         _a: ::core::marker::PhantomData<&'a ()> = ::core::marker::PhantomData,
106///         _b: ::core::marker::PhantomData<&'b ()> = ::core::marker::PhantomData
107///     }
108/// }
109/// ```
110///
111/// There're some limitations:
112///
113/// - The inner field must be named as `inner` (e.g. `inner: P`).
114/// - When no default value is specified, the wrapper type will not implement
115///   the `From` trait.
116/// - Does **NOT** automatically apply `repr(transparent)` attribute, since the
117///   macro doesn't know if other fields were zero-sized types (ZST).
118///
119/// ## Special attributes
120///
121/// ### `Debug` and `DebugName`
122///
123/// We offer `Debug` and `DebugName` attributes to control how the wrapper type
124/// is printed when using the `Debug` trait, instead of `#[derive(Debug)]`.
125///
126/// - `#[wrapper_impl(Debug)]`: transparently implements the `Debug` trait if
127///   the inner type implements it. The debug output is the same as the inner
128///   one.
129/// - `#[wrapper_impl(DebugName)]`: implements the `Debug` trait, but only
130///   prints the name of the wrapper type.
131///
132/// ```rust
133/// # use wrapper_lite::*;
134/// #
135/// wrapper! {
136///     #[wrapper_impl(Debug)]
137///     #[derive(Clone, Copy)]
138///     pub ExampleWrapperDebug<'a, P>(&'a P)
139/// }
140///
141/// wrapper! {
142///     #[wrapper_impl(DebugName)]
143///     #[derive(Clone, Copy)]
144///     pub ExampleWrapperDebugName<'a, P>(&'a P)
145/// }
146///
147/// let data = "Hello".to_string();
148///
149/// // Here we transparently print the debug output of the inner type.
150/// assert_eq!(
151///     format!("{:?}", ExampleWrapperDebug { inner: &data }),
152///     "\"Hello\""
153/// );
154/// // Here we only print the name of the wrapper type.
155/// assert_eq!(
156///     format!("{:?}", ExampleWrapperDebugName { inner: &data }),
157///     "ExampleWrapperDebugName"
158/// );
159/// ```
160///
161/// ## Notice
162///
163/// - The `wrapper_impl` attribute must be on top of any other attributes.
164/// - Should **NOT** implement `Deref` and `DerefMut` at the same time (when
165///   `DerefMut` is implemented, `Deref` would be implemented, too).
166macro_rules! wrapper {
167    // To filter out the `wrapper_impl` attribute and extract the inner type.
168    (
169        @INTERNEL IMPL
170        #[wrapper_impl(AsRef)]
171        $($tt:tt)*
172    ) => {
173        $crate::wrapper! {
174            @INTERNEL IMPL
175            $($tt)*
176        }
177    };
178    (
179        @INTERNEL IMPL
180        #[wrapper_impl(AsMut)]
181        $($tt:tt)*
182    ) => {
183        $crate::wrapper! {
184            @INTERNEL IMPL
185            $($tt)*
186        }
187    };
188    (
189        @INTERNEL IMPL
190        #[wrapper_impl(Borrow)]
191        $($tt:tt)*
192    ) => {
193        $crate::wrapper! {
194            @INTERNEL IMPL
195            $($tt)*
196        }
197    };
198    (
199        @INTERNEL IMPL
200        #[wrapper_impl(Deref)]
201        $($tt:tt)*
202    ) => {
203        $crate::wrapper! {
204            @INTERNEL IMPL
205            $($tt)*
206        }
207    };
208    (
209        @INTERNEL IMPL
210        #[wrapper_impl(DerefMut)]
211        $($tt:tt)*
212    ) => {
213        $crate::wrapper! {
214            @INTERNEL IMPL
215            $($tt)*
216        }
217    };
218    (
219        @INTERNEL IMPL
220        #[wrapper_impl(From)]
221        $($tt:tt)*
222    ) => {
223        $crate::wrapper! {
224            @INTERNEL IMPL
225            $($tt)*
226        }
227    };
228    (
229        @INTERNEL IMPL
230        #[wrapper_impl(Debug)]
231        $($tt:tt)*
232    ) => {
233        $crate::wrapper! {
234            @INTERNEL IMPL
235            $($tt)*
236        }
237    };
238    (
239        @INTERNEL IMPL
240        #[wrapper_impl(DebugName)]
241        $($tt:tt)*
242    ) => {
243        $crate::wrapper! {
244            @INTERNEL IMPL
245            $($tt)*
246        }
247    };
248
249    // The actual implementation of the wrapper type: `pub Name<...>(...)`
250    (
251        @INTERNEL IMPL
252        $(#[$outer:meta])*
253        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? ($inner_vis:vis $inner_ty:ty) $(;)?
254    ) => {
255        $(#[$outer])*
256        #[repr(transparent)]
257        $vis struct $name$(<$($lt),+>)? {
258            /// Inner value
259            $inner_vis inner: $inner_ty,
260        }
261
262        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
263            #[inline(always)]
264            #[doc = concat!("Creates a new instance of [`", stringify!($name), "`]")]
265            $inner_vis const fn const_from(inner: $inner_ty) -> Self {
266                Self {
267                    inner,
268                }
269            }
270        }
271    };
272
273    // The actual implementation of the wrapper type: `pub struct Name<...> { ... }`, with field initial value provided, make `const_from` const.
274    (
275        @INTERNEL IMPL
276        $(#[$outer:meta])*
277        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
278            $(#[$field_inner_meta:meta])*
279            $inner_vis:vis inner: $inner_ty:ty
280            $(
281                ,
282                $(#[$field_meta:meta])*
283                $field_vis:vis $field:ident: $field_ty:ty = $field_default: expr
284            )*
285            $(,)?
286        }
287    ) => {
288        $(#[$outer])*
289        $vis struct $name$(<$($lt),+>)? {
290            $(#[$field_inner_meta])*
291            $inner_vis inner: $inner_ty,
292            $(
293                $(#[$field_meta])*
294                $field_vis $field: $field_ty
295            ),*
296        }
297
298        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
299            #[inline(always)]
300            #[doc = concat!("Creates a new instance of [`", stringify!($name), "`]")]
301            $inner_vis const fn const_from(inner: $inner_ty) -> Self {
302                Self {
303                    inner,
304                    $(
305                        $field: $field_default,
306                    )*
307                }
308            }
309        }
310    };
311
312    // The actual implementation of the wrapper type with fields: `pub struct Name<...> { ... }`
313    (
314        @INTERNEL IMPL
315        $(#[$outer:meta])*
316        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
317            $(#[$field_inner_meta:meta])*
318            $inner_vis:vis inner: $inner_ty:ty
319            $(
320                ,
321                $(#[$field_meta:meta])*
322                $field_vis:vis $field:ident: $field_ty:ty
323            )*
324            $(,)?
325        }
326    ) => {
327        $(#[$outer])*
328        $vis struct $name$(<$($lt),+>)? {
329            $(#[$field_inner_meta])*
330            $inner_vis inner: $inner_ty
331            $(
332                ,
333                $(#[$field_meta])*
334                $field_vis $field: $field_ty
335            )*
336        }
337    };
338
339    // Extract wrapper impl for `AsRef` trait.
340    (
341        @INTERNEL WRAPPER_IMPL
342        #[wrapper_impl(AsRef)]
343        $($tt:tt)*
344    ) => {
345        $crate::wrapper! {
346            @INTERNEL WRAPPER_IMPL_AS_REF
347            $($tt)*
348        }
349
350        $crate::wrapper! {
351            @INTERNEL WRAPPER_IMPL
352            $($tt)*
353        }
354    };
355
356    // Extract wrapper impl for `AsMut` trait.
357    (
358        @INTERNEL WRAPPER_IMPL
359        #[wrapper_impl(AsMut)]
360        $($tt:tt)*
361    ) => {
362        $crate::wrapper! {
363            @INTERNEL WRAPPER_IMPL_AS_MUT
364            $($tt)*
365        }
366
367        $crate::wrapper! {
368            @INTERNEL WRAPPER_IMPL
369            $($tt)*
370        }
371    };
372
373    // Extract wrapper impl for `Borrow` trait.
374    (
375        @INTERNEL WRAPPER_IMPL
376        #[wrapper_impl(Borrow)]
377        $($tt:tt)*
378    ) => {
379        $crate::wrapper! {
380            @INTERNEL WRAPPER_IMPL_BORROW
381            $($tt)*
382        }
383
384        $crate::wrapper! {
385            @INTERNEL WRAPPER_IMPL
386            $($tt)*
387        }
388    };
389
390    // Extract wrapper impl for `Debug` trait.
391    (
392        @INTERNEL WRAPPER_IMPL
393        #[wrapper_impl(Debug)]
394        $($tt:tt)*
395    ) => {
396        $crate::wrapper! {
397            @INTERNEL WRAPPER_IMPL_DEBUG
398            $($tt)*
399        }
400
401        $crate::wrapper! {
402            @INTERNEL WRAPPER_IMPL
403            $($tt)*
404        }
405    };
406
407    // Extract wrapper impl for `Debug` trait  printing its name only.
408    (
409        @INTERNEL WRAPPER_IMPL
410        #[wrapper_impl(DebugName)]
411        $($tt:tt)*
412    ) => {
413        $crate::wrapper! {
414            @INTERNEL WRAPPER_IMPL_DEBUG_NAME
415            $($tt)*
416        }
417
418        $crate::wrapper! {
419            @INTERNEL WRAPPER_IMPL
420            $($tt)*
421        }
422    };
423
424    // Extract wrapper impl for `DerefMut` trait (and `Deref`).
425    (
426        @INTERNEL WRAPPER_IMPL
427        #[wrapper_impl(DerefMut)]
428        $($tt:tt)*
429    ) => {
430        $crate::wrapper! {
431            @INTERNEL WRAPPER_IMPL_DEREF_MUT
432            $($tt)*
433        }
434
435        $crate::wrapper! {
436            @INTERNEL WRAPPER_IMPL
437            $($tt)*
438        }
439    };
440
441    // Extract wrapper impl for `Deref` trait.
442    (
443        @INTERNEL WRAPPER_IMPL
444        #[wrapper_impl(Deref)]
445        $($tt:tt)*
446    ) => {
447        $crate::wrapper! {
448            @INTERNEL WRAPPER_IMPL_DEREF
449            $($tt)*
450        }
451
452        $crate::wrapper! {
453            @INTERNEL WRAPPER_IMPL
454            $($tt)*
455        }
456    };
457
458    // Extract wrapper impl for `From` trait.
459    (
460        @INTERNEL WRAPPER_IMPL
461        #[wrapper_impl(From)]
462        $($tt:tt)*
463    ) => {
464        $crate::wrapper! {
465            @INTERNEL WRAPPER_IMPL_FROM
466            $($tt)*
467        }
468
469        $crate::wrapper! {
470            @INTERNEL WRAPPER_IMPL
471            $($tt)*
472        }
473    };
474
475    // ================ Impl `AsRef` trait for the wrapper type. ================
476    (
477        @INTERNEL WRAPPER_IMPL_AS_REF
478        $(#[$meta:meta])*
479        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
480        ($inner_vis:vis $inner_ty:ty)
481        $($tt:tt)*
482    ) => {
483        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsRef<$inner_ty> for $name$(<$($lt),+>)? {
484            fn as_ref(&self) -> &$inner_ty {
485                &self.inner
486            }
487        }
488
489        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
490            /// Returns a reference to the inner value.
491            #[inline(always)]
492            pub const fn as_inner(&self) -> &$inner_ty {
493                &self.inner
494            }
495        }
496    };
497    (
498        @INTERNEL WRAPPER_IMPL_AS_REF
499        $(#[$meta:meta])*
500        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
501            $(#[$field_inner_meta:meta])*
502            $inner_vis:vis inner: $inner_ty:ty
503            $(
504                ,
505                $(#[$field_meta:meta])*
506                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
507            )*
508            $(,)?
509        }
510    ) => {
511        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsRef<$inner_ty> for $name$(<$($lt),+>)? {
512            fn as_ref(&self) -> &$inner_ty {
513                &self.inner
514            }
515        }
516
517        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
518            /// Returns a reference to the inner value.
519            #[inline(always)]
520            pub const fn as_inner(&self) -> &$inner_ty {
521                &self.inner
522            }
523        }
524    };
525    // ================ Impl `AsRef` trait for the wrapper type. ================
526
527
528    // ================ Impl `AsMut` trait for the wrapper type. ================
529    (
530        @INTERNEL WRAPPER_IMPL_AS_MUT
531        $(#[$meta:meta])*
532        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
533        ($inner_vis:vis $inner_ty:ty)
534        $($tt:tt)*
535    ) => {
536        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsMut<$inner_ty> for $name$(<$($lt),+>)? {
537            fn as_mut(&mut self) -> &mut $inner_ty {
538                &mut self.inner
539            }
540        }
541
542        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
543            /// Returns a reference to the inner value.
544            #[inline(always)]
545            pub const fn as_inner_mut(&mut self) -> &mut $inner_ty {
546                &mut self.inner
547            }
548        }
549    };
550    (
551        @INTERNEL WRAPPER_IMPL_AS_MUT
552        $(#[$meta:meta])*
553        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
554            $(#[$field_inner_meta:meta])*
555            $inner_vis:vis inner: $inner_ty:ty
556            $(
557                ,
558                $(#[$field_meta:meta])*
559                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
560            )*
561            $(,)?
562        }
563    ) => {
564        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::AsMut<$inner_ty> for $name$(<$($lt),+>)? {
565            fn as_mut(&mut self) -> &mut $inner_ty {
566                &mut self.inner
567            }
568        }
569
570        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
571            /// Returns a reference to the inner value.
572            #[cfg_attr(feature = "const-mut-method", rustversion::attr(since(1.83), const))]
573            #[inline(always)]
574            pub fn as_inner_mut(&mut self) -> &mut $inner_ty {
575                &mut self.inner
576            }
577        }
578    };
579    // ================ Impl `AsMut` trait for the wrapper type. ================
580
581    // ================ Impl `Borrow` trait for the wrapper type. ================
582    (
583        @INTERNEL WRAPPER_IMPL_BORROW
584        $(#[$meta:meta])*
585        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
586        ($inner_vis:vis $inner_ty:ty)
587        $($tt:tt)*
588    ) => {
589        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::borrow::Borrow<$inner_ty> for $name$(<$($lt),+>)? {
590            fn borrow(&self) -> &$inner_ty {
591                &self.inner
592            }
593        }
594    };
595    (
596        @INTERNEL WRAPPER_IMPL_BORROW
597        $(#[$meta:meta])*
598        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
599            $(#[$field_inner_meta:meta])*
600            $inner_vis:vis inner: $inner_ty:ty
601            $(
602                ,
603                $(#[$field_meta:meta])*
604                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
605            )*
606            $(,)?
607        }
608    ) => {
609        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::borrow::Borrow<$inner_ty> for $name$(<$($lt),+>)? {
610            fn borrow(&self) -> &$inner_ty {
611                &self.inner
612            }
613        }
614    };
615    // ================ Impl `Borrow` trait for the wrapper type. ================
616
617    // ================ Impl `Debug` trait for the wrapper type. ================
618    (
619        @INTERNEL WRAPPER_IMPL_DEBUG
620        $(#[$meta:meta])*
621        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
622        ($inner_vis:vis $inner_ty:ty)
623        $($tt:tt)*
624    ) => {
625        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
626        where
627            $inner_ty: ::core::fmt::Debug,
628        {
629            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
630                self.inner.fmt(f)
631            }
632        }
633    };
634    (
635        @INTERNEL WRAPPER_IMPL_DEBUG
636        $(#[$meta:meta])*
637        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
638            $(#[$field_inner_meta:meta])*
639            $inner_vis:vis inner: $inner_ty:ty
640            $(
641                ,
642                $(#[$field_meta:meta])*
643                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
644            )*
645            $(,)?
646        }
647    ) => {
648        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)?
649        where
650            $inner_ty: ::core::fmt::Debug,
651        {
652            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
653                self.inner.fmt(f)
654            }
655        }
656    };
657    // ================ Impl `Debug` trait for the wrapper type. ================
658
659    // ================ Impl `DebugName` trait for the wrapper type. ================
660    (
661        @INTERNEL WRAPPER_IMPL_DEBUG_NAME
662        $(#[$meta:meta])*
663        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
664        ($inner_vis:vis $inner_ty:ty)
665        $($tt:tt)*
666    ) => {
667        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)? {
668            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
669                f.debug_struct(stringify!($name)).finish()
670            }
671        }
672    };
673    (
674        @INTERNEL WRAPPER_IMPL_DEBUG_NAME
675        $(#[$meta:meta])*
676        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
677            $(#[$field_inner_meta:meta])*
678            $inner_vis:vis inner: $inner_ty:ty
679            $(
680                ,
681                $(#[$field_meta:meta])*
682                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
683            )*
684            $(,)?
685        }
686    ) => {
687        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::fmt::Debug for $name$(<$($lt),+>)? {
688            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
689                f.debug_struct(stringify!($name)).finish()
690            }
691        }
692    };
693    // ================ Impl `DebugName` trait for the wrapper type. ================
694
695    // ================ Impl `DerefMut` traits for the wrapper type. ================
696    (
697        @INTERNEL WRAPPER_IMPL_DEREF_MUT
698        $(#[$meta:meta])*
699        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
700        ($inner_vis:vis $inner_ty:ty)
701        $($tt:tt)*
702    ) => {
703        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)? {
704            type Target = $inner_ty;
705
706            fn deref(&self) -> &Self::Target {
707                &self.inner
708            }
709        }
710
711        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::DerefMut for $name$(<$($lt),+>)? {
712            fn deref_mut(&mut self) -> &mut Self::Target {
713                &mut self.inner
714            }
715        }
716    };
717    (
718        @INTERNEL WRAPPER_IMPL_DEREF_MUT
719        $(#[$meta:meta])*
720        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
721            $(#[$field_inner_meta:meta])*
722            $inner_vis:vis inner: $inner_ty:ty
723            $(
724                ,
725                $(#[$field_meta:meta])*
726                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
727            )*
728            $(,)?
729        }
730    ) => {
731        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)? {
732            type Target = $inner_ty;
733
734            fn deref(&self) -> &Self::Target {
735                &self.inner
736            }
737        }
738
739        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::DerefMut for $name$(<$($lt),+>)? {
740            fn deref_mut(&mut self) -> &mut Self::Target {
741                &mut self.inner
742            }
743        }
744    };
745    // ================ Impl `DerefMut` traits for the wrapper type. ================
746
747    // ================ Impl `Deref` trait for the wrapper type. ================
748    (
749        @INTERNEL WRAPPER_IMPL_DEREF
750        $(#[$meta:meta])*
751        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
752        ($inner_vis:vis $inner_ty:ty)
753        $($tt:tt)*
754    ) => {
755        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)? {
756            type Target = $inner_ty;
757
758            fn deref(&self) -> &Self::Target {
759                &self.inner
760            }
761        }
762    };
763    (
764        @INTERNEL WRAPPER_IMPL_DEREF
765        $(#[$meta:meta])*
766        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
767            $(#[$field_inner_meta:meta])*
768            $inner_vis:vis inner: $inner_ty:ty
769            $(
770                ,
771                $(#[$field_meta:meta])*
772                $field_vis:vis $field:ident: $field_ty:ty$( = $field_default: expr)?
773            )*
774            $(,)?
775        }
776    ) => {
777        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::ops::Deref for $name$(<$($lt),+>)? {
778            type Target = $inner_ty;
779
780            fn deref(&self) -> &Self::Target {
781                &self.inner
782            }
783        }
784    };
785    // ================ Impl `Deref` trait for the wrapper type. ================
786
787    // ================ Impl `From` trait for the wrapper type. ================
788    (
789        @INTERNEL WRAPPER_IMPL_FROM
790        $(#[$meta:meta])*
791        $vis:vis $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)?
792        ($inner_vis:vis $inner_ty:ty)
793        $($tt:tt)*
794    ) => {
795        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::From<$inner_ty> for $name$(<$($lt),+>)? {
796            fn from(inner: $inner_ty) -> Self {
797                Self::const_from(inner)
798            }
799        }
800
801        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
802            /// Creates a new instance of the wrapper type from the inner value.
803            #[allow(unreachable_pub)]
804            #[inline(always)]
805            pub const fn from(inner: $inner_ty) -> Self {
806                Self::const_from(inner)
807            }
808        }
809    };
810    (
811        @INTERNEL WRAPPER_IMPL_FROM
812        $(#[$meta:meta])*
813        $vis:vis struct $name:ident$(<$($lt:tt$(:$clt:tt$(+$dlt:tt)*)?),+>)? {
814            $(#[$field_inner_meta:meta])*
815            $inner_vis:vis inner: $inner_ty:ty
816            $(
817                ,
818                $(#[$field_meta:meta])*
819                $field_vis:vis $field:ident: $field_ty:ty = $field_default:expr
820            )*
821            $(,)?
822        }
823    ) => {
824        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? ::core::convert::From<$inner_ty> for $name$(<$($lt),+>)? {
825            fn from(inner: $inner_ty) -> Self {
826                Self::const_from(inner)
827            }
828        }
829
830        impl$(<$($lt$(:$clt$(+$dlt)*)?),+>)? $name$(<$($lt),+>)? {
831            /// Creates a new instance of the wrapper type from the inner value.
832            #[allow(unreachable_pub)]
833            #[inline(always)]
834            pub const fn from(inner: $inner_ty) -> Self {
835                Self::const_from(inner)
836            }
837        }
838    };
839    (@INTERNEL WRAPPER_IMPL_FROM $($tt:tt)*) => {};
840    // ================ Impl `From` trait for the wrapper type. ================
841
842    // No other wrapper_impl meta
843    (@INTERNEL WRAPPER_IMPL $($tt:tt)*) => {};
844
845    // Catch-all for invalid usage of the macro.
846    (@INTERNEL $($tt:tt)*) => {
847        compile_error!(
848            "Invalid usage of `wrapper!` macro. @INTERNEL \
849            Please refer to the documentation for the correct syntax."
850        );
851    };
852
853    // Core macro for the wrapper type.
854    ($($tt:tt)*) => {
855        $crate::wrapper!(@INTERNEL IMPL $($tt)*);
856        $crate::wrapper!(@INTERNEL WRAPPER_IMPL $($tt)*);
857    };
858}