Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: impl Default for row id newtypes
  • Loading branch information
molpopgen committed Jan 23, 2023
commit 786181a11b5f7743c8cf86b65f2aad8400aaf958
6 changes: 6 additions & 0 deletions src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ macro_rules! impl_id_traits {
self.partial_cmp(&other.0)
}
}

impl Default for $idtype {
fn default() -> Self {
Self::NULL
}
}
};
}

Expand Down
42 changes: 42 additions & 0 deletions src/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ use bindings::tsk_size_t;
/// assert_eq!(interesting(x), x);
/// ```
///
/// The types implement `Default`, which returns `NULL` values:
///
/// ```
/// assert_eq!(tskit::NodeId::default(), tskit::NodeId::NULL);
/// ```
///
/// The types also implement `Display`:
///
/// ```
Expand All @@ -71,6 +77,12 @@ pub struct NodeId(tsk_id_t);
/// This is an integer referring to a row of an [``IndividualTable``](crate::IndividualTable).
///
/// The features for this type follow the same pattern as for [``NodeId``]
///
/// # Examples
///
/// ```
/// assert_eq!(tskit::IndividualId::default(), tskit::IndividualId::NULL);
/// ```
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
pub struct IndividualId(tsk_id_t);
Expand All @@ -80,6 +92,12 @@ pub struct IndividualId(tsk_id_t);
/// This is an integer referring to a row of an [``PopulationTable``](crate::PopulationTable).
///
/// The features for this type follow the same pattern as for [``NodeId``]
///
/// # Examples
///
/// ```
/// assert_eq!(tskit::PopulationId::default(), tskit::PopulationId::NULL);
/// ```
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
pub struct PopulationId(tsk_id_t);
Expand All @@ -89,6 +107,12 @@ pub struct PopulationId(tsk_id_t);
/// This is an integer referring to a row of an [``SiteTable``](crate::SiteTable).
///
/// The features for this type follow the same pattern as for [``NodeId``]
///
/// # Examples
///
/// ```
/// assert_eq!(tskit::SiteId::default(), tskit::SiteId::NULL);
/// ```
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
pub struct SiteId(tsk_id_t);
Expand All @@ -98,6 +122,12 @@ pub struct SiteId(tsk_id_t);
/// This is an integer referring to a row of an [``MutationTable``](crate::MutationTable).
///
/// The features for this type follow the same pattern as for [``NodeId``]
///
/// # Examples
///
/// ```
/// assert_eq!(tskit::MutationId::default(), tskit::MutationId::NULL);
/// ```
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
pub struct MutationId(tsk_id_t);
Expand All @@ -107,6 +137,12 @@ pub struct MutationId(tsk_id_t);
/// This is an integer referring to a row of an [``MigrationTable``](crate::MigrationTable).
///
/// The features for this type follow the same pattern as for [``NodeId``]
///
/// # Examples
///
/// ```
/// assert_eq!(tskit::MigrationId::default(), tskit::MigrationId::NULL);
/// ```
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
pub struct MigrationId(tsk_id_t);
Expand All @@ -116,6 +152,12 @@ pub struct MigrationId(tsk_id_t);
/// This is an integer referring to a row of an [``EdgeTable``](crate::EdgeTable).
///
/// The features for this type follow the same pattern as for [``NodeId``]
///
/// # Examples
///
/// ```
/// assert_eq!(tskit::SiteId::default(), tskit::SiteId::NULL);
/// ```
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, std::hash::Hash)]
pub struct EdgeId(tsk_id_t);
Expand Down