Skip to content

Commit

Permalink
refactor: manually impl Default for NodeDefaultsWithMetadata (#490)
Browse files Browse the repository at this point in the history
This change doesn't require that metadata types impl
Default.
  • Loading branch information
molpopgen authored Mar 16, 2023
1 parent 13f4549 commit a24c618
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ pub struct NodeDefaults {
///
/// See [the book](https://tskit-dev.github.io/tskit-rust/)
/// for details.
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct NodeDefaultsWithMetadata<M>
where
M: crate::metadata::NodeMetadata,
Expand All @@ -293,6 +293,23 @@ where
pub metadata: Option<M>,
}

// Manual implementation required so that
// we do not force client code to impl Default
// for metadata types.
impl<M> Default for NodeDefaultsWithMetadata<M>
where
M: crate::metadata::NodeMetadata,
{
fn default() -> Self {
Self {
flags: NodeFlags::default(),
population: PopulationId::default(),
individual: IndividualId::default(),
metadata: None,
}
}
}

mod private {
pub trait DefaultNodeDataMarker {}

Expand Down Expand Up @@ -362,12 +379,6 @@ where
/// value: i32,
/// }
///
/// impl Default for NodeMetadata {
/// fn default() -> Self {
/// Self{value: 0}
/// }
/// }
///
/// impl tskit::metadata::MetadataRoundtrip for NodeMetadata {
/// fn encode(&self) -> Result<Vec<u8>, tskit::metadata::MetadataError> {
/// match serde_json::to_string(self) {
Expand Down Expand Up @@ -404,12 +415,6 @@ where
/// value: i32,
/// }
///
/// impl Default for NodeMetadata {
/// fn default() -> Self {
/// Self{value: 0}
/// }
/// }
///
/// impl tskit::metadata::MetadataRoundtrip for NodeMetadata {
/// fn encode(&self) -> Result<Vec<u8>, tskit::metadata::MetadataError> {
/// match serde_json::to_string(self) {
Expand Down

0 comments on commit a24c618

Please sign in to comment.