Skip to content

refactor: manually impl Default for NodeDefaultsWithMetadata #490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2023
Merged
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
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