#![allow(unused_variables)]
mod handle;
mod id;
mod registry;
pub use self::{handle::Handle, id::Id, registry::Registry};
#[doc(hidden)]
pub use abscissa_derive::Component;
use crate::{application::Application, shutdown::Shutdown, FrameworkError, Version};
use std::{any::Any, cmp::Ordering, fmt::Debug, slice::Iter};
pub trait Component<A>: AsAny + Debug + Send + Sync
where
A: Application,
{
fn id(&self) -> Id;
fn version(&self) -> Version;
fn after_config(&mut self, config: &A::Cfg) -> Result<(), FrameworkError> {
Ok(())
}
fn dependencies(&self) -> Iter<'_, Id> {
[].iter()
}
fn register_dependency(
&mut self,
handle: Handle,
dependency: &mut dyn Component<A>,
) -> Result<(), FrameworkError> {
unimplemented!();
}
fn before_shutdown(&self, kind: Shutdown) -> Result<(), FrameworkError> {
Ok(())
}
}
impl<A> PartialEq for Box<dyn Component<A>>
where
A: Application,
{
fn eq(&self, other: &Self) -> bool {
self.id() == other.id()
}
}
impl<A> PartialOrd for Box<dyn Component<A>>
where
A: Application,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if other.dependencies().any(|dep| *dep == self.id()) {
if self.dependencies().any(|dep| *dep == other.id()) {
None
} else {
Some(Ordering::Greater)
}
} else if self.dependencies().any(|dep| *dep == other.id()) {
Some(Ordering::Less)
} else {
Some(Ordering::Equal)
}
}
}
pub trait AsAny: Any {
fn as_any(&self) -> &dyn Any;
fn as_mut_any(&mut self) -> &mut dyn Any;
}
impl<T> AsAny for T
where
T: Any,
{
fn as_any(&self) -> &dyn Any {
self
}
fn as_mut_any(&mut self) -> &mut dyn Any {
self
}
}