Skip to content

Commit 896b589

Browse files
authored
Implement Display for Id newtypes. (#168)
Closes #166
1 parent d2c6383 commit 896b589

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/_macros.rs

+9
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,15 @@ macro_rules! impl_id_traits {
266266
}
267267
}
268268

269+
impl std::fmt::Display for $idtype {
270+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
271+
match *self == Self::NULL {
272+
false => write!(f, "{}({})", stringify!($idtype), self.0),
273+
true => write!(f, "{}(NULL)", stringify!($idtype)),
274+
}
275+
}
276+
}
277+
269278
impl From<$crate::tsk_id_t> for $idtype {
270279
fn from(value: $crate::tsk_id_t) -> Self {
271280
Self(value)

src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ pub use bindings::tsk_size_t;
149149
/// assert_eq!(interesting(x), x);
150150
/// ```
151151
///
152+
/// The types also implement `Display`:
153+
///
154+
/// ```
155+
/// use tskit::NodeId;
156+
///
157+
/// let n = NodeId::from(11);
158+
/// assert_eq!(format!("{}", n), "NodeId(11)".to_string());
159+
/// let n = NodeId::from(NodeId::NULL);
160+
/// assert_eq!(format!("{}", n), "NodeId(NULL)".to_string());
161+
/// ```
162+
///
152163
#[repr(transparent)]
153164
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
154165
pub struct NodeId(tsk_id_t);

src/provenance.rs

-6
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,6 @@ impl PartialEq for ProvenanceTableRow {
127127
}
128128
}
129129

130-
impl std::fmt::Display for ProvenanceId {
131-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
132-
write!(f, "ProvenanceId({})", self.0)
133-
}
134-
}
135-
136130
impl std::fmt::Display for ProvenanceTableRow {
137131
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
138132
write!(

0 commit comments

Comments
 (0)