diff --git a/src/_macros.rs b/src/_macros.rs index 982583791..169e5d95b 100644 --- a/src/_macros.rs +++ b/src/_macros.rs @@ -266,6 +266,15 @@ macro_rules! impl_id_traits { } } + impl std::fmt::Display for $idtype { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match *self == Self::NULL { + false => write!(f, "{}({})", stringify!($idtype), self.0), + true => write!(f, "{}(NULL)", stringify!($idtype)), + } + } + } + impl From<$crate::tsk_id_t> for $idtype { fn from(value: $crate::tsk_id_t) -> Self { Self(value) diff --git a/src/lib.rs b/src/lib.rs index ae1ce64b6..939d75c76 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,6 +149,17 @@ pub use bindings::tsk_size_t; /// assert_eq!(interesting(x), x); /// ``` /// +/// The types also implement `Display`: +/// +/// ``` +/// use tskit::NodeId; +/// +/// let n = NodeId::from(11); +/// assert_eq!(format!("{}", n), "NodeId(11)".to_string()); +/// let n = NodeId::from(NodeId::NULL); +/// assert_eq!(format!("{}", n), "NodeId(NULL)".to_string()); +/// ``` +/// #[repr(transparent)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)] pub struct NodeId(tsk_id_t); diff --git a/src/provenance.rs b/src/provenance.rs index 647ca7933..79fca231f 100644 --- a/src/provenance.rs +++ b/src/provenance.rs @@ -127,12 +127,6 @@ impl PartialEq for ProvenanceTableRow { } } -impl std::fmt::Display for ProvenanceId { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "ProvenanceId({})", self.0) - } -} - impl std::fmt::Display for ProvenanceTableRow { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(