dioxus_core/
properties.rs1use std::{any::TypeId, 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#[warnings::warning]
119pub(crate) fn component_called_as_function<C: ComponentFunction<P, M>, P, M>(_: C) {
120 let type_name = std::any::type_name::<C>();
122 let component_name = Runtime::with(|rt| {
123 current_scope_id()
124 .ok()
125 .and_then(|id| rt.get_state(id).map(|scope| scope.name))
126 })
127 .ok()
128 .flatten();
129
130 if component_name == Some(type_name) {
132 return;
133 }
134
135 tracing::error!("It looks like you called the component {type_name} like a function instead of a component. Components should be called with braces like `{type_name} {{ prop: value }}` instead of as a function");
137}
138
139#[doc(hidden)]
141#[allow(clippy::no_effect)]
142pub fn verify_component_called_as_component<C: ComponentFunction<P, M>, P, M>(component: C) {
143 component_called_as_function(component);
144}
145
146#[rustversion::attr(
161 since(1.78.0),
162 diagnostic::on_unimplemented(
163 message = "`Component<{Props}>` is not implemented for `{Self}`",
164 label = "Component",
165 note = "Components are functions in the form `fn() -> Element`, `fn(props: Properties) -> Element`, or `#[component] fn(partial_eq1: u32, partial_eq2: u32) -> Element`.",
166 note = "You may have forgotten to add `#[component]` to your function to automatically implement the `ComponentFunction` trait."
167 )
168)]
169pub trait ComponentFunction<Props, Marker = ()>: Clone + 'static {
170 fn id(&self) -> TypeId {
172 TypeId::of::<Self>()
173 }
174
175 fn rebuild(&self, props: Props) -> Element;
177}
178
179impl<F: Fn(P) -> Element + Clone + 'static, P> ComponentFunction<P> for F {
181 fn rebuild(&self, props: P) -> Element {
182 self(props)
183 }
184}
185
186pub struct EmptyMarker;
188impl<F: Fn() -> Element + Clone + 'static> ComponentFunction<(), EmptyMarker> for F {
189 fn rebuild(&self, _: ()) -> Element {
190 self()
191 }
192}
193
194pub trait SuperInto<O, M = ()> {
196 fn super_into(self) -> O;
198}
199
200impl<T, O, M> SuperInto<O, M> for T
201where
202 O: SuperFrom<T, M>,
203{
204 fn super_into(self) -> O {
205 O::super_from(self)
206 }
207}
208
209pub trait SuperFrom<T, M = ()> {
211 fn super_from(_: T) -> Self;
213}
214
215impl<T, O> SuperFrom<T, ()> for O
217where
218 O: From<T>,
219{
220 fn super_from(input: T) -> Self {
221 Self::from(input)
222 }
223}
224
225#[doc(hidden)]
226pub struct OptionStringFromMarker;
227
228impl<'a> SuperFrom<&'a str, OptionStringFromMarker> for Option<String> {
229 fn super_from(input: &'a str) -> Self {
230 Some(String::from(input))
231 }
232}
233
234#[doc(hidden)]
235pub struct OptionArgumentsFromMarker;
236
237impl<'a> SuperFrom<Arguments<'a>, OptionArgumentsFromMarker> for Option<String> {
238 fn super_from(input: Arguments<'a>) -> Self {
239 Some(input.to_string())
240 }
241}
242
243#[doc(hidden)]
244pub struct OptionCallbackMarker<T>(std::marker::PhantomData<T>);
245
246impl<
248 Function: FnMut(Args) -> Spawn + 'static,
249 Args: 'static,
250 Spawn: SpawnIfAsync<Marker, Ret> + 'static,
251 Ret: 'static,
252 Marker,
253 > SuperFrom<Function, OptionCallbackMarker<Marker>> for Option<Callback<Args, Ret>>
254{
255 fn super_from(input: Function) -> Self {
256 Some(Callback::new(input))
257 }
258}
259
260#[test]
261#[allow(unused)]
262fn optional_callback_compiles() {
263 fn compiles() {
264 let callback: Callback<i32, i32> = (|num| num * num).super_into();
266 let callback: Callback<i32, ()> = (|num| async move { println!("{num}") }).super_into();
267
268 let optional: Option<Callback<i32, i32>> = (|num| num * num).super_into();
270 let optional: Option<Callback<i32, ()>> =
271 (|num| async move { println!("{num}") }).super_into();
272 }
273}
274
275#[test]
276#[allow(unused)]
277fn from_props_compiles() {
278 let option: i32 = 0i32.super_into();
280 let option: i32 = 0.super_into(); let option: i128 = 0.super_into();
282 let option: &'static str = "hello world".super_into();
283
284 let option: i64 = 0i32.super_into();
286 let option: String = "hello world".super_into();
287
288 let option: Option<i32> = 0i32.super_into();
290 let option: Option<i32> = 0.super_into();
291 let option: Option<i128> = 0.super_into();
292 fn takes_option_string<M>(_: impl SuperInto<Option<String>, M>) {}
293 takes_option_string("hello world");
294 takes_option_string("hello world".to_string());
295}