imgref_iter/iter/windows/
mod.rs1use std::iter::FusedIterator;
2use std::marker::PhantomData;
3use imgref::Img;
4use crate::iter::{Iter, IterMut};
5
6mod ptr;
7
8pub use ptr::*;
9
10#[repr(transparent)]
11#[derive(Clone, Eq, PartialEq, Debug)]
12pub struct IterWindows<'a, T>(IterWindowsPtr<T>, PhantomData<&'a [T]>);
13
14impl<'a, T> IterWindows<'a, T> {
15 #[inline]
21 pub unsafe fn wrap(ptr: IterWindowsPtr<T>) -> Self {
22 Self(ptr, PhantomData)
23 }
24
25 #[inline]
27 pub fn rows<S: AsRef<[T]>>(buf: &'a Img<S>) -> Self {
28 unsafe { Self::wrap(IterWindowsPtr::rows(buf)) }
29 }
30
31 #[inline]
33 pub fn cols<S: AsRef<[T]>>(buf: &'a Img<S>) -> Self {
34 unsafe { Self::wrap(IterWindowsPtr::cols(buf)) }
35 }
36}
37
38impl<'a, T> Iterator for IterWindows<'a, T> {
39 type Item = Iter<'a, T>;
40
41 #[inline]
42 fn next(&mut self) -> Option<Self::Item> {
43 self.0.next().map(|ptr| unsafe { Iter::wrap(ptr) })
44 }
45
46 #[inline]
47 fn size_hint(&self) -> (usize, Option<usize>) {
48 let len = self.len();
49 (len, Some(len))
50 }
51}
52
53impl<'a, T> DoubleEndedIterator for IterWindows<'a, T> {
54 #[inline]
55 fn next_back(&mut self) -> Option<Self::Item> {
56 self.0.next_back().map(|ptr| unsafe { Iter::wrap(ptr) })
57 }
58}
59
60impl<'a, T> ExactSizeIterator for IterWindows<'a, T> {
61 #[inline]
62 fn len(&self) -> usize {
63 self.0.len()
64 }
65}
66
67impl<'a, T> FusedIterator for IterWindows<'a, T> {}
68
69#[repr(transparent)]
70#[derive(Eq, PartialEq, Debug)]
71pub struct IterWindowsMut<'a, T>(IterWindowsPtrMut<T>, PhantomData<&'a mut [T]>);
72
73impl<'a, T> IterWindowsMut<'a, T> {
74 #[inline]
80 pub unsafe fn wrap(ptr: IterWindowsPtrMut<T>) -> Self {
81 Self(ptr, PhantomData)
82 }
83
84 #[inline]
86 pub fn rows<S: AsMut<[T]>>(buf: &'a mut Img<S>) -> Self {
87 unsafe { Self::wrap(IterWindowsPtrMut::rows(buf)) }
88 }
89
90 #[inline]
92 pub fn cols<S: AsMut<[T]>>(buf: &'a mut Img<S>) -> Self {
93 unsafe { Self::wrap(IterWindowsPtrMut::cols(buf)) }
94 }
95}
96
97impl<'a, T> Iterator for IterWindowsMut<'a, T> {
98 type Item = IterMut<'a, T>;
99
100 #[inline]
101 fn next(&mut self) -> Option<Self::Item> {
102 self.0.next().map(|ptr| unsafe { IterMut::wrap(ptr) })
103 }
104
105 #[inline]
106 fn size_hint(&self) -> (usize, Option<usize>) {
107 let len = self.len();
108 (len, Some(len))
109 }
110}
111
112impl<'a, T> DoubleEndedIterator for IterWindowsMut<'a, T> {
113 #[inline]
114 fn next_back(&mut self) -> Option<Self::Item> {
115 self.0.next_back().map(|ptr| unsafe { IterMut::wrap(ptr) })
116 }
117}
118
119impl<'a, T> ExactSizeIterator for IterWindowsMut<'a, T> {
120 #[inline]
121 fn len(&self) -> usize {
122 self.0.len()
123 }
124}
125
126impl<'a, T> FusedIterator for IterWindowsMut<'a, T> {}