From a24c6185feccc441d73e935d4e38ea3977d17968 Mon Sep 17 00:00:00 2001 From: "Kevin R. Thornton" Date: Thu, 16 Mar 2023 15:35:17 -0700 Subject: [PATCH] refactor: manually impl Default for NodeDefaultsWithMetadata (#490) This change doesn't require that metadata types impl Default. --- src/node_table.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/node_table.rs b/src/node_table.rs index b76c499f..8b852995 100644 --- a/src/node_table.rs +++ b/src/node_table.rs @@ -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 where M: crate::metadata::NodeMetadata, @@ -293,6 +293,23 @@ where pub metadata: Option, } +// Manual implementation required so that +// we do not force client code to impl Default +// for metadata types. +impl Default for NodeDefaultsWithMetadata +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 {} @@ -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, tskit::metadata::MetadataError> { /// match serde_json::to_string(self) { @@ -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, tskit::metadata::MetadataError> { /// match serde_json::to_string(self) {