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