use std::any::Any;
pub trait ErasedComponent: Any + Send + Sync {}
impl<T: Any + Send + Sync> ErasedComponent for T {}
pub trait AnyComponent: ErasedComponent {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum ComponentPackage {
Framework,
Application,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct DependencyDecl {
pub role: &'static str,
pub slot: &'static str,
}
#[derive(Debug)]
pub enum RestoreError {
Malformed(bincode::Error),
Custom(String),
}
impl std::fmt::Display for RestoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Malformed(e) => write!(f, "malformed component state bytes: {e}"),
Self::Custom(m) => write!(f, "component restore failed: {m}"),
}
}
}
impl std::error::Error for RestoreError {}
pub type SerializeFn = fn(&dyn ErasedComponent) -> Vec<u8>;
pub type RestoreFn = fn(&[u8]) -> Result<Box<dyn ErasedComponent>, RestoreError>;
#[derive(Debug)]
pub struct ConstructError {
pub type_name: &'static str,
pub detail: String,
}
impl std::fmt::Display for ConstructError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "construct {}: {}", self.type_name, self.detail)
}
}
impl std::error::Error for ConstructError {}
pub type ConstructFn = fn(&dyn Any) -> Result<Box<dyn ErasedComponent>, ConstructError>;