concurrent_slice/
windows.rs

1use crate::common::*;
2
3/// The iterator returned from [owning_windows()](crate::slice::ConcurrentSlice::owning_windows).
4#[derive(Debug)]
5pub struct Windows<S, T> {
6    pub(super) owner: OwningRef<S, [T]>,
7    pub(super) size: usize,
8    pub(super) index: usize,
9}
10
11impl<S, T> Clone for Windows<S, T>
12where
13    S: CloneStableAddress,
14{
15    fn clone(&self) -> Self {
16        Self {
17            owner: self.owner.clone(),
18            ..*self
19        }
20    }
21}
22
23impl<S, T> Iterator for Windows<S, T>
24where
25    S: CloneStableAddress,
26{
27    type Item = OwningRef<S, [T]>;
28
29    fn next(&mut self) -> Option<Self::Item> {
30        if self.index + self.size > self.owner.len() {
31            return None;
32        }
33
34        let window = self
35            .owner
36            .clone()
37            .map(|slice| &slice[self.index..(self.index + self.size)]);
38        self.index += 1;
39        Some(window)
40    }
41
42    fn size_hint(&self) -> (usize, Option<usize>) {
43        if self.owner.len() >= self.size {
44            let len = self.owner.len() - self.size + 1;
45            (len, Some(len))
46        } else {
47            (0, Some(0))
48        }
49    }
50}
51
52impl<S, T> ExactSizeIterator for Windows<S, T> where S: CloneStableAddress {}