orx_concurrent_option/common_traits/
iter.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
use crate::ConcurrentOption;
use core::{iter::FusedIterator, sync::atomic::Ordering};

// INTO-ITER

impl<'a, T> IntoIterator for &'a ConcurrentOption<T> {
    type Item = &'a T;
    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        unsafe { self.iter_with_order(Ordering::Relaxed) }
    }
}

impl<'a, T> IntoIterator for &'a mut ConcurrentOption<T> {
    type Item = &'a mut T;
    type IntoIter = IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.exclusive_iter_mut()
    }
}

impl<T> IntoIterator for ConcurrentOption<T> {
    type Item = T;

    type IntoIter = core::option::IntoIter<T>;

    fn into_iter(mut self) -> Self::IntoIter {
        self.exclusive_take().into_iter()
    }
}

// ITER

/// Iterator over the `ConcurrentOption` yielding at most one element.
pub struct Iter<'a, T> {
    pub(crate) maybe: Option<&'a T>,
}

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.maybe.take()
    }
}

impl<'a, T> FusedIterator for Iter<'a, T> {}

impl<'a, T> ExactSizeIterator for Iter<'a, T> {
    fn len(&self) -> usize {
        match self.maybe.is_some() {
            true => 1,
            false => 0,
        }
    }
}

impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.next()
    }
}

// ITER-MUT

/// Mutable iterator over the `ConcurrentOption` yielding at most one element.
pub struct IterMut<'a, T> {
    pub(crate) maybe: Option<&'a mut T>,
}

impl<'a, T> Iterator for IterMut<'a, T> {
    type Item = &'a mut T;

    fn next(&mut self) -> Option<Self::Item> {
        self.maybe.take()
    }
}

impl<'a, T> FusedIterator for IterMut<'a, T> {}

impl<'a, T> ExactSizeIterator for IterMut<'a, T> {
    fn len(&self) -> usize {
        match self.maybe.is_some() {
            true => 1,
            false => 0,
        }
    }
}

impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.next()
    }
}