1#![no_std]
4
5#[macro_export]
6macro_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]
58macro_rules! wrapper {
167 (
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 (
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_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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 (
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 #[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 #[inline(always)]
520 pub const fn as_inner(&self) -> &$inner_ty {
521 &self.inner
522 }
523 }
524 };
525 (
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 #[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 #[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 (
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 (
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 (
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 (
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 (
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 (
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 #[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 #[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 (@INTERNEL WRAPPER_IMPL $($tt:tt)*) => {};
844
845 (@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 ($($tt:tt)*) => {
855 $crate::wrapper!(@INTERNEL IMPL $($tt)*);
856 $crate::wrapper!(@INTERNEL WRAPPER_IMPL $($tt)*);
857 };
858}