orx_concurrent_vec/common_traits/
eq.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::{ConcurrentElement, ConcurrentSlice, ConcurrentVec};

// elem
impl<T: PartialEq> PartialEq for ConcurrentElement<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl<T: PartialEq> PartialEq<T> for ConcurrentElement<T> {
    fn eq(&self, other: &T) -> bool {
        self.map(|x| x == other)
    }
}

// vec

impl<T: PartialEq> PartialEq for ConcurrentVec<T> {
    fn eq(&self, other: &Self) -> bool {
        eq_elem_iters(self.iter(), other.iter())
    }
}

impl<'a, T: PartialEq> PartialEq<ConcurrentSlice<'a, T>> for ConcurrentVec<T> {
    fn eq(&self, other: &ConcurrentSlice<'a, T>) -> bool {
        eq_elem_iters(self.iter(), other.iter())
    }
}

impl<T: PartialEq> PartialEq<[T]> for ConcurrentVec<T> {
    fn eq(&self, other: &[T]) -> bool {
        eq_elem_iter_to_iter(self.iter(), other.iter())
    }
}

impl<const N: usize, T: PartialEq> PartialEq<[T; N]> for ConcurrentVec<T> {
    fn eq(&self, other: &[T; N]) -> bool {
        eq_elem_iter_to_iter(self.iter(), other.iter())
    }
}

// slice

impl<'a, T: PartialEq> PartialEq for ConcurrentSlice<'a, T> {
    fn eq(&self, other: &Self) -> bool {
        eq_elem_iters(self.iter(), other.iter())
    }
}

impl<'a, T: PartialEq> PartialEq<ConcurrentVec<T>> for ConcurrentSlice<'a, T> {
    fn eq(&self, other: &ConcurrentVec<T>) -> bool {
        eq_elem_iters(self.iter(), other.iter())
    }
}

impl<'a, T: PartialEq> PartialEq<[T]> for ConcurrentSlice<'a, T> {
    fn eq(&self, other: &[T]) -> bool {
        eq_elem_iter_to_iter(self.iter(), other.iter())
    }
}

impl<'a, const N: usize, T: PartialEq> PartialEq<[T; N]> for ConcurrentSlice<'a, T> {
    fn eq(&self, other: &[T; N]) -> bool {
        eq_elem_iter_to_iter(self.iter(), other.iter())
    }
}

// helpers

fn eq_elem_iters<'a, T, I, J>(mut a: I, mut b: J) -> bool
where
    I: Iterator<Item = &'a ConcurrentElement<T>>,
    J: Iterator<Item = &'a ConcurrentElement<T>>,
    T: PartialEq + 'a,
{
    loop {
        match (a.next(), b.next()) {
            (Some(a), Some(b)) if a == b => continue,
            (None, None) => return true,
            _ => return false,
        }
    }
}

fn eq_elem_iter_to_iter<'a, T, I, J>(mut a: I, mut b: J) -> bool
where
    I: Iterator<Item = &'a ConcurrentElement<T>>,
    J: Iterator<Item = &'a T>,
    T: PartialEq + 'a,
{
    loop {
        match (a.next(), b.next()) {
            (Some(a), Some(b)) if a.0.is_some_and(|a| a == b) => continue,
            (None, None) => return true,
            _ => return false,
        }
    }
}