use super::*;
use slotmap::{new_key_type, KeyData};
new_key_type! {
pub struct NodeID;
}
impl NodeID {
pub fn to_u64(&self) -> u64 {
self.0.as_ffi()
}
pub fn from_u64(id: u64) -> Self {
NodeID::from(KeyData::from_ffi(id))
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "specta", derive(specta::Type))]
pub struct Node<T> {
pub id: NodeID,
pub data: T,
pub connections: Vec<EdgeID>,
}
impl<T> PartialEq for Node<T> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<T: std::hash::Hash> std::hash::Hash for Node<T> {
fn hash<H: std::hash::Hasher>(&self, ra_expand_state: &mut H) {
self.id.hash(ra_expand_state);
}
}
impl<T: fmt::Debug> fmt::Debug for Node<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Node {{ id: {:#?}, data: {:#?}, connections: {:#?} }}",
self.id, self.data, self.connections
)
}
}
impl<T> Node<T> {
pub fn new(id: NodeID, data: T) -> Node<T> {
Node {
id,
data,
connections: Vec::new(),
}
}
pub fn add_connection(&mut self, edge: EdgeID) {
self.connections.push(edge);
}
}