pub(crate) mod def;
pub(crate) mod dynamic;
pub(crate) mod handle;
pub(crate) mod keyed;
pub(crate) mod meta;
use std::any::{Any, TypeId, type_name};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::Arc;
use crate::binding::Qualifier;
use crate::module::def::ModuleDef;
use crate::module::keyed::Keyed;
use crate::type_name::short_type_name;
pub trait Module: Send + Sync + 'static {
fn identity(&self) -> ModuleIdentity;
fn register(&self, m: &mut ModuleDef<'_>);
fn keyed<Q: 'static>(self) -> Keyed<Q, Self>
where
Self: Sized,
{
Keyed::new(self)
}
}
#[derive(Clone)]
pub struct ModuleIdentity {
ty: TypeId,
ty_name: &'static str,
config: Option<ConfigKey>,
qualifier: Option<Qualifier>,
label: Option<&'static str>,
}
impl ModuleIdentity {
pub fn of_type<M: 'static>() -> Self {
ModuleIdentity { ty: TypeId::of::<M>(), ty_name: type_name::<M>(), config: None, qualifier: None, label: None }
}
pub fn of_value<M: Eq + Hash + Clone + Send + Sync + 'static>(m: &M) -> Self {
ModuleIdentity::of_owner::<M, M>(m)
}
pub fn label(self, name: &'static str) -> Self {
ModuleIdentity { label: Some(name), ..self }
}
pub(crate) fn of_owner<O: 'static, C: Eq + Hash + Clone + Send + Sync + 'static>(config: &C) -> Self {
let mut hasher = DefaultHasher::new();
TypeId::of::<C>().hash(&mut hasher);
config.hash(&mut hasher);
ModuleIdentity {
ty: TypeId::of::<O>(),
ty_name: type_name::<O>(),
config: Some(ConfigKey { hash: hasher.finish(), value: Arc::new(config.clone()) }),
qualifier: None,
label: None,
}
}
pub(crate) fn keyed_by(self, qualifier: Qualifier) -> Self {
ModuleIdentity { qualifier: Some(qualifier), ..self }
}
pub(crate) fn type_id(&self) -> TypeId {
self.ty
}
pub(crate) fn type_name(&self) -> &'static str {
self.ty_name
}
pub(crate) fn qualifier(&self) -> Option<Qualifier> {
self.qualifier
}
pub(crate) fn label_text(&self) -> Option<&'static str> {
self.label
}
pub(crate) fn is_configured(&self) -> bool {
self.config.is_some()
}
}
impl PartialEq for ModuleIdentity {
fn eq(&self, other: &Self) -> bool {
self.ty == other.ty && self.qualifier == other.qualifier && self.config == other.config
}
}
impl Eq for ModuleIdentity {}
impl Hash for ModuleIdentity {
fn hash<H: Hasher>(&self, state: &mut H) {
self.ty.hash(state);
self.qualifier.hash(state);
self.config.as_ref().map(|c| c.hash).hash(state);
}
}
#[derive(Clone)]
pub(crate) struct ConfigKey {
pub(crate) hash: u64,
pub(crate) value: Arc<dyn DynKey>,
}
impl PartialEq for ConfigKey {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash && self.value.eq_dyn(&*other.value)
}
}
pub(crate) trait DynKey: Any + Send + Sync {
fn eq_dyn(&self, other: &dyn DynKey) -> bool;
fn as_any(&self) -> &dyn Any;
}
impl<C: Eq + Send + Sync + 'static> DynKey for C {
fn eq_dyn(&self, other: &dyn DynKey) -> bool {
other.as_any().downcast_ref::<C>() == Some(self)
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct ModuleName {
text: Arc<str>,
full: Arc<str>,
}
impl ModuleName {
pub(crate) fn of(identity: &ModuleIdentity, instance: usize) -> Self {
ModuleName {
text: Arc::from(ModuleName::render(identity, instance, false)),
full: Arc::from(ModuleName::render(identity, instance, true)),
}
}
fn render(identity: &ModuleIdentity, instance: usize, full: bool) -> String {
let path = |name: &'static str| if full { name.to_owned() } else { short_type_name(name) };
let mut text = match identity.label {
Some(label) if full => format!("{label} ({})", identity.ty_name),
Some(label) => label.to_owned(),
None => path(identity.ty_name),
};
if let Some(qualifier) = identity.qualifier {
text.push_str(" @ ");
text.push_str(&path(qualifier.name));
}
if instance > 0 {
text.push_str(&format!(" #{}", instance + 1));
}
text
}
pub fn as_str(&self) -> &str {
&self.text
}
}
impl fmt::Display for ModuleName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(if f.alternate() { &self.full } else { &self.text })
}
}
impl fmt::Debug for ModuleName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
pub(crate) fn colliding_names<'a>(names: impl IntoIterator<Item = &'a ModuleName>) -> HashSet<ModuleName> {
let mut by_text: HashMap<&'a str, Vec<&'a ModuleName>> = HashMap::new();
for name in names {
let group = by_text.entry(name.as_str()).or_default();
if !group.contains(&name) {
group.push(name);
}
}
by_text.into_values().filter(|group| group.len() > 1).flatten().cloned().collect()
}