Skip to content

Commit a70646a

Browse files
gilhooleydPhilippe-Cholet
authored andcommitted
Add Clone trait to Unique
Using a Unique iterator requires the Clone trait, so require this trait when creating the Unique object.
1 parent 4d2bd4e commit a70646a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/unique_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ where
180180
pub fn unique<I>(iter: I) -> Unique<I>
181181
where
182182
I: Iterator,
183-
I::Item: Eq + Hash,
183+
I::Item: Eq + Hash + Clone,
184184
{
185185
Unique {
186186
iter: UniqueBy {

tests/test_std.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ use rand::{
2121
use rand::{seq::SliceRandom, thread_rng};
2222
use std::{cmp::min, fmt::Debug, marker::PhantomData};
2323

24+
#[test]
25+
fn unique_iterator() {
26+
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
27+
struct Item {
28+
value: u32,
29+
}
30+
impl Item {
31+
fn new(value: u32) -> Item {
32+
Item {value}
33+
}
34+
}
35+
let data = std::vec![Item::new(1), Item::new(2), Item::new(3), Item::new(2)];
36+
let data: Vec<_> = data.into_iter().unique().collect();
37+
assert_eq!(data, std::vec![Item::new(1), Item::new(2), Item::new(3)]);
38+
}
39+
2440
#[test]
2541
fn product3() {
2642
let prod = iproduct!(0..3, 0..2, 0..2);

0 commit comments

Comments
 (0)