Skip to content

Commit a54ea9f

Browse files
committed
feat: impl PartialEq for table row views.
* can compare owning rows to row views. * streamline impl of PartialEq for IndividualTableRow * derive Debug for table row views * derive Debug for the non-owning tables.
1 parent 998dab5 commit a54ea9f

File tree

9 files changed

+272
-20
lines changed

9 files changed

+272
-20
lines changed

src/_macros.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,29 @@ macro_rules! row_lending_iterator_get {
11041104
};
11051105
}
11061106

1107+
macro_rules! optional_container_comparison {
1108+
($lhs: expr, $rhs: expr) => {
1109+
if let Some(value) = &$lhs {
1110+
if let Some(ovalue) = &$rhs {
1111+
if value.len() != ovalue.len() {
1112+
return false;
1113+
}
1114+
if value.iter().zip(ovalue.iter()).any(|(a, b)| a != b) {
1115+
false
1116+
} else {
1117+
true
1118+
}
1119+
} else {
1120+
false
1121+
}
1122+
} else if $rhs.is_some() {
1123+
false
1124+
} else {
1125+
true
1126+
}
1127+
};
1128+
}
1129+
11071130
#[cfg(test)]
11081131
mod test {
11091132
use crate::error::TskitError;

src/edge_table.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl Iterator for EdgeTableIterator {
6565
}
6666

6767
/// Row of an [`EdgeTable`]
68+
#[derive(Debug)]
6869
pub struct EdgeTableRowView<'a> {
6970
table: &'a EdgeTable,
7071
pub id: EdgeId,
@@ -89,6 +90,41 @@ impl<'a> EdgeTableRowView<'a> {
8990
}
9091
}
9192

93+
impl<'a> PartialEq for EdgeTableRowView<'a> {
94+
fn eq(&self, other: &Self) -> bool {
95+
self.id == other.id
96+
&& self.parent == other.parent
97+
&& self.child == other.child
98+
&& crate::util::partial_cmp_equal(&self.left, &other.left)
99+
&& crate::util::partial_cmp_equal(&self.right, &other.right)
100+
&& self.metadata == other.metadata
101+
}
102+
}
103+
104+
impl<'a> Eq for EdgeTableRowView<'a> {}
105+
106+
impl<'a> PartialEq<EdgeTableRow> for EdgeTableRowView<'a> {
107+
fn eq(&self, other: &EdgeTableRow) -> bool {
108+
self.id == other.id
109+
&& self.parent == other.parent
110+
&& self.child == other.child
111+
&& crate::util::partial_cmp_equal(&self.left, &other.left)
112+
&& crate::util::partial_cmp_equal(&self.right, &other.right)
113+
&& optional_container_comparison!(self.metadata, other.metadata)
114+
}
115+
}
116+
117+
impl PartialEq<EdgeTableRowView<'_>> for EdgeTableRow {
118+
fn eq(&self, other: &EdgeTableRowView) -> bool {
119+
self.id == other.id
120+
&& self.parent == other.parent
121+
&& self.child == other.child
122+
&& crate::util::partial_cmp_equal(&self.left, &other.left)
123+
&& crate::util::partial_cmp_equal(&self.right, &other.right)
124+
&& optional_container_comparison!(self.metadata, other.metadata)
125+
}
126+
}
127+
92128
impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
93129
type Item = Self;
94130

@@ -110,6 +146,7 @@ impl<'a> streaming_iterator::StreamingIterator for EdgeTableRowView<'a> {
110146
/// by types implementing [`std::ops::Deref`] to
111147
/// [`crate::table_views::TableViews`]
112148
#[repr(transparent)]
149+
#[derive(Debug)]
113150
pub struct EdgeTable {
114151
pub(crate) table_: NonNull<ll_bindings::tsk_edge_table_t>,
115152
}

src/individual_table.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,11 @@ impl PartialEq for IndividualTableRow {
2424
&& self.flags == other.flags
2525
&& self.parents == other.parents
2626
&& self.metadata == other.metadata
27-
&& match &self.location {
28-
None => other.location.is_none(),
29-
Some(a) => match &other.location {
30-
None => false,
31-
Some(b) => {
32-
if a.len() != b.len() {
33-
false
34-
} else {
35-
for (i, j) in a.iter().enumerate() {
36-
if !crate::util::partial_cmp_equal(&b[i], j) {
37-
return false;
38-
}
39-
}
40-
true
41-
}
42-
}
43-
},
44-
}
27+
&& self.location == other.location
4528
}
4629
}
4730

