Skip to content

Commit 2143293

Browse files
committed
remaining bits
1 parent d07f490 commit 2143293

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

book/src/table_collection_adding_rows.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ See the [API docs](https://docs.rs/tskit) for more details and examples.
2727

2828
### Adding nodes using default values
2929

30+
This section is more advanced and may be skipped during a first read.
31+
3032
For some tables it may be common to input the same values over and over for some fields when adding rows.
3133
Let's take a look at how to use default values when adding rows to a node table.
3234

@@ -81,8 +83,37 @@ This case is straightforward:
8183

8284
##### Case 2: default metadata
8385

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+
8492
Consider the following case:
8593

8694
```rust, noplaygound, ignore
8795
{{#include ../../tests/book_table_collection.rs:node_defaults_with_some_metadata_default}}
8896
```
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

Comments
 (0)