@@ -27,6 +27,8 @@ See the [API docs](https://docs.rs/tskit) for more details and examples.
27
27
28
28
### Adding nodes using default values
29
29
30
+ This section is more advanced and may be skipped during a first read.
31
+
30
32
For some tables it may be common to input the same values over and over for some fields when adding rows.
31
33
Let's take a look at how to use default values when adding rows to a node table.
32
34
@@ -81,8 +83,37 @@ This case is straightforward:
81
83
82
84
##### Case 2: default metadata
83
85
86
+ TL;DR:
87
+
88
+ * If table row defaults * include* metadata, you can run into use-after-move issues.
89
+ Fortunately, the compiler will catch this as an error.
90
+ * The solution is for your metadata type to implement ` Clone ` .
91
+
84
92
Consider the following case:
85
93
86
94
``` rust, noplaygound, ignore
87
95
{{#include ../../tests/book_table_collection.rs:node_defaults_with_some_metadata_default}}
88
96
```
97
+
98
+ Imagine that the first row we add uses different metadata but all the other default values:
99
+
100
+ ``` rust, noplaygound, ignore
101
+ {{#include ../../tests/book_table_collection.rs:node_defaults_with_some_metadata_default_add_first_row}}
102
+ ```
103
+
104
+ Nothing interesting has happened.
105
+ However, let's take a look at what we need to do if our next row uses a non-default ` population ` field and the default metadata:
106
+
107
+ ``` rust, noplaygound, ignore
108
+ {{#include ../../tests/book_table_collection.rs:node_defaults_with_some_metadata_default_add_second_row}}
109
+ ```
110
+
111
+ Note the call to ` ..defaults.clone() ` .
112
+ (For that call to compile, ` NodeMetadata ` must implement ` Clone ` !.)
113
+ Without that, our ` defaults ` instance would have * moved* , leading to a move-after-use compiler error when we add a third row:
114
+
115
+ ``` rust, noplaygound, ignore
116
+ {{#include ../../tests/book_table_collection.rs:node_defaults_with_some_metadata_default_add_third_row}}
117
+ ```
118
+
119
+
0 commit comments