use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::hash::Hash;
use crate::base::OptionalFeatures;
pub trait NodeId
where Self: Sized + OptionalFeatures + Eq + PartialEq + Ord + PartialOrd + Debug + Display + Hash + Clone + 'static
{
}
impl<T> NodeId for T where T: Sized + OptionalFeatures + Eq + PartialEq + Ord + PartialOrd + Debug + Display + Hash + Clone + 'static
{}
pub trait Node
where Self: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + 'static
{
}
impl<T> Node for T where T: Sized + OptionalFeatures + Eq + PartialEq + Debug + Clone + 'static {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct EmptyNode {}
impl Default for EmptyNode {
fn default() -> Self {
Self::new()
}
}
impl EmptyNode {
pub fn new() -> Self {
Self {}
}
}
impl Display for EmptyNode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{{}}")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BasicNode {
pub addr: String,
}
impl Default for BasicNode {
fn default() -> Self {
Self {
addr: "localhost".to_string(),
}
}
}
impl BasicNode {
pub fn new(addr: impl ToString) -> Self {
Self { addr: addr.to_string() }
}
}
impl Display for BasicNode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.addr)
}
}
#[cfg(test)]
mod tests {
use std::fmt;
use crate::NodeId;
#[test]
fn node_id_default_impl() {
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
struct AutoNodeId;
impl fmt::Display for AutoNodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "FooNodeId")
}
}
fn assert_node_id<NID: NodeId>(_nid: &NID) {}
assert_node_id(&AutoNodeId);
}
}