topcoat_view/component.rs
1use topcoat_core::{context::Cx, error::Result};
2
3use crate::{Props, View};
4
5pub trait Component {
6 /// The component's props, generic over the lifetime of anything they
7 /// borrow: a [`Child`](crate::Child), a `&str`, or another reference the
8 /// caller hands in.
9 ///
10 /// The lifetime lives here rather than on the implementing type so that a
11 /// component borrowing its props is still a plain unit struct, usable by
12 /// its bare name wherever a value is expected.
13 type Props<'a>: Props;
14
15 #[must_use]
16 fn props_builder<'a>() -> <Self::Props<'a> as Props>::Builder {
17 Self::Props::builder()
18 }
19
20 /// Renders the component to a [`View`].
21 ///
22 /// The returned future is the component's body; the [`View`] it resolves
23 /// to builds into the buffer of the build it is polled in and may borrow
24 /// `cx` and the props.
25 fn render<'cx, 'a>(
26 self,
27 cx: &'cx Cx,
28 props: Self::Props<'a>,
29 ) -> impl Future<Output = Result<impl View + 'cx>> + Send + 'cx
30 where
31 'a: 'cx,
32 Self: 'cx,
33 Self::Props<'a>: 'cx;
34}