1#[macro_export]
6macro_rules! impl_debug {
7 ($ty:ty) => {
8 impl core::fmt::Debug for $ty {
9 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
10 f.write_str(core::any::type_name::<Self>())
11 }
12 }
13 };
14}
15
16#[macro_export]
32macro_rules! raw_view {
33 ($ty:ty, $axis:expr) => {
35 impl $crate::NativeView for $ty {
36 fn stretch_axis(&self) -> $crate::layout::StretchAxis {
37 $axis
38 }
39 }
40
41 impl $crate::View for $ty {
42 fn body(self, _env: &$crate::Environment) -> impl $crate::View {
43 $crate::Native::new(self)
44 }
45
46 fn stretch_axis(&self) -> $crate::layout::StretchAxis {
47 $axis
48 }
49 }
50 };
51
52 ($ty:ty) => {
54 impl $crate::NativeView for $ty {}
55
56 impl $crate::View for $ty {
57 fn body(self, _env: &$crate::Environment) -> impl $crate::View {
58 $crate::Native::new(self)
59 }
60
61 fn stretch_axis(&self) -> $crate::layout::StretchAxis {
62 $crate::layout::StretchAxis::None
63 }
64 }
65 };
66}
67
68#[macro_export]
90macro_rules! configurable {
91 (@impl $(#[$meta:meta])*; $view:ident, $config:ty, $axis:expr) => {
93 $(#[$meta])*
94 pub struct $view($config);
95
96 impl $crate::NativeView for $config {
97 fn stretch_axis(&self) -> $crate::layout::StretchAxis {
98 $axis
99 }
100 }
101
102 impl $crate::view::ConfigurableView for $view {
103 type Config = $config;
104 #[inline] fn config(self) -> Self::Config { self.0 }
105 }
106
107 impl $crate::view::ViewConfiguration for $config {
108 type View = $view;
109 #[inline] fn render(self) -> Self::View { $view(self) }
110 }
111
112 impl From<$config> for $view {
113 #[inline] fn from(value: $config) -> Self { Self(value) }
114 }
115
116 impl $crate::view::View for $view {
117 fn body(self, env: &$crate::Environment) -> impl $crate::View {
118 use $crate::view::ConfigurableView;
119 let config = self.config();
120 if let Some(hook) = env.get::<$crate::view::Hook<$config>>() {
121 $crate::AnyView::new(hook.apply(env, config))
122 } else {
123 $crate::AnyView::new($crate::Native::new(config))
124 }
125 }
126
127 fn stretch_axis(&self) -> $crate::layout::StretchAxis {
128 $crate::NativeView::stretch_axis(&self.0)
129 }
130 }
131 };
132
133 (@impl_dynamic $(#[$meta:meta])*; $view:ident, $config:ty, $stretch_fn:expr) => {
136 $(#[$meta])*
137 #[derive(Debug)]
138 pub struct $view($config);
139
140 impl $crate::NativeView for $config {
141 fn stretch_axis(&self) -> $crate::layout::StretchAxis {
142 ($stretch_fn)(self)
143 }
144 }
145
146 impl $crate::view::ConfigurableView for $view {
147 type Config = $config;
148 #[inline] fn config(self) -> Self::Config { self.0 }
149 }
150
151 impl $crate::view::ViewConfiguration for $config {
152 type View = $view;
153 #[inline] fn render(self) -> Self::View { $view(self) }
154 }
155
156 impl From<$config> for $view {
157 #[inline] fn from(value: $config) -> Self { Self(value) }
158 }
159
160 impl $crate::view::View for $view {
161 fn body(self, env: &$crate::Environment) -> impl $crate::View {
162 use $crate::view::ConfigurableView;
163 let config = self.config();
164 if let Some(hook) = env.get::<$crate::view::Hook<$config>>() {
165 $crate::AnyView::new(hook.apply(env, config))
166 } else {
167 $crate::AnyView::new($crate::Native::new(config))
168 }
169 }
170
171 fn stretch_axis(&self) -> $crate::layout::StretchAxis {
172 $crate::NativeView::stretch_axis(&self.0)
173 }
174 }
175 };
176
177 ($(#[$meta:meta])* $view:ident, $config:ty, |$param:ident| $body:expr) => {
180 $crate::configurable!(@impl_dynamic $(#[$meta])*; $view, $config, |$param: &$config| $body);
181 };
182
183 ($(#[$meta:meta])* $view:ident, $config:ty, $axis:expr) => {
185 $crate::configurable!(@impl $(#[$meta])*; $view, $config, $axis);
186 };
187
188 ($(#[$meta:meta])* $view:ident, $config:ty) => {
190 $crate::configurable!(@impl $(#[$meta])*; $view, $config, $crate::layout::StretchAxis::None);
191 };
192}
193macro_rules! tuples {
194 ($macro:ident) => {
195 $macro!();
196 $macro!(T0);
197 $macro!(T0, T1);
198 $macro!(T0, T1, T2);
199 $macro!(T0, T1, T2, T3);
200 $macro!(T0, T1, T2, T3, T4);
201 $macro!(T0, T1, T2, T3, T4, T5);
202 $macro!(T0, T1, T2, T3, T4, T5, T6);
203 $macro!(T0, T1, T2, T3, T4, T5, T6, T7);
204 $macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8);
205 $macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);
206 $macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
207 $macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
208 $macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
209 $macro!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
210 $macro!(
211 T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14
212 );
213 };
214}
215
216#[macro_export]
221macro_rules! impl_extractor {
222 ($ty:ty) => {
223 impl $crate::extract::Extractor for $ty {
224 fn extract(env: &$crate::Environment) -> core::result::Result<Self, $crate::Error> {
225 $crate::extract::Extractor::extract(env)
226 .map(|value: $crate::extract::Use<$ty>| value.0)
227 }
228 }
229 };
230}
231
232#[macro_export]
237macro_rules! impl_deref {
238 ($ty:ty,$target:ty) => {
239 impl core::ops::Deref for $ty {
240 type Target = $target;
241 fn deref(&self) -> &Self::Target {
242 &self.0
243 }
244 }
245
246 impl core::ops::DerefMut for $ty {
247 fn deref_mut(&mut self) -> &mut Self::Target {
248 &mut self.0
249 }
250 }
251 };
252}