Skip to content

refactor: remove pontential panic! when instantiating table rows. #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 15 additions & 15 deletions src/edge_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ impl PartialEq for EdgeTableRow {
}

fn make_edge_table_row(table: &EdgeTable, pos: tsk_id_t) -> Option<EdgeTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
let table_ref = table.table_;
let rv = EdgeTableRow {
id: pos.into(),
left: table.left(pos).unwrap(),
right: table.right(pos).unwrap(),
parent: table.parent(pos).unwrap(),
child: table.child(pos).unwrap(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
let table_ref = table.table_;
let rv = EdgeTableRow {
id: pos.into(),
left: table.left(pos).unwrap(),
right: table.right(pos).unwrap(),
parent: table.parent(pos).unwrap(),
child: table.child(pos).unwrap(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
} else {
None
}
} else {
None
}
Expand Down
28 changes: 14 additions & 14 deletions src/individual_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ pub struct IndividualTable<'a> {
}

fn make_individual_table_row(table: &IndividualTable, pos: tsk_id_t) -> Option<IndividualTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
let table_ref = table.table_;
let rv = IndividualTableRow {
id: pos.into(),
flags: table.flags(pos).unwrap(),
location: table.location(pos).unwrap().map(|s| s.to_vec()),
parents: table.parents(pos).unwrap().map(|s| s.to_vec()),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
let table_ref = table.table_;
let rv = IndividualTableRow {
id: pos.into(),
flags: table.flags(pos).unwrap(),
location: table.location(pos).unwrap().map(|s| s.to_vec()),
parents: table.parents(pos).unwrap().map(|s| s.to_vec()),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
} else {
None
}
} else {
None
}
Expand Down
32 changes: 16 additions & 16 deletions src/migration_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ impl PartialEq for MigrationTableRow {
}

fn make_migration_table_row(table: &MigrationTable, pos: tsk_id_t) -> Option<MigrationTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
let table_ref = table.table_;
Some(MigrationTableRow {
id: pos.into(),
left: table.left(pos).unwrap(),
right: table.right(pos).unwrap(),
node: table.node(pos).unwrap(),
source: table.source(pos).unwrap(),
dest: table.dest(pos).unwrap(),
time: table.time(pos).unwrap(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
})
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
let table_ref = table.table_;
Some(MigrationTableRow {
id: pos.into(),
left: table.left(pos).unwrap(),
right: table.right(pos).unwrap(),
node: table.node(pos).unwrap(),
source: table.source(pos).unwrap(),
dest: table.dest(pos).unwrap(),
time: table.time(pos).unwrap(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
})
} else {
None
}
} else {
None
}
Expand Down
32 changes: 16 additions & 16 deletions src/mutation_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ impl PartialEq for MutationTableRow {
}

fn make_mutation_table_row(table: &MutationTable, pos: tsk_id_t) -> Option<MutationTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
let table_ref = table.table_;
let rv = MutationTableRow {
id: pos.into(),
site: table.site(pos).unwrap(),
node: table.node(pos).unwrap(),
parent: table.parent(pos).unwrap(),
time: table.time(pos).unwrap(),
derived_state: table.derived_state(pos).unwrap().map(|s| s.to_vec()),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
let table_ref = table.table_;
let rv = MutationTableRow {
id: pos.into(),
site: table.site(pos).unwrap(),
node: table.node(pos).unwrap(),
parent: table.parent(pos).unwrap(),
time: table.time(pos).unwrap(),
derived_state: table.derived_state(pos).unwrap().map(|s| s.to_vec()),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
} else {
None
}
} else {
None
}
Expand Down
28 changes: 14 additions & 14 deletions src/node_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ impl PartialEq for NodeTableRow {
}

fn make_node_table_row(table: &NodeTable, pos: tsk_id_t) -> Option<NodeTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
let table_ref = table.table_;
Some(NodeTableRow {
id: pos.into(),
time: table.time(pos).unwrap(),
flags: table.flags(pos).unwrap(),
population: table.population(pos).unwrap(),
individual: table.individual(pos).unwrap(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
})
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
let table_ref = table.table_;
Some(NodeTableRow {
id: pos.into(),
time: table.time(pos).unwrap(),
flags: table.flags(pos).unwrap(),
population: table.population(pos).unwrap(),
individual: table.individual(pos).unwrap(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
})
} else {
None
}
} else {
None
}
Expand Down
22 changes: 11 additions & 11 deletions src/population_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ impl PartialEq for PopulationTableRow {
}

fn make_population_table_row(table: &PopulationTable, pos: tsk_id_t) -> Option<PopulationTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
let table_ref = table.table_;
let rv = PopulationTableRow {
id: pos.into(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
let table_ref = table.table_;
let rv = PopulationTableRow {
id: pos.into(),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
} else {
None
}
} else {
None
}
Expand Down
20 changes: 10 additions & 10 deletions src/provenance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ impl std::fmt::Display for ProvenanceTableRow {
}

fn make_provenance_row(table: &ProvenanceTable, pos: tsk_id_t) -> Option<ProvenanceTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
Some(ProvenanceTableRow {
id: pos.into(),
timestamp: table.timestamp(pos).unwrap(),
record: table.record(pos).unwrap(),
})
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
Some(ProvenanceTableRow {
id: pos.into(),
timestamp: table.timestamp(pos).unwrap(),
record: table.record(pos).unwrap(),
})
} else {
None
}
} else {
None
}
Expand Down
26 changes: 13 additions & 13 deletions src/site_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ impl PartialEq for SiteTableRow {
}

fn make_site_table_row(table: &SiteTable, pos: tsk_id_t) -> Option<SiteTableRow> {
// panic is okay here, as we are handling a bad
// input value before we first call this to
// set up the iterator
let p = crate::SizeType::try_from(pos).unwrap();
if p < table.num_rows() {
let table_ref = table.table_;
let rv = SiteTableRow {
id: pos.into(),
position: table.position(pos).unwrap(),
ancestral_state: table.ancestral_state(pos).unwrap().map(|s| s.to_vec()),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
if let Ok(p) = crate::SizeType::try_from(pos) {
if p < table.num_rows() {
let table_ref = table.table_;
let rv = SiteTableRow {
id: pos.into(),
position: table.position(pos).unwrap(),
ancestral_state: table.ancestral_state(pos).unwrap().map(|s| s.to_vec()),
metadata: table_row_decode_metadata!(table, table_ref, pos).map(|m| m.to_vec()),
};
Some(rv)
} else {
None
}
} else {
None
}
Expand Down