Skip to content

Commit aa94e1e

Browse files
committed
refactor: Return &str from provenance table getters (#403)
* reduce internal code duplication BREAKING CHANGE: return value changed. Existing code only breaks if it depended on the return value being a String.
1 parent 9f8c805 commit aa94e1e

File tree

1 file changed

+20
-62
lines changed

1 file changed

+20
-62
lines changed

src/provenance.rs

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl std::fmt::Display for ProvenanceTableRow {
4747
fn make_provenance_row(table: &ProvenanceTable, pos: tsk_id_t) -> Option<ProvenanceTableRow> {
4848
Some(ProvenanceTableRow {
4949
id: pos.into(),
50-
timestamp: table.timestamp(pos)?,
51-
record: table.record(pos)?,
50+
timestamp: table.timestamp(pos)?.to_string(),
51+
record: table.record(pos)?.to_string(),
5252
})
5353
}
5454

@@ -122,35 +122,10 @@ impl<'a> streaming_iterator::StreamingIterator for ProvenanceTableRowView<'a> {
122122

123123
row_lending_iterator_get!();
124124

125-
// FIXME: bad duplication
126125
fn advance(&mut self) {
127126
self.id = (i32::from(self.id) + 1).into();
128-
let record_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
129-
self.id.0,
130-
0,
131-
self.table.num_rows(),
132-
self.table.as_ref(),
133-
record,
134-
record_offset,
135-
record_length
136-
);
137-
self.record = match record_slice {
138-
Some(r) => std::str::from_utf8(r).unwrap(),
139-
None => "",
140-
};
141-
let timestamp_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
142-
self.id.0,
143-
0,
144-
self.table.num_rows(),
145-
self.table.as_ref(),
146-
timestamp,
147-
timestamp_offset,
148-
timestamp_length
149-
);
150-
self.timestamp = match timestamp_slice {
151-
Some(t) => std::str::from_utf8(t).unwrap(),
152-
None => "",
153-
};
127+
self.record = self.table.record(self.id).unwrap_or("");
128+
self.timestamp = self.table.timestamp(self.id).unwrap_or("");
154129
}
155130
}
156131

@@ -209,16 +184,20 @@ impl ProvenanceTable {
209184
/// # panic!("Expected Some(timestamp)");
210185
/// # }
211186
/// ```
212-
pub fn timestamp<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<String> {
213-
unsafe_tsk_ragged_char_column_access!(
187+
pub fn timestamp<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<&str> {
188+
let timestamp_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
214189
row.into().0,
215190
0,
216191
self.num_rows(),
217192
self.as_ref(),
218193
timestamp,
219194
timestamp_offset,
220195
timestamp_length
221-
)
196+
);
197+
match timestamp_slice {
198+
Some(tstamp) => std::str::from_utf8(tstamp).ok(),
199+
None => None,
200+
}
222201
}
223202

224203
/// Get the provenance record for row `row`.
@@ -241,16 +220,20 @@ impl ProvenanceTable {
241220
/// # else {
242221
/// # panic!("Expected Some(timestamp)");
243222
/// # }
244-
pub fn record<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<String> {
245-
unsafe_tsk_ragged_char_column_access!(
223+
pub fn record<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<&str> {
224+
let record_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
246225
row.into().0,
247226
0,
248227
self.num_rows(),
249228
self.as_ref(),
250229
record,
251230
record_offset,
252231
record_length
253-
)
232+
);
233+
match record_slice {
234+
Some(rec) => std::str::from_utf8(rec).ok(),
235+
None => None,
236+
}
254237
}
255238

256239
/// Obtain a [`ProvenanceTableRow`] for row `row`.
@@ -271,37 +254,12 @@ impl ProvenanceTable {
271254
/// * `None` otherwise
272255
pub fn row_view<P: Into<ProvenanceId> + Copy>(&self, row: P) -> Option<ProvenanceTableRowView> {
273256
match u64::try_from(row.into().0).ok() {
274-
// FIXME: bad duplication
275257
Some(x) if x < self.num_rows() => {
276-
let record_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
277-
row.into().0,
278-
0,
279-
self.num_rows(),
280-
self.as_ref(),
281-
record,
282-
record_offset,
283-
record_length
284-
);
285-
let timestamp_slice = unsafe_tsk_ragged_char_column_access_to_slice_u8!(
286-
row.into().0,
287-
0,
288-
self.num_rows(),
289-
self.as_ref(),
290-
timestamp,
291-
timestamp_offset,
292-
timestamp_length
293-
);
294258
let view = ProvenanceTableRowView {
295259
table: self,
296260
id: row.into(),
297-
record: match record_slice {
298-
Some(r) => std::str::from_utf8(r).unwrap(),
299-
None => "",
300-
},
301-
timestamp: match timestamp_slice {
302-
Some(t) => std::str::from_utf8(t).unwrap(),
303-
None => "",
304-
},
261+
record: self.record(row)?,
262+
timestamp: self.timestamp(row)?,
305263
};
306264
Some(view)
307265
}

0 commit comments

Comments
 (0)