Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: provenance getters now return Option<&str>
BREAKING CHANGE: return value changed.
Existing code only breaks if it depended on the return
value being a String.
  • Loading branch information
molpopgen committed Nov 9, 2022
commit 3684efb1d905edf8a9bd737190f1a716cad743d9
24 changes: 16 additions & 8 deletions src/provenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl std::fmt::Display for ProvenanceTableRow {
fn make_provenance_row(table: &ProvenanceTable, pos: tsk_id_t) -> Option<ProvenanceTableRow> {
Some(ProvenanceTableRow {
id: pos.into(),
timestamp: table.timestamp(pos)?,
record: table.record(pos)?,
timestamp: table.timestamp(pos)?.to_string(),
record: table.record(pos)?.to_string(),
})
}

Expand Down Expand Up @@ -209,16 +209,20 @@ impl ProvenanceTable {
/// # panic!("Expected Some(timestamp)");
/// # }
/// ```
pub fn timestamp<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<String> {
unsafe_tsk_ragged_char_column_access!(
pub fn timestamp<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<&str> {
let timestamp_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
timestamp,
timestamp_offset,
timestamp_length
)
);
match timestamp_slice {
Some(tstamp) => std::str::from_utf8(tstamp).ok(),
None => None,
}
}

/// Get the provenance record for row `row`.
Expand All @@ -241,16 +245,20 @@ impl ProvenanceTable {
/// # else {
/// # panic!("Expected Some(timestamp)");
/// # }
pub fn record<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<String> {
unsafe_tsk_ragged_char_column_access!(
pub fn record<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<&str> {
let record_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
row.into().0,
0,
self.num_rows(),
self.as_ref(),
record,
record_offset,
record_length
)
);
match record_slice {
Some(rec) => std::str::from_utf8(rec).ok(),
None => None,
}
}

/// Obtain a [`ProvenanceTableRow`] for row `row`.
Expand Down