dioxus_core/
properties.rs1use std::fmt::Arguments;
2
3use crate::innerlude::*;
4
5#[rustversion::attr(
40 since(1.78.0),
41 diagnostic::on_unimplemented(
42 message = "`Props` is not implemented for `{Self}`",
43 label = "Props",
44 note = "Props is a trait that is automatically implemented for all structs that can be used as props for a component",
45 note = "If you manually created a new properties struct, you may have forgotten to add `#[derive(Props, PartialEq, Clone)]` to your struct",
46 )
47)]
48pub trait Properties: Clone + Sized + 'static {
49 type Builder;
52
53 fn builder() -> Self::Builder;
55
56 fn memoize(&mut self, other: &Self) -> bool;
58
59 fn into_vcomponent<M: 'static>(self, render_fn: impl ComponentFunction<Self, M>) -> VComponent {
61 let type_name = std::any::type_name_of_val(&render_fn);
62 VComponent::new(render_fn, self, type_name)
63 }
64}
65
66impl Properties for () {
67 type Builder = EmptyBuilder;
68 fn builder() -> Self::Builder {
69 EmptyBuilder {}
70 }
71 fn memoize(&mut self, _other: &Self) -> bool {
72 true
73 }
74}
75
76pub(crate) struct RootProps<P>(pub P);
78
79impl<P> Clone for RootProps<P>
80where
81 P: Clone,
82{
83 fn clone(&self) -> Self {
84 Self(self.0.clone())
85 }
86}
87
88impl<P> Properties for RootProps<P>
89where
90 P: Clone + 'static,
91{
92 type Builder = P;
93 fn builder() -> Self::Builder {
94 unreachable!("Root props technically are never built")
95 }
96 fn memoize(&mut self, _other: &Self) -> bool {
97 true
98 }
99}
100
101pub struct EmptyBuilder;
104impl EmptyBuilder {
105 pub fn build(self) {}
106}
107
108pub fn fc_to_builder<P, M>(_: impl ComponentFunction<P, M>) -> <P as Properties>::Builder
111where
112 P: Properties,
113{
114 P::builder()
115}
116
117#[rustversion::attr(
132 since(1.78.0),
133 diagnostic::on_unimplemented(
134 message = "`Component<{Props}>` is not implemented for `{Self}`",
135 label = "Component",
136 note = "Components are functions in the form `fn() -> Element`, `fn(props: Properties) -> Element`, or `#[component] fn(partial_eq1: u32, partial_eq2: u32) -> Element`.",
137 note = "You may have forgotten to add `#[component]` to your function to automatically implement the `ComponentFunction` trait."
138 )
139)]
140pub trait ComponentFunction<Props, Marker = ()>: Clone + 'static {
141 fn fn_ptr(&self) -> usize;
143
144 fn rebuild(&self, props: Props) -> Element;
146}
147
148impl<F, P> ComponentFunction<P> for F
150where
151 F: Fn(P) -> Element + Clone + 'static,
152{
153 fn rebuild(&self, props: P) -> Element {
154 subsecond::HotFn::current(self.clone()).call((props,))
155 }
156
157 fn fn_ptr(&self) -> usize {
158 subsecond::HotFn::current(self.clone()).ptr_address().0 as usize
159 }
160}
161
162pub struct EmptyMarker;
164impl<F> ComponentFunction<(), EmptyMarker> for F
165where
166 F: Fn() -> Element + Clone + 'static,
167{
168 fn rebuild(&self, props: ()) -> Element {
169 subsecond::HotFn::current(self.clone()).call(props)
170 }
171
172 fn fn_ptr(&self) -> usize {
173 subsecond::HotFn::current(self.clone()).ptr_address().0 as usize
174 }
175}
176
177pub trait SuperInto<O, M = ()> {
179 fn super_into(self) -> O;
181}
182
183impl<T, O, M> SuperInto<O, M> for T
184where
185 O: SuperFrom<T, M>,
186{
187 fn super_into(self) -> O {
188 O::super_from(self)
189 }
190}
191
192pub trait SuperFrom<T, M = ()> {
194 fn super_from(_: T) -> Self;
196}
197
198impl<T, O> SuperFrom<T, ()> for O
200where
201 O: From<T>,
202{
203 fn super_from(input: T) -> Self {
204 Self::from(input)
205 }
206}
207
208#[doc(hidden)]
209pub struct OptionStringFromMarker;
210
211impl<'a> SuperFrom<&'a str, OptionStringFromMarker> for Option<String> {
212 fn super_from(input: &'a str) -> Self {
213 Some(String::from(input))
214 }
215}
216
217#[doc(hidden)]
218pub struct OptionArgumentsFromMarker;
219
220impl<'a> SuperFrom<Arguments<'a>, OptionArgumentsFromMarker> for Option<String> {
221 fn super_from(input: Arguments<'a>) -> Self {
222 Some(input.to_string())
223 }
224}
225
226#[doc(hidden)]
227pub struct OptionCallbackMarker<T>(std::marker::PhantomData<T>);
228
229impl<
231 Function: FnMut(Args) -> Spawn + 'static,
232 Args: 'static,
233 Spawn: SpawnIfAsync<Marker, Ret> + 'static,
234 Ret: 'static,
235 Marker,
236 > SuperFrom<Function, OptionCallbackMarker<Marker>> for Option<Callback<Args, Ret>>
237{
238 fn super_from(input: Function) -> Self {
239 Some(Callback::new(input))
240 }
241}
242
243#[test]
244#[allow(unused)]
245fn optional_callback_compiles() {
246 fn compiles() {
247 let callback: Callback<i32, i32> = (|num| num * num).super_into();
249 let callback: Callback<i32, ()> = (|num| async move { println!("{num}") }).super_into();
250
251 let optional: Option<Callback<i32, i32>> = (|num| num * num).super_into();
253 let optional: Option<Callback<i32, ()>> =
254 (|num| async move { println!("{num}") }).super_into();
255 }
256}
257
258#[test]
259#[allow(unused)]
260fn from_props_compiles() {
261 let option: i32 = 0i32.super_into();
263 let option: i32 = 0.super_into(); let option: i128 = 0.super_into();
265 let option: &'static str = "hello world".super_into();
266
267 let option: i64 = 0i32.super_into();
269 let option: String = "hello world".super_into();
270
271 let option: Option<i32> = 0i32.super_into();
273 let option: Option<i32> = 0.super_into();
274 let option: Option<i128> = 0.super_into();
275 fn takes_option_string<M>(_: impl SuperInto<Option<String>, M>) {}
276 takes_option_string("hello world");
277 takes_option_string("hello world".to_string());
278}