imgref_iter/iter/windows/
mod.rs

1use 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	/// Wraps an [`IterWindowsPtr`] in an [`IterWindows`].
16	///
17	/// # Safety
18	///
19	/// The [`IterWindowsPtr`] must be valid for reads and shared references.
20	#[inline]
21	pub unsafe fn wrap(ptr: IterWindowsPtr<T>) -> Self {
22		Self(ptr, PhantomData)
23	}
24
25	/// Creates a new [`IterWindows`] over the rows of an [`Img`].
26	#[inline]
27	pub fn rows<S: AsRef<[T]>>(buf: &'a Img<S>) -> Self {
28		unsafe { Self::wrap(IterWindowsPtr::rows(buf)) }
29	}
30
31	/// Creates a new [`IterWindows`] over the cols of an [`Img`].
32	#[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	/// Wraps an [`IterWindowsPtrMut`] in an [`IterWindowsMut`].
75	///
76	/// # Safety
77	///
78	/// The [`IterWindowsPtrMut`] must be valid for reads and writes.
79	#[inline]
80	pub unsafe fn wrap(ptr: IterWindowsPtrMut<T>) -> Self {
81		Self(ptr, PhantomData)
82	}
83
84	/// Creates a new [`IterWindowsMut`] over the rows of an [`Img`].
85	#[inline]
86	pub fn rows<S: AsMut<[T]>>(buf: &'a mut Img<S>) -> Self {
87		unsafe { Self::wrap(IterWindowsPtrMut::rows(buf)) }
88	}
89
90	/// Creates a new [`IterWindowsMut`] over the cols of an [`Img`].
91	#[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> {}