Skip to content

Commit a24c618

Browse files
authored
refactor: manually impl Default for NodeDefaultsWithMetadata (#490)
This change doesn't require that metadata types impl Default.
1 parent 13f4549 commit a24c618

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

src/node_table.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pub struct NodeDefaults {
282282
///
283283
/// See [the book](https://tskit-dev.github.io/tskit-rust/)
284284
/// for details.
285-
#[derive(Debug, Clone, Default)]
285+
#[derive(Debug, Clone)]
286286
pub struct NodeDefaultsWithMetadata<M>
287287
where
288288
M: crate::metadata::NodeMetadata,
@@ -293,6 +293,23 @@ where
293293
pub metadata: Option<M>,
294294
}
295295

296+
// Manual implementation required so that
297+
// we do not force client code to impl Default
298+
// for metadata types.
299+
impl<M> Default for NodeDefaultsWithMetadata<M>
300+
where
301+
M: crate::metadata::NodeMetadata,
302+
{
303+
fn default() -> Self {
304+
Self {
305+
flags: NodeFlags::default(),
306+
population: PopulationId::default(),
307+
individual: IndividualId::default(),
308+
metadata: None,
309+
}
310+
}
311+
}
312+
296313
mod private {
297314
pub trait DefaultNodeDataMarker {}
298315

@@ -362,12 +379,6 @@ where
362379
/// value: i32,
363380
/// }
364381
///
365-
/// impl Default for NodeMetadata {
366-
/// fn default() -> Self {
367-
/// Self{value: 0}
368-
/// }
369-
/// }
370-
///
371382
/// impl tskit::metadata::MetadataRoundtrip for NodeMetadata {
372383
/// fn encode(&self) -> Result<Vec<u8>, tskit::metadata::MetadataError> {
373384
/// match serde_json::to_string(self) {
@@ -404,12 +415,6 @@ where
404415
/// value: i32,
405416
/// }
406417
///
407-
/// impl Default for NodeMetadata {
408-
/// fn default() -> Self {
409-
/// Self{value: 0}
410-
/// }
411-
/// }
412-
///
413418
/// impl tskit::metadata::MetadataRoundtrip for NodeMetadata {
414419
/// fn encode(&self) -> Result<Vec<u8>, tskit::metadata::MetadataError> {
415420
/// match serde_json::to_string(self) {

0 commit comments

Comments
 (0)