Skip to content

Commit 64b06b7

Browse files
committed
use struct deconstruction instead of accessing fields
A bit more robust, in case `Relation` ever grew a new field
1 parent 9b382d2 commit 64b06b7

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub struct Relation<Tuple: Ord> {
4444
impl<Tuple: Ord> Relation<Tuple> {
4545
/// Merges two relations into their union.
4646
pub fn merge(self, other: Self) -> Self {
47-
let mut elements1 = self.elements;
48-
let mut elements2 = other.elements;
47+
let Relation { elements: mut elements1 } = self;
48+
let Relation { elements: mut elements2 } = other;
4949

5050
// If one of the element lists is zero-length, we don't need to do any work
5151
if elements1.is_empty() {
@@ -135,6 +135,13 @@ impl<Tuple: Ord> Relation<Tuple> {
135135
join::antijoin(input1, input2, logic)
136136
}
137137

138+
/// Construct a new relation by mapping another one. Equivalent to
139+
/// creating an iterator but perhaps more convenient. Analogous to
140+
/// `Variable::from_map`.
141+
pub fn from_map<T2: Ord>(&self, input: &Relation<T2>, logic: impl FnMut(&T2) -> Tuple) -> Self {
142+
input.iter().map(logic).collect()
143+
}
144+
138145
/// Creates a `Relation` from a vector of tuples.
139146
pub fn from_vec(mut elements: Vec<Tuple>) -> Self {
140147
elements.sort();

0 commit comments

Comments
 (0)