31+
#[derive(Debug)]
4832
pub struct IndividualTableRowView<'a> {
4933
table: &'a IndividualTable,
5034
pub id: IndividualId,
@@ -67,6 +51,38 @@ impl<'a> IndividualTableRowView<'a> {
6751
}
6852
}
6953

54+
impl<'a> PartialEq for IndividualTableRowView<'a> {
55+
fn eq(&self, other: &Self) -> bool {
56+
self.id == other.id
57+
&& self.flags == other.flags
58+
&& self.parents == other.parents
59+
&& self.metadata == other.metadata
60+
&& self.location == other.location
61+
}
62+
}
63+
64+
impl<'a> Eq for IndividualTableRowView<'a> {}
65+
66+
impl<'a> PartialEq<IndividualTableRow> for IndividualTableRowView<'a> {
67+
fn eq(&self, other: &IndividualTableRow) -> bool {
68+
self.id == other.id
69+
&& self.flags == other.flags
70+
&& optional_container_comparison!(self.parents, other.parents)
71+
&& optional_container_comparison!(self.metadata, other.metadata)
72+
&& optional_container_comparison!(self.location, other.location)
73+
}
74+
}
75+
76+
impl PartialEq<IndividualTableRowView<'_>> for IndividualTableRow {
77+
fn eq(&self, other: &IndividualTableRowView) -> bool {
78+
self.id == other.id
79+
&& self.flags == other.flags
80+
&& optional_container_comparison!(self.parents, other.parents)
81+
&& optional_container_comparison!(self.metadata, other.metadata)
82+
&& optional_container_comparison!(self.location, other.location)
83+
}
84+
}
85+
7086
impl<'a> streaming_iterator::StreamingIterator for IndividualTableRowView<'a> {
7187
type Item = Self;
7288

@@ -86,6 +102,7 @@ impl<'a> streaming_iterator::StreamingIterator for IndividualTableRowView<'a> {
86102
/// These are not created directly but are accessed
87103
/// by types implementing [`std::ops::Deref`] to
88104
/// [`crate::table_views::TableViews`]
105+
#[derive(Debug)]
89106
pub struct IndividualTable {
90107
table_: NonNull<ll_bindings::tsk_individual_table_t>,
91108
}

src/migration_table.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ impl Iterator for MigrationTableIterator {
7373
}
7474
}
7575

76+
#[derive(Debug)]
7677
pub struct MigrationTableRowView<'a> {
7778
table: &'a MigrationTable,
7879
pub id: MigrationId,
@@ -101,6 +102,47 @@ impl<'a> MigrationTableRowView<'a> {
101102
}
102103
}
103104

