@@ -124,6 +124,40 @@ impl<'a> PopulationTable<'a> {
124
124
/// use tskit::OwnedPopulationTable;
125
125
///
126
126
/// let mut populations = OwnedPopulationTable::default();
127
+ /// let rowid = populations.add_row().unwrap();
128
+ /// assert_eq!(rowid, 0);
129
+ /// assert_eq!(populations.num_rows(), 1);
130
+ /// ```
131
+ ///
132
+ /// An example with metadata.
133
+ /// This requires the cargo feature `"derive"` for `tskit`.
134
+ ///
135
+ /// ```
136
+ /// # #[cfg(any(feature="doc", feature="derive"))] {
137
+ /// use tskit::OwnedPopulationTable;
138
+ ///
139
+ /// #[derive(serde::Serialize,
140
+ /// serde::Deserialize,
141
+ /// tskit::metadata::PopulationMetadata)]
142
+ /// #[serializer("serde_json")]
143
+ /// struct PopulationMetadata {
144
+ /// name: String,
145
+ /// }
146
+ ///
147
+ /// let metadata = PopulationMetadata{name: "YRB".to_string()};
148
+ ///
149
+ /// let mut populations = OwnedPopulationTable::default();
150
+ ///
151
+ /// let rowid = populations.add_row_with_metadata(&metadata).unwrap();
152
+ /// assert_eq!(rowid, 0);
153
+ ///
154
+ /// if let Some(decoded) = populations.metadata::<PopulationMetadata>(rowid).unwrap() {
155
+ /// assert_eq!(&decoded.name, "YRB");
156
+ /// } else {
157
+ /// panic!("hmm...we expected some metadata!");
158
+ /// }
159
+ ///
160
+ /// # }
127
161
/// ```
128
162
pub struct OwnedPopulationTable {
129
163
table : mbox:: MBox < ll_bindings:: tsk_population_table_t > ,
@@ -142,6 +176,30 @@ impl OwnedPopulationTable {
142
176
let table = unsafe { mbox:: MBox :: from_non_null_raw ( nonnull) } ;
143
177
Self { table }
144
178
}
179
+
180
+ pub fn add_row ( & mut self ) -> Result < PopulationId , TskitError > {
181
+ let rv = unsafe {
182
+ ll_bindings:: tsk_population_table_add_row ( & mut ( * self . table ) , std:: ptr:: null ( ) , 0 )
183
+ } ;
184
+
185
+ handle_tsk_return_value ! ( rv, PopulationId :: from( rv) )
186
+ }
187
+
188
+ pub fn add_row_with_metadata < M : crate :: metadata:: PopulationMetadata > (
189
+ & mut self ,
190
+ metadata : & M ,
191
+ ) -> Result < PopulationId , TskitError > {
192
+ let md = crate :: metadata:: EncodedMetadata :: new ( metadata) ?;
193
+ let rv = unsafe {
194
+ ll_bindings:: tsk_population_table_add_row (
195
+ & mut ( * self . table ) ,
196
+ md. as_ptr ( ) ,
197
+ md. len ( ) . into ( ) ,
198
+ )
199
+ } ;
200
+
201
+ handle_tsk_return_value ! ( rv, PopulationId :: from( rv) )
202
+ }
145
203
}
146
204
147
205
impl std:: ops:: Deref for OwnedPopulationTable {
0 commit comments