Skip to content

Commit

Permalink
Auto merge of #32951 - LukasKalbertodt:collection_contains_rfc1552, r…
Browse files Browse the repository at this point in the history
…=brson

Add `contains` to `VecDeque` and `LinkedList` (+ tests)

This implements [RFC 1552](https://github.com/rust-lang/rfcs/blob/master/text/1552-contains-method-for-various-collections.md). Tracking issue: #32630

Sorry for the late response. This is my first contribution, so please tell me if anything isn't optimal!
  • Loading branch information
bors committed Apr 20, 2016
2 parents 9bba290 + bf3aefe commit 133f60f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/libcollections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,16 @@ impl<T> LinkedList<T> {
*self = LinkedList::new()
}

/// Returns `true` if the `LinkedList` contains an element equal to the
/// given value.
#[unstable(feature = "linked_list_contains", reason = "recently added",
issue = "32630")]
pub fn contains(&self, x: &T) -> bool
where T: PartialEq<T>
{
self.iter().any(|e| e == x)
}

/// Provides a reference to the front element, or `None` if the list is
/// empty.
///
Expand Down
11 changes: 11 additions & 0 deletions src/libcollections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,17 @@ impl<T> VecDeque<T> {
self.drain(..);
}

/// Returns `true` if the `VecDeque` contains an element equal to the
/// given value.
#[unstable(feature = "vec_deque_contains", reason = "recently added",
issue = "32630")]
pub fn contains(&self, x: &T) -> bool
where T: PartialEq<T>
{
let (a, b) = self.as_slices();
a.contains(x) || b.contains(x)
}

/// Provides a reference to the front element, or `None` if the sequence is
/// empty.
///
Expand Down
2 changes: 2 additions & 0 deletions src/libcollectionstest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#![feature(fn_traits)]
#![feature(enumset)]
#![feature(iter_arith)]
#![feature(linked_list_contains)]
#![feature(map_entry_keys)]
#![feature(map_values_mut)]
#![feature(pattern)]
Expand All @@ -30,6 +31,7 @@
#![feature(test)]
#![feature(unboxed_closures)]
#![feature(unicode)]
#![feature(vec_deque_contains)]

extern crate collections;
extern crate test;
Expand Down
13 changes: 13 additions & 0 deletions src/libcollectionstest/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,16 @@ fn bench_iter_mut_rev(b: &mut test::Bencher) {
assert!(m.iter_mut().rev().count() == 128);
})
}

#[test]
fn test_contains() {
let mut l = LinkedList::new();
l.extend(&[2, 3, 4]);

assert!(l.contains(&3));
assert!(!l.contains(&1));

l.clear();

assert!(!l.contains(&3));
}
13 changes: 13 additions & 0 deletions src/libcollectionstest/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,3 +959,16 @@ fn test_extend_ref() {
assert_eq!(v[4], 5);
assert_eq!(v[5], 6);
}

#[test]
fn test_contains() {
let mut v = VecDeque::new();
v.extend(&[2, 3, 4]);

assert!(v.contains(&3));
assert!(!v.contains(&1));

v.clear();

assert!(!v.contains(&3));
}

0 comments on commit 133f60f

Please sign in to comment.