orx_concurrent_vec/concurrent_slice/partial_eq.rs
1use super::ConcurrentSlice;
2use crate::elem::ConcurrentElement;
3use orx_fixed_vec::IntoConcurrentPinnedVec;
4
5impl<T, P> ConcurrentSlice<'_, T, P>
6where
7 P: IntoConcurrentPinnedVec<ConcurrentElement<T>>,
8 T: PartialEq,
9{
10 /// Returns the index of the first element equal to the given `value`.
11 /// Returns None if the value is absent.
12 ///
13 /// # Examples
14 ///
15 /// ```
16 /// use orx_concurrent_vec::*;
17 ///
18 /// let vec = ConcurrentVec::new();
19 /// vec.extend(['a', 'b', 'c', 'd', 'e']);
20 ///
21 /// let slice = vec.slice(0..3);
22 ///
23 /// assert_eq!(slice.index_of(&'c'), Some(2));
24 /// assert_eq!(slice.index_of(&'d'), None);
25 /// ```
26 pub fn index_of(&self, value: &T) -> Option<usize> {
27 self.iter().position(|e| e == value)
28 }
29
30 /// Returns whether an element equal to the given `value` exists or not.
31 ///
32 /// # Examples
33 ///
34 /// ```
35 /// use orx_concurrent_vec::*;
36 ///
37 /// let vec = ConcurrentVec::new();
38 /// vec.extend(['a', 'b', 'c', 'd', 'e']);
39 ///
40 /// let slice = vec.slice(0..3);
41 ///
42 /// assert_eq!(slice.contains(&'c'), true);
43 /// assert_eq!(slice.contains(&'d'), false);
44 /// ```
45 pub fn contains(&self, value: &T) -> bool {
46 self.index_of(value).is_some()
47 }
48}