concoct/
view_builder.rs

1use crate::{view::Empty, View};
2use std::rc::Rc;
3
4/// Builder for a [`View`].
5///
6/// [`View`] is implemented for anything that implements this trait.
7pub trait ViewBuilder: 'static {
8    fn build(&self) -> impl View;
9}
10
11impl ViewBuilder for () {
12    fn build(&self) -> impl View {
13        Empty
14    }
15}
16
17impl<V: ViewBuilder> ViewBuilder for Rc<V> {
18    fn build(&self) -> impl View {
19        (&**self).build()
20    }
21}
22
23macro_rules! impl_string_view {
24    ($t:ty) => {
25        impl ViewBuilder for $t {
26            fn build(&self) -> impl View {
27                let cx = crate::hook::use_context::<crate::TextViewContext>().unwrap();
28                let mut view = cx.view.borrow_mut();
29                view(self.clone().into())
30            }
31        }
32    };
33}
34
35impl_string_view!(&'static str);
36impl_string_view!(String);