widestring/utfstring/
iter.rs

1use super::{Utf16String, Utf32String};
2use crate::utfstr::{CharsUtf16, CharsUtf32};
3#[allow(unused_imports)]
4use core::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator, Iterator};
5
6/// A draining iterator for [`Utf16String`].
7///
8/// This struct is created by the [`drain`][Utf16String::drain] method on [`Utf16String`]. See its
9/// documentation for more.
10pub struct DrainUtf16<'a> {
11    pub(super) start: usize,
12    pub(super) end: usize,
13    pub(super) iter: CharsUtf16<'a>,
14    pub(super) string: *mut Utf16String,
15}
16
17unsafe impl Sync for DrainUtf16<'_> {}
18unsafe impl Send for DrainUtf16<'_> {}
19
20impl Drop for DrainUtf16<'_> {
21    fn drop(&mut self) {
22        unsafe {
23            // Use Vec::drain. "Reaffirm" the bounds checks to avoid
24            // panic code being inserted again.
25            let self_vec = (*self.string).as_mut_vec();
26            if self.start <= self.end && self.end <= self_vec.len() {
27                self_vec.drain(self.start..self.end);
28            }
29        }
30    }
31}
32
33impl core::fmt::Debug for DrainUtf16<'_> {
34    #[inline]
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        core::fmt::Debug::fmt(&self.iter, f)
37    }
38}
39
40impl core::fmt::Display for DrainUtf16<'_> {
41    #[inline]
42    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43        core::fmt::Display::fmt(&self.iter, f)
44    }
45}
46
47impl Iterator for DrainUtf16<'_> {
48    type Item = char;
49
50    #[inline]
51    fn next(&mut self) -> Option<Self::Item> {
52        self.iter.next()
53    }
54
55    #[inline]
56    fn size_hint(&self) -> (usize, Option<usize>) {
57        self.iter.size_hint()
58    }
59}
60
61impl DoubleEndedIterator for DrainUtf16<'_> {
62    #[inline]
63    fn next_back(&mut self) -> Option<Self::Item> {
64        self.iter.next_back()
65    }
66}
67
68impl FusedIterator for DrainUtf16<'_> {}
69
70/// A draining iterator for [`Utf32String`].
71///
72/// This struct is created by the [`drain`][Utf32String::drain] method on [`Utf32String`]. See its
73/// documentation for more.
74pub struct DrainUtf32<'a> {
75    pub(super) start: usize,
76    pub(super) end: usize,
77    pub(super) iter: CharsUtf32<'a>,
78    pub(super) string: *mut Utf32String,
79}
80
81unsafe impl Sync for DrainUtf32<'_> {}
82unsafe impl Send for DrainUtf32<'_> {}
83
84impl Drop for DrainUtf32<'_> {
85    fn drop(&mut self) {
86        unsafe {
87            // Use Vec::drain. "Reaffirm" the bounds checks to avoid
88            // panic code being inserted again.
89            let self_vec = (*self.string).as_mut_vec();
90            if self.start <= self.end && self.end <= self_vec.len() {
91                self_vec.drain(self.start..self.end);
92            }
93        }
94    }
95}
96
97impl core::fmt::Debug for DrainUtf32<'_> {
98    #[inline]
99    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
100        core::fmt::Debug::fmt(&self.iter, f)
101    }
102}
103
104impl core::fmt::Display for DrainUtf32<'_> {
105    #[inline]
106    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
107        core::fmt::Display::fmt(&self.iter, f)
108    }
109}
110
111impl Iterator for DrainUtf32<'_> {
112    type Item = char;
113
114    #[inline]
115    fn next(&mut self) -> Option<Self::Item> {
116        self.iter.next()
117    }
118
119    #[inline]
120    fn size_hint(&self) -> (usize, Option<usize>) {
121        self.iter.size_hint()
122    }
123}
124
125impl DoubleEndedIterator for DrainUtf32<'_> {
126    #[inline]
127    fn next_back(&mut self) -> Option<Self::Item> {
128        self.iter.next_back()
129    }
130}
131
132impl FusedIterator for DrainUtf32<'_> {}
133
134impl ExactSizeIterator for DrainUtf32<'_> {
135    #[inline]
136    fn len(&self) -> usize {
137        self.iter.len()
138    }
139}