/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.11.5/src/zerovec/slice.rs
Line | Count | Source |
1 | | // This file is part of ICU4X. For terms of use, please see the file |
2 | | // called LICENSE at the top level of the ICU4X source tree |
3 | | // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
4 | | |
5 | | use super::*; |
6 | | use core::cmp::Ordering; |
7 | | use core::ops::Range; |
8 | | |
9 | | /// A zero-copy "slice", i.e. the zero-copy version of `[T]`. |
10 | | /// |
11 | | /// This behaves |
12 | | /// similarly to [`ZeroVec<T>`], however [`ZeroVec<T>`] is allowed to contain |
13 | | /// owned data and as such is ideal for deserialization since most human readable |
14 | | /// serialization formats cannot unconditionally deserialize zero-copy. |
15 | | /// |
16 | | /// This type can be used inside [`VarZeroVec<T>`](crate::VarZeroVec) and [`ZeroMap`](crate::ZeroMap): |
17 | | /// This essentially allows for the construction of zero-copy types isomorphic to `Vec<Vec<T>>` by instead |
18 | | /// using `VarZeroVec<ZeroSlice<T>>`. See the [`VarZeroVec`](crate::VarZeroVec) docs for an example. |
19 | | /// |
20 | | /// # Examples |
21 | | /// |
22 | | /// Const-construct a ZeroSlice of u16: |
23 | | /// |
24 | | /// ``` |
25 | | /// use zerovec::ule::AsULE; |
26 | | /// use zerovec::ZeroSlice; |
27 | | /// |
28 | | /// const DATA: &ZeroSlice<u16> = |
29 | | /// ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([ |
30 | | /// 211, 281, 421, 32973, |
31 | | /// ])); |
32 | | /// |
33 | | /// assert_eq!(DATA.get(1), Some(281)); |
34 | | /// ``` |
35 | | #[repr(transparent)] |
36 | | pub struct ZeroSlice<T: AsULE>([T::ULE]); |
37 | | |
38 | | impl<T> ZeroSlice<T> |
39 | | where |
40 | | T: AsULE, |
41 | | { |
42 | | /// Returns an empty slice. |
43 | 0 | pub const fn new_empty() -> &'static Self { |
44 | 0 | Self::from_ule_slice(&[]) |
45 | 0 | } |
46 | | |
47 | | /// Get this [`ZeroSlice`] as a borrowed [`ZeroVec`] |
48 | | /// |
49 | | /// [`ZeroSlice`] does not have most of the methods that [`ZeroVec`] does, |
50 | | /// so it is recommended to convert it to a [`ZeroVec`] before doing anything. |
51 | | #[inline] |
52 | 0 | pub const fn as_zerovec(&self) -> ZeroVec<'_, T> { |
53 | 0 | ZeroVec::new_borrowed(&self.0) |
54 | 0 | } |
55 | | |
56 | | /// Attempt to construct a `&ZeroSlice<T>` from a byte slice, returning an error |
57 | | /// if it's not a valid byte sequence |
58 | 0 | pub fn parse_bytes(bytes: &[u8]) -> Result<&Self, UleError> { |
59 | 0 | T::ULE::parse_bytes_to_slice(bytes).map(Self::from_ule_slice) |
60 | 0 | } |
61 | | |
62 | | /// Uses a `&[u8]` buffer as a `ZeroVec<T>` without any verification. |
63 | | /// |
64 | | /// # Safety |
65 | | /// |
66 | | /// `bytes` need to be an output from [`ZeroSlice::as_bytes()`]. |
67 | 0 | pub const unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self { |
68 | | // &[u8] and &[T::ULE] are the same slice with different length metadata. |
69 | 0 | Self::from_ule_slice(core::slice::from_raw_parts( |
70 | 0 | bytes.as_ptr() as *const T::ULE, |
71 | 0 | bytes.len() / core::mem::size_of::<T::ULE>(), |
72 | 0 | )) |
73 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<bool>>::from_bytes_unchecked Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u8>>::from_bytes_unchecked Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u32>>::from_bytes_unchecked Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u128>>::from_bytes_unchecked Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::from_bytes_unchecked Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u64>>::from_bytes_unchecked |
74 | | |
75 | | /// Construct a `&ZeroSlice<T>` from a slice of ULEs. |
76 | | /// |
77 | | /// This function can be used for constructing ZeroVecs in a const context, avoiding |
78 | | /// parsing checks. |
79 | | /// |
80 | | /// See [`ZeroSlice`] for an example. |
81 | | #[inline] |
82 | 0 | pub const fn from_ule_slice(slice: &[T::ULE]) -> &Self { |
83 | | // This is safe because ZeroSlice is transparent over [T::ULE] |
84 | | // so &ZeroSlice<T> can be safely cast from &[T::ULE] |
85 | 0 | unsafe { &*(slice as *const _ as *const Self) } |
86 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::JoiningType>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::BidiClass>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::gc::GeneralCategory>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<zerovec::ule::niche::NichedOption<icu_locale_core::subtags::script::Script, 4>>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::script::ScriptWithExt>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<char>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<bool>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u8>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u32>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u128>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::from_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u64>>::from_ule_slice |
87 | | |
88 | | /// Construct a `Box<ZeroSlice<T>>` from a boxed slice of ULEs |
89 | | /// |
90 | | /// ✨ *Enabled with the `alloc` Cargo feature.* |
91 | | #[inline] |
92 | | #[cfg(feature = "alloc")] |
93 | | pub fn from_boxed_slice(slice: alloc::boxed::Box<[T::ULE]>) -> alloc::boxed::Box<Self> { |
94 | | // This is safe because ZeroSlice is transparent over [T::ULE] |
95 | | // so Box<ZeroSlice<T>> can be safely cast from Box<[T::ULE]> |
96 | | unsafe { alloc::boxed::Box::from_raw(alloc::boxed::Box::into_raw(slice) as *mut Self) } |
97 | | } |
98 | | |
99 | | /// Returns this slice as its underlying `&[u8]` byte buffer representation. |
100 | | /// |
101 | | /// Useful for serialization. |
102 | | /// |
103 | | /// # Example |
104 | | /// |
105 | | /// ``` |
106 | | /// use zerovec::ZeroVec; |
107 | | /// |
108 | | /// // The little-endian bytes correspond to the numbers on the following line. |
109 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
110 | | /// let nums: &[u16] = &[211, 281, 421, 32973]; |
111 | | /// |
112 | | /// let zerovec = ZeroVec::alloc_from_slice(nums); |
113 | | /// |
114 | | /// assert_eq!(bytes, zerovec.as_bytes()); |
115 | | /// ``` |
116 | | #[inline] |
117 | 0 | pub fn as_bytes(&self) -> &[u8] { |
118 | 0 | T::ULE::slice_as_bytes(self.as_ule_slice()) |
119 | 0 | } |
120 | | |
121 | | /// Dereferences this slice as `&[T::ULE]`. |
122 | | #[inline] |
123 | 0 | pub const fn as_ule_slice(&self) -> &[T::ULE] { |
124 | 0 | &self.0 |
125 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::JoiningType>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::BidiClass>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::gc::GeneralCategory>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<zerovec::ule::niche::NichedOption<icu_locale_core::subtags::script::Script, 4>>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::script::ScriptWithExt>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<char>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u32>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u8>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::as_ule_slice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::as_ule_slice |
126 | | |
127 | | /// Returns the number of elements in this slice. |
128 | | /// |
129 | | /// # Example |
130 | | /// |
131 | | /// ``` |
132 | | /// use zerovec::ule::AsULE; |
133 | | /// use zerovec::ZeroVec; |
134 | | /// |
135 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
136 | | /// let zerovec: ZeroVec<u16> = |
137 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
138 | | /// |
139 | | /// assert_eq!(4, zerovec.len()); |
140 | | /// assert_eq!( |
141 | | /// bytes.len(), |
142 | | /// zerovec.len() * std::mem::size_of::<<u16 as AsULE>::ULE>() |
143 | | /// ); |
144 | | /// ``` |
145 | | #[inline] |
146 | 0 | pub const fn len(&self) -> usize { |
147 | 0 | self.as_ule_slice().len() |
148 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::JoiningType>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::BidiClass>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::gc::GeneralCategory>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::script::ScriptWithExt>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<char>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u32>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u8>>::len Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::len |
149 | | |
150 | | /// Returns whether this slice is empty. |
151 | | /// |
152 | | /// # Example |
153 | | /// |
154 | | /// ``` |
155 | | /// use zerovec::ZeroVec; |
156 | | /// |
157 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
158 | | /// let zerovec: ZeroVec<u16> = |
159 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
160 | | /// assert!(!zerovec.is_empty()); |
161 | | /// |
162 | | /// let emptyvec: ZeroVec<u16> = ZeroVec::parse_bytes(&[]).expect("infallible"); |
163 | | /// assert!(emptyvec.is_empty()); |
164 | | /// ``` |
165 | | #[inline] |
166 | 0 | pub const fn is_empty(&self) -> bool { |
167 | 0 | self.as_ule_slice().is_empty() |
168 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::is_empty Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::is_empty |
169 | | } |
170 | | |
171 | | impl<T> ZeroSlice<T> |
172 | | where |
173 | | T: AsULE, |
174 | | { |
175 | | /// Gets the element at the specified index. Returns `None` if out of range. |
176 | | /// |
177 | | /// # Example |
178 | | /// |
179 | | /// ``` |
180 | | /// use zerovec::ZeroVec; |
181 | | /// |
182 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
183 | | /// let zerovec: ZeroVec<u16> = |
184 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
185 | | /// |
186 | | /// assert_eq!(zerovec.get(2), Some(421)); |
187 | | /// assert_eq!(zerovec.get(4), None); |
188 | | /// ``` |
189 | | #[inline] |
190 | 0 | pub fn get(&self, index: usize) -> Option<T> { |
191 | 0 | self.as_ule_slice() |
192 | 0 | .get(index) |
193 | 0 | .copied() |
194 | 0 | .map(T::from_unaligned) |
195 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::JoiningType>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::BidiClass>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::gc::GeneralCategory>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::script::ScriptWithExt>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u32>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::get Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::get |
196 | | |
197 | | /// Gets the entire slice as an array of length `N`. Returns `None` if the slice |
198 | | /// does not have exactly `N` elements. |
199 | | /// |
200 | | /// # Example |
201 | | /// |
202 | | /// ``` |
203 | | /// use zerovec::ZeroVec; |
204 | | /// |
205 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
206 | | /// let zerovec: ZeroVec<u16> = |
207 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
208 | | /// let array: [u16; 4] = |
209 | | /// zerovec.get_as_array().expect("should be 4 items in array"); |
210 | | /// |
211 | | /// assert_eq!(array[2], 421); |
212 | | /// ``` |
213 | 0 | pub fn get_as_array<const N: usize>(&self) -> Option<[T; N]> { |
214 | 0 | let ule_array = <&[T::ULE; N]>::try_from(self.as_ule_slice()).ok()?; |
215 | 0 | Some(ule_array.map(|u| T::from_unaligned(u))) |
216 | 0 | } |
217 | | |
218 | | /// Gets a subslice of elements within a certain range. Returns `None` if the range |
219 | | /// is out of bounds of this `ZeroSlice`. |
220 | | /// |
221 | | /// # Example |
222 | | /// |
223 | | /// ``` |
224 | | /// use zerovec::ZeroVec; |
225 | | /// |
226 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
227 | | /// let zerovec: ZeroVec<u16> = |
228 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
229 | | /// |
230 | | /// assert_eq!( |
231 | | /// zerovec.get_subslice(1..3), |
232 | | /// Some(&*ZeroVec::from_slice_or_alloc(&[0x0119, 0x01A5])) |
233 | | /// ); |
234 | | /// assert_eq!(zerovec.get_subslice(3..5), None); |
235 | | /// ``` |
236 | | #[inline] |
237 | 0 | pub fn get_subslice(&self, range: Range<usize>) -> Option<&ZeroSlice<T>> { |
238 | 0 | self.0.get(range).map(ZeroSlice::from_ule_slice) |
239 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<char>>::get_subslice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::get_subslice Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::get_subslice |
240 | | |
241 | | /// Get a borrowed reference to the underlying ULE type at a specified index. |
242 | | /// |
243 | | /// Prefer [`Self::get()`] over this method where possible since working |
244 | | /// directly with `ULE` types is less ergonomic |
245 | 0 | pub fn get_ule_ref(&self, index: usize) -> Option<&T::ULE> { |
246 | 0 | self.as_ule_slice().get(index) |
247 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<zerovec::ule::niche::NichedOption<icu_locale_core::subtags::script::Script, 4>>>::get_ule_ref Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::get_ule_ref |
248 | | |
249 | | /// Casts a `ZeroSlice<T>` to a compatible `ZeroSlice<P>`. |
250 | | /// |
251 | | /// `T` and `P` are compatible if they have the same `ULE` representation. |
252 | | /// |
253 | | /// If the `ULE`s of `T` and `P` are different, use [`Self::try_as_converted()`]. |
254 | | /// |
255 | | /// # Examples |
256 | | /// |
257 | | /// ``` |
258 | | /// use zerovec::ZeroSlice; |
259 | | /// |
260 | | /// const BYTES: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
261 | | /// const ZS_U16: &ZeroSlice<u16> = { |
262 | | /// match ZeroSlice::<u16>::try_from_bytes(BYTES) { |
263 | | /// Ok(s) => s, |
264 | | /// Err(_) => unreachable!(), |
265 | | /// } |
266 | | /// }; |
267 | | /// |
268 | | /// let zs_i16: &ZeroSlice<i16> = ZS_U16.cast(); |
269 | | /// |
270 | | /// assert_eq!(ZS_U16.get(3), Some(32973)); |
271 | | /// assert_eq!(zs_i16.get(3), Some(-32563)); |
272 | | /// ``` |
273 | | #[inline] |
274 | 0 | pub const fn cast<P>(&self) -> &ZeroSlice<P> |
275 | 0 | where |
276 | 0 | P: AsULE<ULE = T::ULE>, |
277 | | { |
278 | 0 | ZeroSlice::<P>::from_ule_slice(self.as_ule_slice()) |
279 | 0 | } |
280 | | |
281 | | /// Converts a `&ZeroSlice<T>` into a `&ZeroSlice<P>`. |
282 | | /// |
283 | | /// The resulting slice will have the same length as the original slice |
284 | | /// if and only if `T::ULE` and `P::ULE` are the same size. |
285 | | /// |
286 | | /// If `T` and `P` have the exact same `ULE`, use [`Self::cast()`]. |
287 | | /// |
288 | | /// # Examples |
289 | | /// |
290 | | /// ``` |
291 | | /// use zerovec::ZeroSlice; |
292 | | /// |
293 | | /// const BYTES: &[u8] = &[0x7F, 0xF3, 0x01, 0x00, 0x49, 0xF6, 0x01, 0x00]; |
294 | | /// const ZS_U32: &ZeroSlice<u32> = { |
295 | | /// match ZeroSlice::<u32>::try_from_bytes(BYTES) { |
296 | | /// Ok(s) => s, |
297 | | /// Err(_) => unreachable!(), |
298 | | /// } |
299 | | /// }; |
300 | | /// |
301 | | /// let zs_u8_4: &ZeroSlice<[u8; 4]> = |
302 | | /// ZS_U32.try_as_converted().expect("valid code points"); |
303 | | /// |
304 | | /// assert_eq!(ZS_U32.get(0), Some(127871)); |
305 | | /// assert_eq!(zs_u8_4.get(0), Some([0x7F, 0xF3, 0x01, 0x00])); |
306 | | /// ``` |
307 | | #[inline] |
308 | 0 | pub fn try_as_converted<P: AsULE>(&self) -> Result<&ZeroSlice<P>, UleError> { |
309 | 0 | let new_slice = P::ULE::parse_bytes_to_slice(self.as_bytes())?; |
310 | 0 | Ok(ZeroSlice::from_ule_slice(new_slice)) |
311 | 0 | } |
312 | | |
313 | | /// Gets the first element. Returns `None` if empty. |
314 | | /// |
315 | | /// # Example |
316 | | /// |
317 | | /// ``` |
318 | | /// use zerovec::ZeroVec; |
319 | | /// |
320 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
321 | | /// let zerovec: ZeroVec<u16> = |
322 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
323 | | /// |
324 | | /// assert_eq!(zerovec.first(), Some(211)); |
325 | | /// ``` |
326 | | #[inline] |
327 | 0 | pub fn first(&self) -> Option<T> { |
328 | 0 | self.as_ule_slice().first().copied().map(T::from_unaligned) |
329 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<char>>::first Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::first Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::first Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::first |
330 | | |
331 | | /// Gets the last element. Returns `None` if empty. |
332 | | /// |
333 | | /// # Example |
334 | | /// |
335 | | /// ``` |
336 | | /// use zerovec::ZeroVec; |
337 | | /// |
338 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
339 | | /// let zerovec: ZeroVec<u16> = |
340 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
341 | | /// |
342 | | /// assert_eq!(zerovec.last(), Some(32973)); |
343 | | /// ``` |
344 | | #[inline] |
345 | 0 | pub fn last(&self) -> Option<T> { |
346 | 0 | self.as_ule_slice().last().copied().map(T::from_unaligned) |
347 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::last Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u8>>::last Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::last |
348 | | |
349 | | /// Gets an iterator over the elements. |
350 | | /// |
351 | | /// # Example |
352 | | /// |
353 | | /// ``` |
354 | | /// use zerovec::ZeroVec; |
355 | | /// |
356 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
357 | | /// let zerovec: ZeroVec<u16> = |
358 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
359 | | /// let mut it = zerovec.iter(); |
360 | | /// |
361 | | /// assert_eq!(it.next(), Some(211)); |
362 | | /// assert_eq!(it.next(), Some(281)); |
363 | | /// assert_eq!(it.next(), Some(421)); |
364 | | /// assert_eq!(it.next(), Some(32973)); |
365 | | /// assert_eq!(it.next(), None); |
366 | | /// ``` |
367 | | #[inline] |
368 | 0 | pub fn iter<'a>(&'a self) -> ZeroSliceIter<'a, T> { |
369 | 0 | ZeroSliceIter(self.as_ule_slice().iter()) |
370 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script>>::iter Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<char>>::iter Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::iter Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::iter Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::iter |
371 | | |
372 | | /// Returns a tuple with the first element and a subslice of the remaining elements. |
373 | | /// |
374 | | /// # Example |
375 | | /// |
376 | | /// ``` |
377 | | /// use zerovec::ule::AsULE; |
378 | | /// use zerovec::ZeroSlice; |
379 | | /// |
380 | | /// const DATA: &ZeroSlice<u16> = |
381 | | /// ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([ |
382 | | /// 211, 281, 421, 32973, |
383 | | /// ])); |
384 | | /// const EXPECTED_VALUE: (u16, &ZeroSlice<u16>) = ( |
385 | | /// 211, |
386 | | /// ZeroSlice::<u16>::from_ule_slice(&<u16 as AsULE>::ULE::from_array([ |
387 | | /// 281, 421, 32973, |
388 | | /// ])), |
389 | | /// ); |
390 | | /// assert_eq!(EXPECTED_VALUE, DATA.split_first().unwrap()); |
391 | | /// ``` |
392 | | #[inline] |
393 | 0 | pub fn split_first(&self) -> Option<(T, &ZeroSlice<T>)> { |
394 | 0 | if let Some(first) = self.first() { |
395 | 0 | return Some(( |
396 | 0 | first, |
397 | 0 | // `unwrap()` must succeed, because `first()` returned `Some`. |
398 | 0 | #[expect(clippy::unwrap_used)] |
399 | 0 | self.get_subslice(1..self.len()).unwrap(), |
400 | 0 | )); |
401 | 0 | } |
402 | 0 | None |
403 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<char>>::split_first Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<u16>>::split_first Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::split_first |
404 | | } |
405 | | |
406 | | /// An iterator over elements in a VarZeroVec |
407 | | #[derive(Debug)] |
408 | | pub struct ZeroSliceIter<'a, T: AsULE>(core::slice::Iter<'a, T::ULE>); |
409 | | |
410 | | impl<'a, T: AsULE> Iterator for ZeroSliceIter<'a, T> { |
411 | | type Item = T; |
412 | 0 | fn next(&mut self) -> Option<T> { |
413 | 0 | self.0.next().copied().map(T::from_unaligned) |
414 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<icu_properties::props::Script> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<char> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<potential_utf::uchar::PotentialCodePoint> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<u16> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<_> as core::iter::traits::iterator::Iterator>::next |
415 | | |
416 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
417 | 0 | self.0.size_hint() |
418 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<char> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<u16> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSliceIter<_> as core::iter::traits::iterator::Iterator>::size_hint |
419 | | } |
420 | | |
421 | | impl<'a, T: AsULE> ExactSizeIterator for ZeroSliceIter<'a, T> { |
422 | 0 | fn len(&self) -> usize { |
423 | 0 | self.0.len() |
424 | 0 | } |
425 | | } |
426 | | |
427 | | impl<'a, T: AsULE> DoubleEndedIterator for ZeroSliceIter<'a, T> { |
428 | 0 | fn next_back(&mut self) -> Option<T> { |
429 | 0 | self.0.next_back().copied().map(T::from_unaligned) |
430 | 0 | } |
431 | | } |
432 | | |
433 | | impl<T> ZeroSlice<T> |
434 | | where |
435 | | T: AsULE + Ord, |
436 | | { |
437 | | /// Binary searches a sorted `ZeroVec<T>` for the given element. For more information, see |
438 | | /// the primitive function [`binary_search`]. |
439 | | /// |
440 | | /// # Example |
441 | | /// |
442 | | /// ``` |
443 | | /// use zerovec::ZeroVec; |
444 | | /// |
445 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
446 | | /// let zerovec: ZeroVec<u16> = |
447 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
448 | | /// |
449 | | /// assert_eq!(zerovec.binary_search(&281), Ok(1)); |
450 | | /// assert_eq!(zerovec.binary_search(&282), Err(2)); |
451 | | /// ``` |
452 | | /// |
453 | | /// [`binary_search`]: https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search |
454 | | #[inline] |
455 | 0 | pub fn binary_search(&self, x: &T) -> Result<usize, usize> { |
456 | 0 | self.as_ule_slice() |
457 | 0 | .binary_search_by(|probe| T::from_unaligned(*probe).cmp(x)) Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script>>::binary_search::{closure#0}Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::binary_search::{closure#0}Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::binary_search::{closure#0} |
458 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script>>::binary_search Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint>>::binary_search Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_>>::binary_search |
459 | | } |
460 | | |
461 | | impl<T> ZeroSlice<T> |
462 | | where |
463 | | T: AsULE, |
464 | | { |
465 | | /// Binary searches a sorted `ZeroVec<T>` based on a given predicate. For more information, see |
466 | | /// the primitive function [`binary_search_by`]. |
467 | | /// |
468 | | /// # Example |
469 | | /// |
470 | | /// ``` |
471 | | /// use zerovec::ZeroVec; |
472 | | /// |
473 | | /// let bytes: &[u8] = &[0xD3, 0x00, 0x19, 0x01, 0xA5, 0x01, 0xCD, 0x80]; |
474 | | /// let zerovec: ZeroVec<u16> = |
475 | | /// ZeroVec::parse_bytes(bytes).expect("infallible"); |
476 | | /// |
477 | | /// assert_eq!(zerovec.binary_search_by(|x| x.cmp(&281)), Ok(1)); |
478 | | /// assert_eq!(zerovec.binary_search_by(|x| x.cmp(&282)), Err(2)); |
479 | | /// ``` |
480 | | /// |
481 | | /// [`binary_search_by`]: https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search_by |
482 | | #[inline] |
483 | 0 | pub fn binary_search_by( |
484 | 0 | &self, |
485 | 0 | mut predicate: impl FnMut(T) -> Ordering, |
486 | 0 | ) -> Result<usize, usize> { |
487 | 0 | self.as_ule_slice() |
488 | 0 | .binary_search_by(|probe| predicate(T::from_unaligned(*probe))) |
489 | 0 | } |
490 | | } |
491 | | |
492 | | // Safety (based on the safety checklist on the VarULE trait): |
493 | | // (`ZeroSlice<T>` is a transparent wrapper around [T::ULE]) |
494 | | // 1. [T::ULE] does not include any uninitialized or padding bytes (achieved by being a slice of a ULE type) |
495 | | // 2. [T::ULE] is aligned to 1 byte (achieved by being a slice of a ULE type) |
496 | | // 3. The impl of `validate_bytes()` returns an error if any byte is not valid. |
497 | | // 4. The impl of `validate_bytes()` returns an error if the slice cannot be used in its entirety |
498 | | // 5. The impl of `from_bytes_unchecked()` returns a reference to the same data. |
499 | | // 6. `as_bytes()` and `parse_bytes()` are defaulted |
500 | | // 7. `[T::ULE]` byte equality is semantic equality (relying on the guideline of the underlying `ULE` type) |
501 | | unsafe impl<T: AsULE + 'static> VarULE for ZeroSlice<T> { |
502 | | #[inline] |
503 | 0 | fn validate_bytes(bytes: &[u8]) -> Result<(), UleError> { |
504 | 0 | T::ULE::validate_bytes(bytes) |
505 | 0 | } |
506 | | |
507 | | #[inline] |
508 | 0 | unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self { |
509 | 0 | Self::from_ule_slice(T::ULE::slice_from_bytes_unchecked(bytes)) |
510 | 0 | } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script> as zerovec::ule::VarULE>::from_bytes_unchecked Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint> as zerovec::ule::VarULE>::from_bytes_unchecked Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<_> as zerovec::ule::VarULE>::from_bytes_unchecked |
511 | | } |
512 | | |
513 | | impl<T> Eq for ZeroSlice<T> where T: AsULE + Eq {} |
514 | | |
515 | | impl<T> PartialEq<ZeroSlice<T>> for ZeroSlice<T> |
516 | | where |
517 | | T: AsULE + PartialEq, |
518 | | { |
519 | | #[inline] |
520 | 0 | fn eq(&self, other: &ZeroSlice<T>) -> bool { |
521 | 0 | self.as_zerovec().eq(&other.as_zerovec()) |
522 | 0 | } |
523 | | } |
524 | | |
525 | | impl<T> PartialEq<[T]> for ZeroSlice<T> |
526 | | where |
527 | | T: AsULE + PartialEq, |
528 | | { |
529 | | #[inline] |
530 | 0 | fn eq(&self, other: &[T]) -> bool { |
531 | 0 | self.iter().eq(other.iter().copied()) |
532 | 0 | } |
533 | | } |
534 | | |
535 | | impl<'a, T> PartialEq<ZeroVec<'a, T>> for ZeroSlice<T> |
536 | | where |
537 | | T: AsULE + PartialEq, |
538 | | { |
539 | | #[inline] |
540 | 0 | fn eq(&self, other: &ZeroVec<'a, T>) -> bool { |
541 | 0 | self.as_zerovec().eq(other) |
542 | 0 | } |
543 | | } |
544 | | |
545 | | impl<'a, T> PartialEq<ZeroSlice<T>> for ZeroVec<'a, T> |
546 | | where |
547 | | T: AsULE + PartialEq, |
548 | | { |
549 | | #[inline] |
550 | 0 | fn eq(&self, other: &ZeroSlice<T>) -> bool { |
551 | 0 | self.eq(&other.as_zerovec()) |
552 | 0 | } |
553 | | } |
554 | | |
555 | | impl<T> fmt::Debug for ZeroSlice<T> |
556 | | where |
557 | | T: AsULE + fmt::Debug, |
558 | | { |
559 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
560 | 0 | self.as_zerovec().fmt(f) |
561 | 0 | } |
562 | | } |
563 | | |
564 | | impl<T: AsULE + PartialOrd> PartialOrd for ZeroSlice<T> { |
565 | 0 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
566 | 0 | self.iter().partial_cmp(other.iter()) |
567 | 0 | } |
568 | | } |
569 | | |
570 | | impl<T: AsULE + Ord> Ord for ZeroSlice<T> { |
571 | 0 | fn cmp(&self, other: &Self) -> Ordering { |
572 | 0 | self.iter().cmp(other.iter()) |
573 | 0 | } |
574 | | } |
575 | | |
576 | | #[cfg(feature = "alloc")] |
577 | | impl<T: AsULE> AsRef<ZeroSlice<T>> for alloc::vec::Vec<T::ULE> { |
578 | | fn as_ref(&self) -> &ZeroSlice<T> { |
579 | | ZeroSlice::<T>::from_ule_slice(self) |
580 | | } |
581 | | } |
582 | | |
583 | | impl<T: AsULE> AsRef<ZeroSlice<T>> for &[T::ULE] { |
584 | 0 | fn as_ref(&self) -> &ZeroSlice<T> { |
585 | 0 | ZeroSlice::<T>::from_ule_slice(self) |
586 | 0 | } |
587 | | } |
588 | | |
589 | | impl<T> Default for &ZeroSlice<T> |
590 | | where |
591 | | T: AsULE, |
592 | | { |
593 | 0 | fn default() -> Self { |
594 | 0 | ZeroSlice::from_ule_slice(&[]) |
595 | 0 | } Unexecuted instantiation: <&zerovec::zerovec::slice::ZeroSlice<icu_properties::props::Script> as core::default::Default>::default Unexecuted instantiation: <&zerovec::zerovec::slice::ZeroSlice<_> as core::default::Default>::default |
596 | | } |
597 | | |
598 | | #[cfg(test)] |
599 | | mod test { |
600 | | use super::*; |
601 | | use crate::zeroslice; |
602 | | |
603 | | #[test] |
604 | | fn test_split_first() { |
605 | | { |
606 | | // empty slice. |
607 | | assert_eq!(None, ZeroSlice::<u16>::new_empty().split_first()); |
608 | | } |
609 | | { |
610 | | // single element slice |
611 | | const DATA: &ZeroSlice<u16> = |
612 | | zeroslice!(u16; <u16 as AsULE>::ULE::from_unsigned; [211]); |
613 | | assert_eq!((211, zeroslice![]), DATA.split_first().unwrap()); |
614 | | } |
615 | | { |
616 | | // slice with many elements. |
617 | | const DATA: &ZeroSlice<u16> = |
618 | | zeroslice!(u16; <u16 as AsULE>::ULE::from_unsigned; [211, 281, 421, 32973]); |
619 | | const EXPECTED_VALUE: (u16, &ZeroSlice<u16>) = ( |
620 | | 211, |
621 | | zeroslice!(u16; <u16 as AsULE>::ULE::from_unsigned; [281, 421, 32973]), |
622 | | ); |
623 | | |
624 | | assert_eq!(EXPECTED_VALUE, DATA.split_first().unwrap()); |
625 | | } |
626 | | } |
627 | | } |