@@ -39,14 +39,16 @@ use thiserror::Error;
39
39
/// }
40
40
/// }
41
41
///
42
+ /// impl tskit::metadata::MutationMetadata for MyMutation {}
43
+ ///
42
44
/// let mut tables = tskit::TableCollection::new(100.).unwrap();
43
45
/// let mutation = MyMutation{origin_time: 100,
44
46
/// effect_size: -1e-4,
45
47
/// dominance: 0.25};
46
48
///
47
49
/// // Add table row with metadata.
48
50
/// tables.add_mutation_with_metadata(0, 0, tskit::MutationId::NULL, 100., None,
49
- /// Some( &mutation) ).unwrap();
51
+ /// &mutation).unwrap();
50
52
///
51
53
/// // Decode the metadata
52
54
/// // The two unwraps are:
@@ -80,33 +82,54 @@ pub trait MetadataRoundtrip {
80
82
Self : Sized ;
81
83
}
82
84
85
+ /// Marker trait indicating [`MetadataRoundtrip`]
86
+ /// for the mutation table of a [`TableCollection`](crate::TableCollection).
87
+ pub trait MutationMetadata : MetadataRoundtrip { }
88
+
89
+ /// Marker trait indicating [`MetadataRoundtrip`]
90
+ /// for the node table of a [`TableCollection`](crate::TableCollection).
91
+ pub trait NodeMetadata : MetadataRoundtrip { }
92
+
93
+ /// Marker trait indicating [`MetadataRoundtrip`]
94
+ /// for the edge table of a [`TableCollection`](crate::TableCollection).
95
+ pub trait EdgeMetadata : MetadataRoundtrip { }
96
+ ///
97
+ /// Marker trait indicating [`MetadataRoundtrip`]
98
+ /// for the migratoin table of a [`TableCollection`](crate::TableCollection).
99
+ pub trait MigrationMetadata : MetadataRoundtrip { }
100
+
101
+ /// Marker trait indicating [`MetadataRoundtrip`]
102
+ /// for the site table of a [`TableCollection`](crate::TableCollection).
103
+ pub trait SiteMetadata : MetadataRoundtrip { }
104
+
105
+ /// Marker trait indicating [`MetadataRoundtrip`]
106
+ /// for the individual table of a [`TableCollection`](crate::TableCollection).
107
+ pub trait IndividualMetadata : MetadataRoundtrip { }
108
+
109
+ /// Marker trait indicating [`MetadataRoundtrip`]
110
+ /// for the population table of a [`TableCollection`](crate::TableCollection).
111
+ pub trait PopulationMetadata : MetadataRoundtrip { }
112
+
83
113
pub ( crate ) struct EncodedMetadata {
84
- encoded : Option < Vec < u8 > > ,
114
+ encoded : Vec < u8 > ,
85
115
}
86
116
87
117
impl EncodedMetadata {
88
- pub ( crate ) fn new ( md : Option < & dyn MetadataRoundtrip > ) -> Result < Self , MetadataError > {
89
- match md {
90
- Some ( x) => {
91
- let e = x. encode ( ) ?;
92
- Ok ( Self { encoded : Some ( e) } )
93
- }
94
- None => Ok ( Self { encoded : None } ) ,
95
- }
118
+ pub ( crate ) fn new < M : MetadataRoundtrip + ?Sized > ( md : & M ) -> Result < Self , MetadataError > {
119
+ let encoded = md. encode ( ) ?;
120
+ Ok ( Self { encoded } )
96
121
}
97
122
98
123
pub ( crate ) fn as_ptr ( & self ) -> * const libc:: c_char {
99
- match & self . encoded {
100
- Some ( x) => x. as_ptr ( ) as * const libc:: c_char ,
101
- None => std:: ptr:: null ( ) ,
124
+ if self . encoded . is_empty ( ) {
125
+ std:: ptr:: null ( )
126
+ } else {
127
+ self . encoded . as_ptr ( ) as * const libc:: c_char
102
128
}
103
129
}
104
130
105
131
pub ( crate ) fn len ( & self ) -> tsk_size_t {
106
- match & self . encoded {
107
- Some ( x) => x. len ( ) as tsk_size_t ,
108
- None => 0 ,
109
- }
132
+ self . encoded . len ( ) as tsk_size_t
110
133
}
111
134
}
112
135
@@ -194,6 +217,8 @@ mod tests {
194
217
}
195
218
}
196
219
220
+ impl MutationMetadata for F { }
221
+
197
222
#[ test]
198
223
fn test_metadata_round_trip ( ) {
199
224
let f = F { x : -3 , y : 42 } ;
@@ -211,7 +236,7 @@ mod tests {
211
236
#[ test]
212
237
fn test_encoded_metadata_roundtrip ( ) {
213
238
let f = F { x : -3 , y : 42 } ;
214
- let enc = EncodedMetadata :: new ( Some ( & f ) ) . unwrap ( ) ;
239
+ let enc = EncodedMetadata :: new ( & f ) . unwrap ( ) ;
215
240
let p = enc. as_ptr ( ) ;
216
241
let mut d = vec ! [ ] ;
217
242
for i in 0 ..enc. len ( ) {
@@ -245,7 +270,7 @@ mod test_serde {
245
270
#[ test]
246
271
fn test_encoded_metadata_roundtrip ( ) {
247
272
let f = F { x : -3 , y : 42 } ;
248
- let enc = EncodedMetadata :: new ( Some ( & f ) ) . unwrap ( ) ;
273
+ let enc = EncodedMetadata :: new ( & f ) . unwrap ( ) ;
249
274
let p = enc. as_ptr ( ) ;
250
275
let mut d = vec ! [ ] ;
251
276
for i in 0 ..enc. len ( ) {
0 commit comments