Skip to main content

polyhorn_ios/raw/
component.rs

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