1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use std::{any::Any, sync::Arc};

#[derive(Clone)]
pub struct ComponentRef(Arc<dyn Any + Send + Sync>);

impl ComponentRef {
    pub fn new<T>(component: T) -> Self
    where
        T: Any + Send + Sync,
    {
        Self(Arc::new(component))
    }

    pub fn downcast<T>(self) -> Option<Arc<T>>
    where
        T: Any + Send + Sync,
    {
        self.0.downcast::<T>().ok()
    }
}