use std::collections::HashMap;
use std::sync::OnceLock;
pub mod builtins;
pub mod common_relations;
pub mod lattice;
pub mod relations;
pub mod storage;
pub use builtins::*;
pub use common_relations::{
BROADCAST_BINARY, ELEMENTWISE, MATMUL_BINARY, NO_RELATIONS, REDUCE_AXIS, UNARY_SAME_ELEMENT,
};
pub use lattice::Lattice;
pub use relations::{CustomRelationCtx, PortRef, RelationResult, TypeRelation};
pub use storage::{AnyTensor, Dtype, Storage};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TypeKind {
Abstract,
Concrete,
}
#[derive(Clone, Copy, Debug)]
pub struct TypeNode {
pub id: &'static str,
pub parent: Option<&'static str>,
pub kind: TypeKind,
pub ffi_name: &'static str,
pub wire_hash: u64,
pub denotation: &'static str,
}
impl PartialEq for TypeNode {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self, other)
}
}
impl Eq for TypeNode {}
impl std::hash::Hash for TypeNode {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::ptr::hash(self, state);
}
}
impl TypeNode {
pub fn is_subtype_of(&'static self, other: &'static TypeNode) -> bool {
Lattice::get().is_subtype(self, other)
}
pub fn is_concrete(&self) -> bool {
matches!(self.kind, TypeKind::Concrete)
}
pub fn is_abstract(&self) -> bool {
matches!(self.kind, TypeKind::Abstract)
}
}
pub struct TypeNodeReg(pub &'static TypeNode);
inventory::collect!(TypeNodeReg);
pub(crate) fn id_to_node_map() -> &'static HashMap<&'static str, &'static TypeNode> {
static MAP: OnceLock<HashMap<&'static str, &'static TypeNode>> = OnceLock::new();
MAP.get_or_init(|| {
let mut m: HashMap<&'static str, &'static TypeNode> = HashMap::new();
for reg in inventory::iter::<TypeNodeReg> {
m.insert(reg.0.id, reg.0);
}
m
})
}
pub fn lookup_by_id(id: &str) -> Option<&'static TypeNode> {
id_to_node_map().get(id).copied()
}
#[cfg(test)]
#[path = "storage_tests.rs"]
mod storage_tests;