react/
component.rs

1pub trait PropsBuilder<T> {
2    fn build(self) -> T;
3}
4
5pub trait Props {
6    type InitialBuilder;
7
8    fn init_builder() -> Self::InitialBuilder;
9}
10
11pub struct NoProps;
12
13impl PropsBuilder<NoProps> for NoProps {
14    #[inline]
15    fn build(self) -> NoProps {
16        self
17    }
18}
19
20impl Props for NoProps {
21    type InitialBuilder = NoProps;
22
23    #[inline]
24    fn init_builder() -> Self::InitialBuilder {
25        NoProps
26    }
27}
28
29pub trait UseRender {
30    type RenderArg: Props;
31    /// This allows implementor type to specify
32    /// a custom type as the return type of the methods.
33    ///
34    /// With this associated type param, some special components
35    /// can return special elements with special traits.
36    /// For example, [`table`](crate::html::table) component
37    /// returns
38    type RenderOutput;
39    fn use_render(&self, props: &Self::RenderArg) -> Self::RenderOutput;
40}
41
42pub trait Component {
43    type Props: Props;
44    /// Output of `create_element`.
45    /// Many components may return `Option<Element>` in [`UseRender::use_render`]
46    /// while return `Element` in create_element.
47    /// Thus this type parameter exists.
48    type Element;
49
50    fn create_element(self, props: Self::Props, key: Option<crate::Key>) -> Self::Element;
51}
52
53pub trait UseRenderStatic {
54    type RenderArg: Props;
55    /// See [`UseRender::Output`]
56    type RenderOutput;
57    fn use_render(props: &Self::RenderArg) -> Self::RenderOutput;
58}
59
60pub trait ComponentStatic {
61    type Props: Props;
62    /// See [`Component::Element`]
63    type Element;
64
65    fn create_element(props: Self::Props, key: Option<crate::Key>) -> Self::Element;
66}
67
68impl<T: UseRenderStatic> UseRender for T {
69    type RenderArg = <T as UseRenderStatic>::RenderArg;
70    type RenderOutput = <T as UseRenderStatic>::RenderOutput;
71
72    #[inline]
73    fn use_render(&self, props: &Self::RenderArg) -> Self::RenderOutput {
74        T::use_render(props)
75    }
76}
77
78impl<T: ComponentStatic> Component for T {
79    type Props = <T as ComponentStatic>::Props;
80    type Element = <T as ComponentStatic>::Element;
81
82    #[inline]
83    fn create_element(self, props: Self::Props, key: Option<crate::Key>) -> Self::Element {
84        T::create_element(props, key)
85    }
86}