scsys_core/macros/
wrapper.rs

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