Skip to content

Commit 892ee71

Browse files
committed
replace the From impl with one that takes a vector
`foo.into()` should not require cloning and re-allocating
1 parent a4c1d80 commit 892ee71

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ impl<Tuple: Ord> Relation<Tuple> {
133133
}
134134
}
135135

136-
impl<Tuple: Ord, I: IntoIterator<Item = Tuple>> From<I> for Relation<Tuple> {
137-
fn from(iterator: I) -> Self {
138-
Self::from_iter(iterator)
136+
impl<Tuple: Ord> From<Vec<Tuple>> for Relation<Tuple> {
137+
fn from(iterator: Vec<Tuple>) -> Self {
138+
Self::from_vec(iterator)
139139
}
140140
}
141141

@@ -275,8 +275,8 @@ impl<Tuple: Ord> Variable<Tuple> {
275275
///
276276
/// let mut iteration = Iteration::new();
277277
/// let variable = iteration.variable::<(usize, usize)>("source");
278-
/// variable.insert(Relation::from((0 .. 10).map(|x| (x, x + 1))));
279-
/// variable.insert(Relation::from((0 .. 10).map(|x| (x + 1, x))));
278+
/// variable.insert((0 .. 10).map(|x| (x, x + 1)).collect());
279+
/// variable.insert((0 .. 10).map(|x| (x + 1, x)).collect());
280280
///
281281
/// while iteration.changed() {
282282
/// variable.from_join(&variable, &variable, |&key, &val1, &val2| (val1, val2));
@@ -312,9 +312,9 @@ impl<Tuple: Ord> Variable<Tuple> {
312312
///
313313
/// let mut iteration = Iteration::new();
314314
/// let variable = iteration.variable::<(usize, usize)>("source");
315-
/// variable.insert(Relation::from((0 .. 10).map(|x| (x, x + 1))));
315+
/// variable.insert((0 .. 10).map(|x| (x, x + 1)).collect());
316316
///
317-
/// let relation = Relation::from((0 .. 10).filter(|x| x % 3 == 0));
317+
/// let relation: Relation<_> = (0 .. 10).filter(|x| x % 3 == 0).collect();
318318
///
319319
/// while iteration.changed() {
320320
/// variable.from_antijoin(&variable, &relation, |&key, &val| (val, key));
@@ -346,7 +346,7 @@ impl<Tuple: Ord> Variable<Tuple> {
346346
///
347347
/// let mut iteration = Iteration::new();
348348
/// let variable = iteration.variable::<(usize, usize)>("source");
349-
/// variable.insert(Relation::from((0 .. 10).map(|x| (x, x))));
349+
/// variable.insert((0 .. 10).map(|x| (x, x)).collect());
350350
///
351351
/// while iteration.changed() {
352352
/// variable.from_map(&variable, |&(key, val)|

src/test.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ fn inputs() -> impl Strategy<Value = Vec<(u32, u32)>> {
1212

1313
/// The original way to use datafrog -- computes reachable nodes from a set of edges
1414
fn reachable_with_var_join(edges: &[(u32, u32)]) -> Relation<(u32, u32)> {
15-
let edges = Relation::from(edges.iter().cloned());
15+
let edges: Relation<_> = edges.iter().collect();
1616
let mut iteration = Iteration::new();
1717

1818
let edges_by_successor = iteration.variable::<(u32, u32)>("edges_invert");
19-
edges_by_successor.insert(Relation::from(edges.iter().map(|&(n1, n2)| (n2, n1))));
19+
edges_by_successor.insert(edges.iter().map(|&(n1, n2)| (n2, n1)).collect());
2020

2121
let reachable = iteration.variable::<(u32, u32)>("reachable");
2222
reachable.insert(edges);
@@ -35,7 +35,7 @@ fn reachable_with_relation_join(edges: &[(u32, u32)]) -> Relation<(u32, u32)> {
3535
let mut iteration = Iteration::new();
3636

3737
// NB. Changed from `reachable_with_var_join`:
38-
let edges_by_successor = Relation::from(edges.iter().map(|&(n1, n2)| (n2, n1)));
38+
let edges_by_successor: Relation<_> = edges.iter().map(|&(n1, n2)| (n2, n1)).collect();
3939

4040
let reachable = iteration.variable::<(u32, u32)>("reachable");
4141
reachable.insert(edges);
@@ -49,10 +49,10 @@ fn reachable_with_relation_join(edges: &[(u32, u32)]) -> Relation<(u32, u32)> {
4949
}
5050

5151
fn reachable_with_leapfrog(edges: &[(u32, u32)]) -> Relation<(u32, u32)> {
52-
let edges = Relation::from(edges.iter().cloned());
52+
let edges: Relation<_> = edges.iter().collect();
5353
let mut iteration = Iteration::new();
5454

55-
let edges_by_successor = Relation::from(edges.iter().map(|&(n1, n2)| (n2, n1)));
55+
let edges_by_successor: Relation<_> = edges.iter().map(|&(n1, n2)| (n2, n1)).collect();
5656

5757
let reachable = iteration.variable::<(u32, u32)>("reachable");
5858
reachable.insert(edges);
@@ -78,10 +78,10 @@ fn sum_join_via_var(
7878
let mut iteration = Iteration::new();
7979

8080
let input1 = iteration.variable::<(u32, u32)>("input1");
81-
input1.insert(Relation::from(input1_slice.iter().cloned()));
81+
input1.insert(input1_slice.iter().collect());
8282

8383
let input2 = iteration.variable::<(u32, u32)>("input1");
84-
input2.insert(Relation::from(input2_slice.iter().cloned()));
84+
input2.insert(input2_slice.iter().collect());
8585

8686
let output = iteration.variable::<(u32, u32)>("output");
8787

@@ -99,8 +99,8 @@ fn sum_join_via_relation(
9999
input1_slice: &[(u32, u32)],
100100
input2_slice: &[(u32, u32)],
101101
) -> Relation<(u32, u32)> {
102-
let input1 = Relation::from(input1_slice.iter().cloned());
103-
let input2 = Relation::from(input2_slice.iter().cloned());
102+
let input1: Relation<_> = input1_slice.iter().collect();
103+
let input2: Relation<_> = input2_slice.iter().collect();
104104
Relation::from_join(&input1, &input2, |&k1, &v1, &v2| (k1, v1 * 100 + v2))
105105
}
106106

0 commit comments

Comments
 (0)