polyhorn_android/raw/
component.rs

1use std::rc::Rc;
2
3use super::Platform;
4use crate::{Element, Manager};
5
6/// Platform-specific component trait.
7pub trait Component: 'static {
8    /// Render function that must be implemented by components.
9    fn render(&self, manager: &mut Manager) -> Element;
10}
11
12/// Opaque reference counted wrapper around a component.
13#[derive(Clone)]
14pub struct OpaqueComponent(Rc<dyn Component>);
15
16impl AsRef<dyn Component> for OpaqueComponent {
17    fn as_ref(&self) -> &dyn Component {
18        self.0.as_ref()
19    }
20}
21
22/// This is a little bit of machinery that is necessary until we have proper
23/// trait aliases in Rust. Ideally, we would be able to alias
24/// `polyhorn_android::Component` to
25/// `polyhorn_core::Component<polyhorn_android::Platform>`, but that's not yet
26/// possible.
27mod machinery {
28    use super::{Component, Element, Manager, OpaqueComponent, Platform, Rc};
29
30    impl polyhorn_core::Component<Platform> for OpaqueComponent {
31        fn render(&self, manager: &mut Manager) -> Element {
32            self.0.render(manager)
33        }
34    }
35
36    impl<T> From<T> for OpaqueComponent
37    where
38        T: Component + 'static,
39    {
40        fn from(value: T) -> Self {
41            OpaqueComponent(Rc::new(value))
42        }
43    }
44}