scsys_core/macros/
wrapper.rs1#[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 pub fn new() -> Self
44 where
45 $T: Default,
46 {
47 Self($T::default())
48 }
49 pub fn from_value(value: $T) -> Self {
51 Self(value)
52 }
53 pub const fn get(&self) -> &$T {
55 &self.0
56 }
57 pub const fn get_mut(&mut self) -> &mut $T {
59 &mut self.0
60 }
61 pub fn into_inner(self) -> $T {
63 self.0
64 }
65 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 pub fn replace(&mut self, value: $T) -> $T {
75 core::mem::replace(self.get_mut(), value)
76 }
77 pub fn set(&mut self, value: $T) -> &mut Self {
79 *self.get_mut() = value;
80 self
81 }
82 pub fn take(&mut self) -> $T
85 where
86 $T: Default,
87 {
88 core::mem::take(self.get_mut())
89 }
90 pub fn with(self, value: $T) -> Self {
92 Self(value)
93 }
94 pub fn view(&self) -> $S<&$T> {
96 $S(self.get())
97 }
98 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}