1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::rc::Rc;

use super::Platform;
use crate::{Element, Manager};

/// Platform-specific component trait.
pub trait Component: 'static {
    /// Render function that must be implemented by components.
    fn render(&self, manager: &mut Manager) -> Element;
}

/// Opaque reference counted wrapper around a component.
#[derive(Clone)]
pub struct OpaqueComponent(Rc<dyn Component>);

impl AsRef<dyn Component> for OpaqueComponent {
    fn as_ref(&self) -> &dyn Component {
        self.0.as_ref()
    }
}

/// This is a little bit of machinery that is necessary until we have proper
/// trait aliases in Rust. Ideally, we would be able to alias
/// `polyhorn_android::Component` to
/// `polyhorn_core::Component<polyhorn_android::Platform>`, but that's not yet
/// possible.
mod machinery {
    use super::{Component, Element, Manager, OpaqueComponent, Platform, Rc};

    impl polyhorn_core::Component<Platform> for OpaqueComponent {
        fn render(&self, manager: &mut Manager) -> Element {
            self.0.render(manager)
        }
    }

    impl<T> From<T> for OpaqueComponent
    where
        T: Component + 'static,
    {
        fn from(value: T) -> Self {
            OpaqueComponent(Rc::new(value))
        }
    }
}