nitinol_process/receptor/
any.rs

1use super::Receptor;
2use crate::Process;
3use std::any::{type_name, Any};
4use std::sync::Arc;
5use crate::errors::InvalidCast;
6
7pub trait DynRef: 'static + Sync + Send {
8    fn as_any(&self) -> &dyn Any;
9}
10
11pub(crate) struct AnyRef(Arc<dyn DynRef>);
12
13impl AnyRef {
14    pub fn downcast<T: Process>(&self) -> Result<Receptor<T>, InvalidCast> {
15        self.0.as_any()
16            .downcast_ref::<Receptor<T>>()
17            .cloned()
18            .ok_or(InvalidCast { to: type_name::<T>() })
19    }
20}
21
22impl Clone for AnyRef {
23    fn clone(&self) -> Self {
24        Self(Arc::clone(&self.0))
25    }
26}
27
28impl<T: Process> From<Receptor<T>> for AnyRef {
29    fn from(value: Receptor<T>) -> Self {
30        Self(Arc::new(value))
31    }
32}