Skip to content

Commit

Permalink
refactor: use Into<Idtype> for metadata retrieval fns (#633)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: use of table.metadata(0.into()) becomes
table.metadata(0)
  • Loading branch information
molpopgen authored May 8, 2024
1 parent b420bee commit 29c1237
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/edge_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl EdgeTable {
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::EdgeMetadata>(
&self,
row: EdgeId,
row: impl Into<EdgeId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
Expand Down
10 changes: 5 additions & 5 deletions src/individual_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl IndividualTable {
/// # assert!(tables.add_individual_with_metadata(0, None, None,
/// # &metadata).is_ok());
/// // We know the metadata are here, so we unwrap the Option and the Result:
/// let decoded = tables.individuals().metadata::<IndividualMetadata>(0.into()).unwrap().unwrap();
/// let decoded = tables.individuals().metadata::<IndividualMetadata>(0).unwrap().unwrap();
/// assert_eq!(decoded.x, 1);
/// # }
/// ```
Expand All @@ -313,7 +313,7 @@ impl IndividualTable {
/// # assert!(tables
/// # .add_individual_with_metadata(0, None, None, &metadata)
/// # .is_ok());
/// match tables.individuals().metadata::<IndividualMetadata>(0.into())
/// match tables.individuals().metadata::<IndividualMetadata>(0)
/// {
/// Some(Ok(metadata)) => assert_eq!(metadata.x, 1),
/// Some(Err(_)) => panic!("got an error??"),
Expand Down Expand Up @@ -350,7 +350,7 @@ impl IndividualTable {
# }
#
# let mut tables = tskit::TableCollection::new(10.).unwrap();
match tables.individuals().metadata::<MutationMetadata>(0.into())
match tables.individuals().metadata::<MutationMetadata>(0)
{
Some(Ok(metadata)) => assert_eq!(metadata.x, 1),
Some(Err(_)) => panic!("got an error??"),
Expand Down Expand Up @@ -398,7 +398,7 @@ match tables.individuals().metadata::<MutationMetadata>(0.into())
/// // Add a row with our metadata
/// assert!(tables.add_individual_with_metadata(0, None, None, &metadata).is_ok());
/// // Trying to fetch using our SECOND type as the generic type works!
/// match tables.individuals().metadata::<IndividualMetadataToo>(0.into())
/// match tables.individuals().metadata::<IndividualMetadataToo>(0)
/// {
/// Some(Ok(metadata)) => assert_eq!(metadata.x, 1),
/// Some(Err(_)) => panic!("got an error??"),
Expand All @@ -420,7 +420,7 @@ match tables.individuals().metadata::<MutationMetadata>(0.into())
/// metadata type for a table.
pub fn metadata<T: metadata::IndividualMetadata>(
&self,
row: IndividualId,
row: impl Into<IndividualId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
Expand Down
2 changes: 1 addition & 1 deletion src/migration_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl MigrationTable {
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::MigrationMetadata>(
&self,
row: MigrationId,
row: impl Into<MigrationId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
Expand Down
2 changes: 1 addition & 1 deletion src/mutation_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl MutationTable {
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::MutationMetadata>(
&self,
row: MutationId,
row: impl Into<MutationId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
Expand Down
2 changes: 1 addition & 1 deletion src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ impl NodeTable {
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::NodeMetadata>(
&self,
row: NodeId,
row: impl Into<NodeId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.table_.raw_metadata(row).ok()??;
Some(decode_metadata_row!(T, buffer).map_err(|e| e.into()))
Expand Down
2 changes: 1 addition & 1 deletion src/population_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl PopulationTable {
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::PopulationMetadata>(
&self,
row: PopulationId,
row: impl Into<PopulationId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(TskitError::from))
Expand Down
2 changes: 1 addition & 1 deletion src/site_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl SiteTable {
/// See [`crate::IndividualTable::metadata`] for examples.
pub fn metadata<T: metadata::SiteMetadata>(
&self,
row: SiteId,
row: impl Into<SiteId>,
) -> Option<Result<T, TskitError>> {
let buffer = self.raw_metadata(row)?;
Some(decode_metadata_row!(T, buffer).map_err(TskitError::from))
Expand Down
3 changes: 2 additions & 1 deletion src/sys/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl NodeTable {
}
}

pub fn raw_metadata(&self, row: NodeId) -> Result<Option<&[u8]>, TskitError> {
pub fn raw_metadata(&self, row: impl Into<NodeId>) -> Result<Option<&[u8]>, TskitError> {
let row = row.into();
if row.is_null() || row.as_usize() >= self.as_ref().num_rows.try_into().unwrap() {
Err(TskitError::IndexError)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/table_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl TableCollection {
/// let metadata = IndividualMetadata{x: 1};
/// assert!(tables.add_individual_with_metadata(0, None, None,
/// &metadata).is_ok());
/// # let decoded = tables.individuals().metadata::<IndividualMetadata>(0.into()).unwrap().unwrap();
/// # let decoded = tables.individuals().metadata::<IndividualMetadata>(0).unwrap().unwrap();
/// # assert_eq!(decoded.x, 1);
/// # }
pub fn add_individual_with_metadata<F, L, P, M>(
Expand Down
5 changes: 1 addition & 4 deletions tests/book_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ fn book_mutation_metadata() {
// There is also no metadata at row 2,
// because that row does not exist, so
// you get None back
assert!(tables
.mutations()
.metadata::<MutationMetadata>(2.into())
.is_none());
assert!(tables.mutations().metadata::<MutationMetadata>(2).is_none());
// ANCHOR_END: metadata_retrieval_none

// ANCHOR: metadata_bulk_decode_lending_iter
Expand Down
4 changes: 2 additions & 2 deletions tests/experimental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod experimental_features {
// current API requires
let decoded = tables
.mutations()
.metadata::<MutationMetadataType>(0.into())
.metadata::<MutationMetadataType>(0)
.unwrap()
.unwrap();
assert_eq!(decoded.effect_size, 0.10);
Expand Down Expand Up @@ -277,7 +277,7 @@ mod experimental_features_refined {
let decoded = tables
.0
.mutations()
.metadata::<MutationMetadataType>(0.into())
.metadata::<MutationMetadataType>(0)
.unwrap()
.unwrap();
assert_eq!(decoded.effect_size, 0.10);
Expand Down

0 comments on commit 29c1237

Please sign in to comment.