105+
impl<'a> PartialEq for MigrationTableRowView<'a> {
106+
fn eq(&self, other: &Self) -> bool {
107+
self.id == other.id
108+
&& self.node == other.node
109+
&& self.source == other.source
110+
&& self.dest == other.dest
111+
&& crate::util::partial_cmp_equal(&self.left, &other.left)
112+
&& crate::util::partial_cmp_equal(&self.right, &other.right)
113+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
114+
&& self.metadata == other.metadata
115+
}
116+
}
117+
118+
impl<'a> Eq for MigrationTableRowView<'a> {}
119+
120+
impl<'a> PartialEq<MigrationTableRow> for MigrationTableRowView<'a> {
121+
fn eq(&self, other: &MigrationTableRow) -> bool {
122+
self.id == other.id
123+
&& self.node == other.node
124+
&& self.source == other.source
125+
&& self.dest == other.dest
126+
&& crate::util::partial_cmp_equal(&self.left, &other.left)
127+
&& crate::util::partial_cmp_equal(&self.right, &other.right)
128+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
129+
&& optional_container_comparison!(self.metadata, other.metadata)
130+
}
131+
}
132+
133+
impl PartialEq<MigrationTableRowView<'_>> for MigrationTableRow {
134+
fn eq(&self, other: &MigrationTableRowView) -> bool {
135+
self.id == other.id
136+
&& self.node == other.node
137+
&& self.source == other.source
138+
&& self.dest == other.dest
139+
&& crate::util::partial_cmp_equal(&self.left, &other.left)
140+
&& crate::util::partial_cmp_equal(&self.right, &other.right)
141+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
142+
&& optional_container_comparison!(self.metadata, other.metadata)
143+
}
144+
}
145+
104146
impl<'a> streaming_iterator::StreamingIterator for MigrationTableRowView<'a> {
105147
type Item = Self;
106148

@@ -123,6 +165,7 @@ impl<'a> streaming_iterator::StreamingIterator for MigrationTableRowView<'a> {
123165
/// These are not created directly but are accessed
124166
/// by types implementing [`std::ops::Deref`] to
125167
/// [`crate::table_views::TableViews`]
168+
#[derive(Debug)]
126169
pub struct MigrationTable {
127170
table_: NonNull<ll_bindings::tsk_migration_table_t>,
128171
}

src/mutation_table.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl Iterator for MutationTableIterator {
7676
}
7777
}
7878

79+
#[derive(Debug)]
7980
pub struct MutationTableRowView<'a> {
8081
table: &'a MutationTable,
8182
pub id: MutationId,
@@ -102,6 +103,44 @@ impl<'a> MutationTableRowView<'a> {
102103
}
103104
}
104105

106+
impl<'a> PartialEq for MutationTableRowView<'a> {
107+
fn eq(&self, other: &Self) -> bool {
108+
self.id == other.id
109+
&& self.site == other.site
110+
&& self.node == other.node
111+
&& self.parent == other.parent
112+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
113+
&& self.derived_state == other.derived_state
114+
&& self.metadata == other.metadata
115+
}
116+
}
117+
118+
impl<'a> Eq for MutationTableRowView<'a> {}
119+
120+
impl<'a> PartialEq<MutationTableRow> for MutationTableRowView<'a> {
121+
fn eq(&self, other: &MutationTableRow) -> bool {
122+
self.id == other.id
123+
&& self.site == other.site
124+
&& self.node == other.node
125+
&& self.parent == other.parent
126+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
127+
&& optional_container_comparison!(self.derived_state, other.derived_state)
128+
&& optional_container_comparison!(self.metadata, other.metadata)
129+
}
130+
}
131+
132+
impl PartialEq<MutationTableRowView<'_>> for MutationTableRow {
133+
fn eq(&self, other: &MutationTableRowView) -> bool {
134+
self.id == other.id
135+
&& self.site == other.site
136+
&& self.node == other.node
137+
&& self.parent == other.parent
138+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
139+
&& optional_container_comparison!(self.derived_state, other.derived_state)
140+
&& optional_container_comparison!(self.metadata, other.metadata)
141+
}
142+
}
143+
105144
impl<'a> streaming_iterator::StreamingIterator for MutationTableRowView<'a> {
106145
type Item = Self;
107146

@@ -123,6 +162,7 @@ impl<'a> streaming_iterator::StreamingIterator for MutationTableRowView<'a> {
123162
/// These are not created directly but are accessed
124163
/// by types implementing [`std::ops::Deref`] to
125164
/// [`crate::table_views::TableViews`]
165+
#[derive(Debug)]
126166
pub struct MutationTable {
127167
table_: NonNull<ll_bindings::tsk_mutation_table_t>,
128168
}

src/node_table.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl Iterator for NodeTableIterator {
6666
}
6767
}
6868

69+
#[derive(Debug)]
6970
pub struct NodeTableRowView<'a> {
7071
table: &'a NodeTable,
7172
pub id: NodeId,
@@ -90,6 +91,41 @@ impl<'a> NodeTableRowView<'a> {
9091
}
9192
}
9293

