scsys_core/macros/
wrapper.rs1#[deprecated(since = "0.3.2", note = "use `contained::fmt_wrapper` instead")]
7#[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 pub fn new() -> Self
72 where
73 $T: Default,
74 {
75 Self($T::default())
76 }
77 pub fn from_value(value: $T) -> Self {
79 Self(value)
80 }
81 pub const fn get(&self) -> &$T {
83 &self.0
84 }
85 pub const fn get_mut(&mut self) -> &mut $T {
87 &mut self.0
88 }
89 pub fn into_inner(self) -> $T {
91 self.0
92 }
93 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 pub fn replace(&mut self, value: $T) -> $T {
103 core::mem::replace(self.get_mut(), value)
104 }
105 pub fn set(&mut self, value: $T) -> &mut Self {
107 *self.get_mut() = value;
108 self
109 }
110 pub fn take(&mut self) -> $T
113 where
114 $T: Default,
115 {
116 core::mem::take(self.get_mut())
117 }
118 pub fn with(self, value: $T) -> Self {
120 Self(value)
121 }
122 pub fn view(&self) -> $S<&$T> {
124 $S(self.get())
125 }
126 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}