scsys_core/macros/
wrapper.rs

1/*
2    Appellation: wrapper <module>
3    Contrib: @FL03
4*/
5
6#[deprecated(since = "0.3.2", note = "use `contained::fmt_wrapper` instead")]
7///
8///
9/// ### Example
10///
11/// ```rust
12/// use scsys_core::fmt_wrapper;
13///
14/// pub struct Sample<T>(pub T);
15///
16/// fmt_wrapper!(Sample<Q>(Binary, Debug, Display, LowerHex, UpperHex, LowerExp, UpperExp, Pointer));
17/// ```
18#[macro_export]
19macro_rules! fmt_wrapper {
20    ($s:ident<$T:ident> ($($trait:ident),* $(,)?)) => {
21        $(
22            $crate::fmt_wrapper!(@impl $s::<$T>::$trait);
23        )*
24    };
25    ($s:ident<$T:ident>.$field:ident {$($trait:ident),* $(,)?}) => {
26        $(
27            $crate::fmt_wrapper!(@impl $s::<$T>::$trait.$field);
28        )*
29    };
30
31    (@impl $s:ident::<$T:ident>::$trait:ident) => {
32        $crate::fmt_wrapper!(@impl #[field(0)] $s::<$T>::$trait);
33    };
34    (@impl #[field($field:tt)] $s:ident::<$T:ident>::$trait:ident) => {
35        impl<$T> ::core::fmt::$trait for $s<$T>
36        where
37        $T: ::core::fmt::$trait
38        {
39            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
40                ::core::fmt::$trait::fmt(
41                    &self.$field,
42                    f,
43                )
44            }
45        }
46    };
47}
48
49#[macro_export]
50macro_rules! wrapper {
51    ($($S:ident($vis:vis $T:ident) $(where $($rest:tt)*)?;),* $(,)?) => {
52        $(
53            $crate::wrapper!(@impl $S($vis $T) $(where $($rest)*)?;);
54        )*
55    };
56    (@impl
57        #[derive($($derive:ident),*)]
58        $S:ident($vis:vis $T:ident) $(where $($rest:tt)*)?;
59    ) => {
60        #[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd, $($derive),*)]
61        #[cfg_attr(
62            feature = "serde",
63            derive(serde::Deserialize, serde::Serialize),
64            serde(default, transparent),
65        )]
66        #[repr(transparent)]
67        pub struct $S<$T>($vis $T) $(where $($rest)*)?;
68
69        impl<$T> $S<$T> {
70            /// returns a new instance initialized with the default value
71            pub fn new() -> Self
72            where
73                $T: Default,
74            {
75                Self($T::default())
76            }
77            /// returns a new instance with the given value
78            pub fn from_value(value: $T) -> Self {
79                Self(value)
80            }
81            /// returns an immutable reference to the inner value
82            pub const fn get(&self) -> &$T {
83                &self.0
84            }
85            /// returns a mutable reference to the inner value
86            pub const fn get_mut(&mut self) -> &mut $T {
87                &mut self.0
88            }
89            /// consumes the current instance to return the inner value
90            pub fn into_inner(self) -> $T {
91                self.0
92            }
93            /// applies the given function to the inner value and returns a new instance with
94            /// the result
95            pub fn map<R, F>(self, f: F) -> $S<R>
96            where
97                F: FnOnce($T) -> R,
98            {
99                $S(f(self.0))
100            }
101            /// uses the [`replace`](core::mem::replace) method to update and return the inner value
102            pub fn replace(&mut self, value: $T) -> $T {
103                core::mem::replace(self.get_mut(), value)
104            }
105            /// update the innerstate before returing a mutable reference to the wrapper
106            pub fn set(&mut self, value: $T) -> &mut Self {
107                *self.get_mut() = value;
108                self
109            }
110            /// uses the [`take`](core::mem::take) method to replace the inner value with the default
111            /// value to return its previous value
112            pub fn take(&mut self) -> $T
113            where
114                $T: Default,
115            {
116                core::mem::take(self.get_mut())
117            }
118            /// consumes the current instance to create another with the given value
119            pub fn with(self, value: $T) -> Self {
120                Self(value)
121            }
122            /// captures a referenced value in a new instance
123            pub fn view(&self) -> $S<&$T> {
124                $S(self.get())
125            }
126            /// captures a mutable reference to the inner value
127            pub fn view_mut(&mut self) -> $S<&mut $T> {
128                $S(self.get_mut())
129            }
130        }
131
132        impl<$T> AsRef<$T> for $S<$T> {
133            fn as_ref(&self) -> &$T {
134                self.get()
135            }
136        }
137
138        impl<$T> AsMut<$T> for $S<$T> {
139            fn as_mut(&mut self) -> &mut $T {
140                self.get_mut()
141            }
142        }
143
144        impl<$T> ::core::borrow::Borrow<$T> for $S<$T> {
145            fn borrow(&self) -> &$T {
146                self.get()
147            }
148        }
149
150        impl<$T> ::core::borrow::BorrowMut<$T> for $S<$T> {
151            fn borrow_mut(&mut self) -> &mut $T {
152                self.get_mut()
153            }
154        }
155
156        impl<$T> ::core::ops::Deref for $S<$T> {
157            type Target = $T;
158
159            fn deref(&self) -> &Self::Target {
160                self.get()
161            }
162        }
163
164        impl<$T> ::core::ops::DerefMut for $S<$T> {
165            fn deref_mut(&mut self) -> &mut Self::Target {
166                self.get_mut()
167            }
168        }
169
170        impl<$T> From<$T> for $S<$T> {
171            fn from(value: $T) -> Self {
172                Self(value)
173            }
174        }
175    };
176}