94+
impl<'a> PartialEq for NodeTableRowView<'a> {
95+
fn eq(&self, other: &Self) -> bool {
96+
self.id == other.id
97+
&& self.flags == other.flags
98+
&& self.population == other.population
99+
&& self.individual == other.individual
100+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
101+
&& self.metadata == other.metadata
102+
}
103+
}
104+
105+
impl<'a> Eq for NodeTableRowView<'a> {}
106+
107+
impl<'a> PartialEq<NodeTableRow> for NodeTableRowView<'a> {
108+
fn eq(&self, other: &NodeTableRow) -> bool {
109+
self.id == other.id
110+
&& self.flags == other.flags
111+
&& self.population == other.population
112+
&& self.individual == other.individual
113+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
114+
&& optional_container_comparison!(self.metadata, other.metadata)
115+
}
116+
}
117+
118+
impl PartialEq<NodeTableRowView<'_>> for NodeTableRow {
119+
fn eq(&self, other: &NodeTableRowView) -> bool {
120+
self.id == other.id
121+
&& self.flags == other.flags
122+
&& self.population == other.population
123+
&& self.individual == other.individual
124+
&& crate::util::partial_cmp_equal(&self.time, &other.time)
125+
&& optional_container_comparison!(self.metadata, other.metadata)
126+
}
127+
}
128+
93129
impl<'a> streaming_iterator::StreamingIterator for NodeTableRowView<'a> {
94130
type Item = Self;
95131

@@ -110,6 +146,7 @@ impl<'a> streaming_iterator::StreamingIterator for NodeTableRowView<'a> {
110146
/// These are not created directly but are accessed
111147
/// by types implementing [`std::ops::Deref`] to
112148
/// [`crate::table_views::TableViews`]
149+
#[derive(Debug)]
113150
pub struct NodeTable {
114151
table_: NonNull<ll_bindings::tsk_node_table_t>,
115152
}

src/population_table.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl Iterator for PopulationTableIterator {
6161
}
6262
}
6363

64+
#[derive(Debug)]
6465
pub struct PopulationTableRowView<'a> {
6566
table: &'a PopulationTable,
6667
pub id: PopulationId,
@@ -77,6 +78,26 @@ impl<'a> PopulationTableRowView<'a> {
7778
}
7879
}
7980

81+
impl<'a> PartialEq for PopulationTableRowView<'a> {
82+
fn eq(&self, other: &Self) -> bool {
83+
self.id == other.id && self.metadata == other.metadata
84+
}
85+
}
86+
87+
impl<'a> Eq for PopulationTableRowView<'a> {}
88+
89+
impl<'a> PartialEq<PopulationTableRow> for PopulationTableRowView<'a> {
90+
fn eq(&self, other: &PopulationTableRow) -> bool {
91+
self.id == other.id && optional_container_comparison!(self.metadata, other.metadata)
92+
}
93+
}
94+
95+
impl PartialEq<PopulationTableRowView<'_>> for PopulationTableRow {
96+
fn eq(&self, other: &PopulationTableRowView) -> bool {
97+
self.id == other.id && optional_container_comparison!(self.metadata, other.metadata)
98+
}
99+
}
100+
80101
impl<'a> streaming_iterator::StreamingIterator for PopulationTableRowView<'a> {
81102
type Item = Self;
82103

@@ -94,6 +115,7 @@ impl<'a> streaming_iterator::StreamingIterator for PopulationTableRowView<'a> {
94115
/// by types implementing [`std::ops::Deref`] to
95116
/// [`crate::table_views::TableViews`]
96117
#[repr(transparent)]
118+
#[derive(Debug)]
97119
pub struct PopulationTable {
98120
table_: NonNull<ll_bindings::tsk_population_table_t>,
99121
}

0 commit comments

Comments
 (0)