/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zerovec-0.11.4/src/ule/mod.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  |  | #![allow(clippy::upper_case_acronyms)]  | 
6  |  |  | 
7  |  | //! Traits over unaligned little-endian data (ULE, pronounced "yule").  | 
8  |  | //!  | 
9  |  | //! The main traits for this module are [`ULE`], [`AsULE`] and, [`VarULE`].  | 
10  |  | //!  | 
11  |  | //! See [the design doc](https://github.com/unicode-org/icu4x/blob/main/utils/zerovec/design_doc.md) for details on how these traits  | 
12  |  | //! works under the hood.  | 
13  |  | mod chars;  | 
14  |  | #[cfg(doc)]  | 
15  |  | pub mod custom;  | 
16  |  | mod encode;  | 
17  |  | mod macros;  | 
18  |  | mod multi;  | 
19  |  | mod niche;  | 
20  |  | mod option;  | 
21  |  | mod plain;  | 
22  |  | mod slices;  | 
23  |  | #[cfg(test)]  | 
24  |  | pub mod test_utils;  | 
25  |  |  | 
26  |  | pub mod tuple;  | 
27  |  | pub mod tuplevar;  | 
28  |  | pub mod vartuple;  | 
29  |  | pub use chars::CharULE;  | 
30  |  | #[cfg(feature = "alloc")]  | 
31  |  | pub use encode::encode_varule_to_box;  | 
32  |  | pub use encode::EncodeAsVarULE;  | 
33  |  | pub use multi::MultiFieldsULE;  | 
34  |  | pub use niche::{NicheBytes, NichedOption, NichedOptionULE}; | 
35  |  | pub use option::{OptionULE, OptionVarULE}; | 
36  |  | pub use plain::RawBytesULE;  | 
37  |  |  | 
38  |  | use core::{any, fmt, mem, slice}; | 
39  |  |  | 
40  |  | /// Fixed-width, byte-aligned data that can be cast to and from a little-endian byte slice.  | 
41  |  | ///  | 
42  |  | /// If you need to implement this trait, consider using [`#[make_ule]`](crate::make_ule) or  | 
43  |  | ///  [`#[derive(ULE)]`](macro@ULE) instead.  | 
44  |  | ///  | 
45  |  | /// Types that are not fixed-width can implement [`VarULE`] instead.  | 
46  |  | ///  | 
47  |  | /// "ULE" stands for "Unaligned little-endian"  | 
48  |  | ///  | 
49  |  | /// # Safety  | 
50  |  | ///  | 
51  |  | /// Safety checklist for `ULE`:  | 
52  |  | ///  | 
53  |  | /// 1. The type *must not* include any uninitialized or padding bytes.  | 
54  |  | /// 2. The type must have an alignment of 1 byte, or it is a ZST that is safe to construct.  | 
55  |  | /// 3. The impl of [`ULE::validate_bytes()`] *must* return an error if the given byte slice  | 
56  |  | ///    would not represent a valid slice of this type.  | 
57  |  | /// 4. The impl of [`ULE::validate_bytes()`] *must* return an error if the given byte slice  | 
58  |  | ///    cannot be used in its entirety (if its length is not a multiple of `size_of::<Self>()`).  | 
59  |  | /// 5. All other methods *must* be left with their default impl, or else implemented according to  | 
60  |  | ///    their respective safety guidelines.  | 
61  |  | /// 6. Acknowledge the following note about the equality invariant.  | 
62  |  | ///  | 
63  |  | /// If the ULE type is a struct only containing other ULE types (or other types which satisfy invariants 1 and 2,  | 
64  |  | /// like `[u8; N]`), invariants 1 and 2 can be achieved via `#[repr(C, packed)]` or `#[repr(transparent)]`.  | 
65  |  | ///  | 
66  |  | /// # Equality invariant  | 
67  |  | ///  | 
68  |  | /// A non-safety invariant is that if `Self` implements `PartialEq`, the it *must* be logically  | 
69  |  | /// equivalent to byte equality on [`Self::slice_as_bytes()`].  | 
70  |  | ///  | 
71  |  | /// It may be necessary to introduce a "canonical form" of the ULE if logical equality does not  | 
72  |  | /// equal byte equality. In such a case, [`Self::validate_bytes()`] should return an error  | 
73  |  | /// for any values that are not in canonical form. For example, the decimal strings "1.23e4" and  | 
74  |  | /// "12.3e3" are logically equal, but not byte-for-byte equal, so we could define a canonical form  | 
75  |  | /// where only a single digit is allowed before `.`.  | 
76  |  | ///  | 
77  |  | /// Failure to follow this invariant will cause surprising behavior in `PartialEq`, which may  | 
78  |  | /// result in unpredictable operations on `ZeroVec`, `VarZeroVec`, and `ZeroMap`.  | 
79  |  | pub unsafe trait ULE  | 
80  |  | where  | 
81  |  |     Self: Sized,  | 
82  |  |     Self: Copy + 'static,  | 
83  |  | { | 
84  |  |     /// Validates a byte slice, `&[u8]`.  | 
85  |  |     ///  | 
86  |  |     /// If `Self` is not well-defined for all possible bit values, the bytes should be validated.  | 
87  |  |     /// If the bytes can be transmuted, *in their entirety*, to a valid slice of `Self`, then `Ok`  | 
88  |  |     /// should be returned; otherwise, `Err` should be returned.  | 
89  |  |     fn validate_bytes(bytes: &[u8]) -> Result<(), UleError>;  | 
90  |  |  | 
91  |  |     /// Parses a byte slice, `&[u8]`, and return it as `&[Self]` with the same lifetime.  | 
92  |  |     ///  | 
93  |  |     /// If `Self` is not well-defined for all possible bit values, the bytes should be validated,  | 
94  |  |     /// and an error should be returned in the same cases as [`Self::validate_bytes()`].  | 
95  |  |     ///  | 
96  |  |     /// The default implementation executes [`Self::validate_bytes()`] followed by  | 
97  |  |     /// [`Self::slice_from_bytes_unchecked`].  | 
98  |  |     ///  | 
99  |  |     /// Note: The following equality should hold: `bytes.len() % size_of::<Self>() == 0`. This  | 
100  |  |     /// means that the returned slice can span the entire byte slice.  | 
101  | 0  |     fn parse_bytes_to_slice(bytes: &[u8]) -> Result<&[Self], UleError> { | 
102  | 0  |         Self::validate_bytes(bytes)?;  | 
103  | 0  |         debug_assert_eq!(bytes.len() % mem::size_of::<Self>(), 0);  | 
104  | 0  |         Ok(unsafe { Self::slice_from_bytes_unchecked(bytes) }) | 
105  | 0  |     } Unexecuted instantiation: <zerovec::ule::plain::RawBytesULE<2> as zerovec::ule::ULE>::parse_bytes_to_slice Unexecuted instantiation: <u8 as zerovec::ule::ULE>::parse_bytes_to_slice Unexecuted instantiation: <_ as zerovec::ule::ULE>::parse_bytes_to_slice  | 
106  |  |  | 
107  |  |     /// Takes a byte slice, `&[u8]`, and return it as `&[Self]` with the same lifetime, assuming  | 
108  |  |     /// that this byte slice has previously been run through [`Self::parse_bytes_to_slice()`] with  | 
109  |  |     /// success.  | 
110  |  |     ///  | 
111  |  |     /// The default implementation performs a pointer cast to the same region of memory.  | 
112  |  |     ///  | 
113  |  |     /// # Safety  | 
114  |  |     ///  | 
115  |  |     /// ## Callers  | 
116  |  |     ///  | 
117  |  |     /// Callers of this method must take care to ensure that `bytes` was previously passed through  | 
118  |  |     /// [`Self::validate_bytes()`] with success (and was not changed since then).  | 
119  |  |     ///  | 
120  |  |     /// ## Implementors  | 
121  |  |     ///  | 
122  |  |     /// Implementations of this method may call unsafe functions to cast the pointer to the correct  | 
123  |  |     /// type, assuming the "Callers" invariant above.  | 
124  |  |     ///  | 
125  |  |     /// Keep in mind that `&[Self]` and `&[u8]` may have different lengths.  | 
126  |  |     ///  | 
127  |  |     /// Safety checklist:  | 
128  |  |     ///  | 
129  |  |     /// 1. This method *must* return the same result as [`Self::parse_bytes_to_slice()`].  | 
130  |  |     /// 2. This method *must* return a slice to the same region of memory as the argument.  | 
131  |  |     #[inline]  | 
132  | 0  |     unsafe fn slice_from_bytes_unchecked(bytes: &[u8]) -> &[Self] { | 
133  | 0  |         let data = bytes.as_ptr();  | 
134  | 0  |         let len = bytes.len() / mem::size_of::<Self>();  | 
135  | 0  |         debug_assert_eq!(bytes.len() % mem::size_of::<Self>(), 0);  | 
136  | 0  |         core::slice::from_raw_parts(data as *const Self, len)  | 
137  | 0  |     } Unexecuted instantiation: <zerovec::ule::plain::RawBytesULE<2> as zerovec::ule::ULE>::slice_from_bytes_unchecked Unexecuted instantiation: <zerovec::ule::plain::RawBytesULE<3> as zerovec::ule::ULE>::slice_from_bytes_unchecked Unexecuted instantiation: <u8 as zerovec::ule::ULE>::slice_from_bytes_unchecked Unexecuted instantiation: <_ as zerovec::ule::ULE>::slice_from_bytes_unchecked  | 
138  |  |  | 
139  |  |     /// Given `&[Self]`, returns a `&[u8]` with the same lifetime.  | 
140  |  |     ///  | 
141  |  |     /// The default implementation performs a pointer cast to the same region of memory.  | 
142  |  |     ///  | 
143  |  |     /// # Safety  | 
144  |  |     ///  | 
145  |  |     /// Implementations of this method should call potentially unsafe functions to cast the  | 
146  |  |     /// pointer to the correct type.  | 
147  |  |     ///  | 
148  |  |     /// Keep in mind that `&[Self]` and `&[u8]` may have different lengths.  | 
149  |  |     #[inline]  | 
150  | 0  |     fn slice_as_bytes(slice: &[Self]) -> &[u8] { | 
151  |  |         unsafe { | 
152  | 0  |             slice::from_raw_parts(slice as *const [Self] as *const u8, mem::size_of_val(slice))  | 
153  |  |         }  | 
154  | 0  |     } Unexecuted instantiation: <zerovec::ule::plain::RawBytesULE<2> as zerovec::ule::ULE>::slice_as_bytes Unexecuted instantiation: <zerovec::ule::plain::RawBytesULE<4> as zerovec::ule::ULE>::slice_as_bytes Unexecuted instantiation: <_ as zerovec::ule::ULE>::slice_as_bytes  | 
155  |  | }  | 
156  |  |  | 
157  |  | /// A trait for any type that has a 1:1 mapping with an unaligned little-endian (ULE) type.  | 
158  |  | ///  | 
159  |  | /// If you need to implement this trait, consider using [`#[make_ule]`](crate::make_ule) instead.  | 
160  |  | pub trait AsULE: Copy { | 
161  |  |     /// The ULE type corresponding to `Self`.  | 
162  |  |     ///  | 
163  |  |     /// Types having infallible conversions from all bit values (Plain Old Data) can use  | 
164  |  |     /// `RawBytesULE` with the desired width; for example, `u32` uses `RawBytesULE<4>`.  | 
165  |  |     ///  | 
166  |  |     /// Types that are not well-defined for all bit values should implement a custom ULE.  | 
167  |  |     type ULE: ULE;  | 
168  |  |  | 
169  |  |     /// Converts from `Self` to `Self::ULE`.  | 
170  |  |     ///  | 
171  |  |     /// This function may involve byte order swapping (native-endian to little-endian).  | 
172  |  |     ///  | 
173  |  |     /// For best performance, mark your implementation of this function `#[inline]`.  | 
174  |  |     fn to_unaligned(self) -> Self::ULE;  | 
175  |  |  | 
176  |  |     /// Converts from `Self::ULE` to `Self`.  | 
177  |  |     ///  | 
178  |  |     /// This function may involve byte order swapping (little-endian to native-endian).  | 
179  |  |     ///  | 
180  |  |     /// For best performance, mark your implementation of this function `#[inline]`.  | 
181  |  |     ///  | 
182  |  |     /// # Safety  | 
183  |  |     ///  | 
184  |  |     /// This function is infallible because bit validation should have occurred when `Self::ULE`  | 
185  |  |     /// was first constructed. An implementation may therefore involve an `unsafe{}` block, like | 
186  |  |     /// `from_bytes_unchecked()`.  | 
187  |  |     fn from_unaligned(unaligned: Self::ULE) -> Self;  | 
188  |  | }  | 
189  |  |  | 
190  |  | /// A type whose byte sequence equals the byte sequence of its ULE type on  | 
191  |  | /// little-endian platforms.  | 
192  |  | ///  | 
193  |  | /// This enables certain performance optimizations, such as  | 
194  |  | /// [`ZeroVec::try_from_slice`](crate::ZeroVec::try_from_slice).  | 
195  |  | ///  | 
196  |  | /// # Implementation safety  | 
197  |  | ///  | 
198  |  | /// This trait is safe to implement if the type's ULE (as defined by `impl `[`AsULE`]` for T`)  | 
199  |  | /// has an equal byte sequence as the type itself on little-endian platforms; i.e., one where  | 
200  |  | /// `*const T` can be cast to a valid `*const T::ULE`.  | 
201  |  | pub unsafe trait EqULE: AsULE {} | 
202  |  |  | 
203  |  | /// A trait for a type where aligned slices can be cast to unaligned slices.  | 
204  |  | ///  | 
205  |  | /// Auto-implemented on all types implementing [`EqULE`].  | 
206  |  | pub trait SliceAsULE  | 
207  |  | where  | 
208  |  |     Self: AsULE + Sized,  | 
209  |  | { | 
210  |  |     /// Converts from `&[Self]` to `&[Self::ULE]` if possible.  | 
211  |  |     ///  | 
212  |  |     /// In general, this function returns `Some` on little-endian and `None` on big-endian.  | 
213  |  |     fn slice_to_unaligned(slice: &[Self]) -> Option<&[Self::ULE]>;  | 
214  |  | }  | 
215  |  |  | 
216  |  | #[cfg(target_endian = "little")]  | 
217  |  | impl<T> SliceAsULE for T  | 
218  |  | where  | 
219  |  |     T: EqULE,  | 
220  |  | { | 
221  |  |     #[inline]  | 
222  | 0  |     fn slice_to_unaligned(slice: &[Self]) -> Option<&[Self::ULE]> { | 
223  |  |         // This is safe because on little-endian platforms, the byte sequence of &[T]  | 
224  |  |         // is equivalent to the byte sequence of &[T::ULE] by the contract of EqULE,  | 
225  |  |         // and &[T::ULE] has equal or looser alignment than &[T].  | 
226  | 0  |         let ule_slice =  | 
227  | 0  |             unsafe { core::slice::from_raw_parts(slice.as_ptr() as *const Self::ULE, slice.len()) }; | 
228  | 0  |         Some(ule_slice)  | 
229  | 0  |     }  | 
230  |  | }  | 
231  |  |  | 
232  |  | #[cfg(not(target_endian = "little"))]  | 
233  |  | impl<T> SliceAsULE for T  | 
234  |  | where  | 
235  |  |     T: EqULE,  | 
236  |  | { | 
237  |  |     #[inline]  | 
238  |  |     fn slice_to_unaligned(_: &[Self]) -> Option<&[Self::ULE]> { | 
239  |  |         None  | 
240  |  |     }  | 
241  |  | }  | 
242  |  |  | 
243  |  | /// Variable-width, byte-aligned data that can be cast to and from a little-endian byte slice.  | 
244  |  | ///  | 
245  |  | /// If you need to implement this trait, consider using [`#[make_varule]`](crate::make_varule) or  | 
246  |  | ///  [`#[derive(VarULE)]`](macro@VarULE) instead.  | 
247  |  | ///  | 
248  |  | /// This trait is mostly for unsized types like `str` and `[T]`. It can be implemented on sized types;  | 
249  |  | /// however, it is much more preferable to use [`ULE`] for that purpose. The [`custom`] module contains  | 
250  |  | /// additional documentation on how this type can be implemented on custom types.  | 
251  |  | ///  | 
252  |  | /// If deserialization with `VarZeroVec` is desired is recommended to implement `Deserialize` for  | 
253  |  | /// `Box<T>` (serde does not do this automatically for unsized `T`).  | 
254  |  | ///  | 
255  |  | /// For convenience it is typically desired to implement [`EncodeAsVarULE`] and [`ZeroFrom`](zerofrom::ZeroFrom)  | 
256  |  | /// on some stack type to convert to and from the ULE type efficiently when necessary.  | 
257  |  | ///  | 
258  |  | /// # Safety  | 
259  |  | ///  | 
260  |  | /// Safety checklist for `VarULE`:  | 
261  |  | ///  | 
262  |  | /// 1. The type *must not* include any uninitialized or padding bytes.  | 
263  |  | /// 2. The type must have an alignment of 1 byte.  | 
264  |  | /// 3. The impl of [`VarULE::validate_bytes()`] *must* return an error if the given byte slice  | 
265  |  | ///    would not represent a valid slice of this type.  | 
266  |  | /// 4. The impl of [`VarULE::validate_bytes()`] *must* return an error if the given byte slice  | 
267  |  | ///    cannot be used in its entirety.  | 
268  |  | /// 5. The impl of [`VarULE::from_bytes_unchecked()`] must produce a reference to the same  | 
269  |  | ///    underlying data assuming that the given bytes previously passed validation.  | 
270  |  | /// 6. All other methods *must* be left with their default impl, or else implemented according to  | 
271  |  | ///    their respective safety guidelines.  | 
272  |  | /// 7. Acknowledge the following note about the equality invariant.  | 
273  |  | ///  | 
274  |  | /// If the ULE type is a struct only containing other ULE/VarULE types (or other types which satisfy invariants 1 and 2,  | 
275  |  | /// like `[u8; N]`), invariants 1 and 2 can be achieved via `#[repr(C, packed)]` or `#[repr(transparent)]`.  | 
276  |  | ///  | 
277  |  | /// # Equality invariant  | 
278  |  | ///  | 
279  |  | /// A non-safety invariant is that if `Self` implements `PartialEq`, the it *must* be logically  | 
280  |  | /// equivalent to byte equality on [`Self::as_bytes()`].  | 
281  |  | ///  | 
282  |  | /// It may be necessary to introduce a "canonical form" of the ULE if logical equality does not  | 
283  |  | /// equal byte equality. In such a case, [`Self::validate_bytes()`] should return an error  | 
284  |  | /// for any values that are not in canonical form. For example, the decimal strings "1.23e4" and  | 
285  |  | /// "12.3e3" are logically equal, but not byte-for-byte equal, so we could define a canonical form  | 
286  |  | /// where only a single digit is allowed before `.`.  | 
287  |  | ///  | 
288  |  | /// There may also be cases where a `VarULE` has muiltiple canonical forms, such as a faster  | 
289  |  | /// version and a smaller version. The cleanest way to handle this case would be separate types.  | 
290  |  | /// However, if this is not feasible, then the application should ensure that the data it is  | 
291  |  | /// deserializing is in the expected form. For example, if the data is being loaded from an  | 
292  |  | /// external source, then requests could carry information about the expected form of the data.  | 
293  |  | ///  | 
294  |  | /// Failure to follow this invariant will cause surprising behavior in `PartialEq`, which may  | 
295  |  | /// result in unpredictable operations on `ZeroVec`, `VarZeroVec`, and `ZeroMap`.  | 
296  |  | pub unsafe trait VarULE: 'static { | 
297  |  |     /// Validates a byte slice, `&[u8]`.  | 
298  |  |     ///  | 
299  |  |     /// If `Self` is not well-defined for all possible bit values, the bytes should be validated.  | 
300  |  |     /// If the bytes can be transmuted, *in their entirety*, to a valid `&Self`, then `Ok` should  | 
301  |  |     /// be returned; otherwise, `Self::Error` should be returned.  | 
302  |  |     fn validate_bytes(_bytes: &[u8]) -> Result<(), UleError>;  | 
303  |  |  | 
304  |  |     /// Parses a byte slice, `&[u8]`, and return it as `&Self` with the same lifetime.  | 
305  |  |     ///  | 
306  |  |     /// If `Self` is not well-defined for all possible bit values, the bytes should be validated,  | 
307  |  |     /// and an error should be returned in the same cases as [`Self::validate_bytes()`].  | 
308  |  |     ///  | 
309  |  |     /// The default implementation executes [`Self::validate_bytes()`] followed by  | 
310  |  |     /// [`Self::from_bytes_unchecked`].  | 
311  |  |     ///  | 
312  |  |     /// Note: The following equality should hold: `size_of_val(result) == size_of_val(bytes)`,  | 
313  |  |     /// where `result` is the successful return value of the method. This means that the return  | 
314  |  |     /// value spans the entire byte slice.  | 
315  | 0  |     fn parse_bytes(bytes: &[u8]) -> Result<&Self, UleError> { | 
316  | 0  |         Self::validate_bytes(bytes)?;  | 
317  | 0  |         let result = unsafe { Self::from_bytes_unchecked(bytes) }; | 
318  | 0  |         debug_assert_eq!(mem::size_of_val(result), mem::size_of_val(bytes));  | 
319  | 0  |         Ok(result)  | 
320  | 0  |     }  | 
321  |  |  | 
322  |  |     /// Takes a byte slice, `&[u8]`, and return it as `&Self` with the same lifetime, assuming  | 
323  |  |     /// that this byte slice has previously been run through [`Self::parse_bytes()`] with  | 
324  |  |     /// success.  | 
325  |  |     ///  | 
326  |  |     /// # Safety  | 
327  |  |     ///  | 
328  |  |     /// ## Callers  | 
329  |  |     ///  | 
330  |  |     /// Callers of this method must take care to ensure that `bytes` was previously passed through  | 
331  |  |     /// [`Self::validate_bytes()`] with success (and was not changed since then).  | 
332  |  |     ///  | 
333  |  |     /// ## Implementors  | 
334  |  |     ///  | 
335  |  |     /// Implementations of this method may call unsafe functions to cast the pointer to the correct  | 
336  |  |     /// type, assuming the "Callers" invariant above.  | 
337  |  |     ///  | 
338  |  |     /// Safety checklist:  | 
339  |  |     ///  | 
340  |  |     /// 1. This method *must* return the same result as [`Self::parse_bytes()`].  | 
341  |  |     /// 2. This method *must* return a slice to the same region of memory as the argument.  | 
342  |  |     unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self;  | 
343  |  |  | 
344  |  |     /// Given `&Self`, returns a `&[u8]` with the same lifetime.  | 
345  |  |     ///  | 
346  |  |     /// The default implementation performs a pointer cast to the same region of memory.  | 
347  |  |     ///  | 
348  |  |     /// # Safety  | 
349  |  |     ///  | 
350  |  |     /// Implementations of this method should call potentially unsafe functions to cast the  | 
351  |  |     /// pointer to the correct type.  | 
352  |  |     #[inline]  | 
353  | 0  |     fn as_bytes(&self) -> &[u8] { | 
354  | 0  |         unsafe { slice::from_raw_parts(self as *const Self as *const u8, mem::size_of_val(self)) } | 
355  | 0  |     } Unexecuted instantiation: <zerovec::zerovec::slice::ZeroSlice<potential_utf::uchar::PotentialCodePoint> as zerovec::ule::VarULE>::as_bytes Unexecuted instantiation: <icu_collections::codepointinvliststringlist::CodePointInversionListAndStringListULE as zerovec::ule::VarULE>::as_bytes Unexecuted instantiation: <icu_collections::codepointinvlist::cpinvlist::CodePointInversionListULE as zerovec::ule::VarULE>::as_bytes Unexecuted instantiation: <_ as zerovec::ule::VarULE>::as_bytes  | 
356  |  |  | 
357  |  |     /// Allocate on the heap as a `Box<T>`  | 
358  |  |     #[inline]  | 
359  |  |     #[cfg(feature = "alloc")]  | 
360  | 0  |     fn to_boxed(&self) -> alloc::boxed::Box<Self> { | 
361  |  |         use alloc::borrow::ToOwned;  | 
362  |  |         use alloc::boxed::Box;  | 
363  |  |         use core::alloc::Layout;  | 
364  | 0  |         let bytesvec = self.as_bytes().to_owned().into_boxed_slice();  | 
365  | 0  |         let bytesvec = mem::ManuallyDrop::new(bytesvec);  | 
366  |  |         unsafe { | 
367  |  |             // Get the pointer representation  | 
368  | 0  |             let ptr: *mut Self = Self::from_bytes_unchecked(&bytesvec) as *const Self as *mut Self;  | 
369  | 0  |             assert_eq!(Layout::for_value(&*ptr), Layout::for_value(&**bytesvec));  | 
370  |  |             // Transmute the pointer to an owned pointer  | 
371  | 0  |             Box::from_raw(ptr)  | 
372  |  |         }  | 
373  | 0  |     }  | 
374  |  | }  | 
375  |  |  | 
376  |  | // Proc macro reexports  | 
377  |  | //  | 
378  |  | // These exist so that our docs can use intra-doc links.  | 
379  |  | // Due to quirks of how rustdoc does documentation on reexports, these must be in this module and not reexported from  | 
380  |  | // a submodule  | 
381  |  |  | 
382  |  | /// Custom derive for [`ULE`].  | 
383  |  | ///  | 
384  |  | /// This can be attached to [`Copy`] structs containing only [`ULE`] types.  | 
385  |  | ///  | 
386  |  | /// Most of the time, it is recommended one use [`#[make_ule]`](crate::make_ule) instead of defining  | 
387  |  | /// a custom ULE type.  | 
388  |  | #[cfg(feature = "derive")]  | 
389  |  | pub use zerovec_derive::ULE;  | 
390  |  |  | 
391  |  | /// Custom derive for [`VarULE`]  | 
392  |  | ///  | 
393  |  | /// This can be attached to structs containing only [`ULE`] types with one [`VarULE`] type at the end.  | 
394  |  | ///  | 
395  |  | /// Most of the time, it is recommended one use [`#[make_varule]`](crate::make_varule) instead of defining  | 
396  |  | /// a custom [`VarULE`] type.  | 
397  |  | #[cfg(feature = "derive")]  | 
398  |  | pub use zerovec_derive::VarULE;  | 
399  |  |  | 
400  |  | /// An error type to be used for decoding slices of ULE types  | 
401  |  | #[derive(Copy, Clone, Debug, PartialEq, Eq)]  | 
402  |  | #[non_exhaustive]  | 
403  |  | pub enum UleError { | 
404  |  |     /// Attempted to parse a buffer into a slice of the given ULE type but its  | 
405  |  |     /// length was not compatible.  | 
406  |  |     ///  | 
407  |  |     /// Typically created by a [`ULE`] impl via [`UleError::length()`].  | 
408  |  |     ///  | 
409  |  |     /// [`ULE`]: crate::ule::ULE  | 
410  |  |     InvalidLength { ty: &'static str, len: usize }, | 
411  |  |     /// The byte sequence provided for `ty` failed to parse correctly in the  | 
412  |  |     /// given ULE type.  | 
413  |  |     ///  | 
414  |  |     /// Typically created by a [`ULE`] impl via [`UleError::parse()`].  | 
415  |  |     ///  | 
416  |  |     /// [`ULE`]: crate::ule::ULE  | 
417  |  |     ParseError { ty: &'static str }, | 
418  |  | }  | 
419  |  |  | 
420  |  | impl fmt::Display for UleError { | 
421  | 0  |     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { | 
422  | 0  |         match *self { | 
423  | 0  |             UleError::InvalidLength { ty, len } => { | 
424  | 0  |                 write!(f, "Invalid length {len} for slice of type {ty}") | 
425  |  |             }  | 
426  | 0  |             UleError::ParseError { ty } => { | 
427  | 0  |                 write!(f, "Could not parse bytes to slice of type {ty}") | 
428  |  |             }  | 
429  |  |         }  | 
430  | 0  |     }  | 
431  |  | }  | 
432  |  |  | 
433  |  | impl UleError { | 
434  |  |     /// Construct a parse error for the given type  | 
435  | 0  |     pub fn parse<T: ?Sized + 'static>() -> UleError { | 
436  | 0  |         UleError::ParseError { | 
437  | 0  |             ty: any::type_name::<T>(),  | 
438  | 0  |         }  | 
439  | 0  |     } Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::subtags::Subtag> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::subtags::region::Region> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::subtags::script::Script> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::subtags::variant::Variant> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::subtags::language::Language> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::extensions::private::other::Subtag> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::extensions::unicode::subdivision::SubdivisionSuffix> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::extensions::unicode::key::Key> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::extensions::unicode::attribute::Attribute> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<icu_locale_core::extensions::transform::key::Key> Unexecuted instantiation: <zerovec::ule::UleError>::parse::<_>  | 
440  |  |  | 
441  |  |     /// Construct an "invalid length" error for the given type and length  | 
442  | 0  |     pub fn length<T: ?Sized + 'static>(len: usize) -> UleError { | 
443  | 0  |         UleError::InvalidLength { | 
444  | 0  |             ty: any::type_name::<T>(),  | 
445  | 0  |             len,  | 
446  | 0  |         }  | 
447  | 0  |     } Unexecuted instantiation: <zerovec::ule::UleError>::length::<zerovec::ule::plain::RawBytesULE<2>> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::subtags::Subtag> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::subtags::region::Region> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::subtags::script::Script> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::subtags::variant::Variant> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::subtags::language::Language> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::extensions::private::other::Subtag> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::extensions::unicode::subdivision::SubdivisionSuffix> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::extensions::unicode::key::Key> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::extensions::unicode::attribute::Attribute> Unexecuted instantiation: <zerovec::ule::UleError>::length::<icu_locale_core::extensions::transform::key::Key> Unexecuted instantiation: <zerovec::ule::UleError>::length::<_>  | 
448  |  | }  | 
449  |  |  | 
450  |  | impl core::error::Error for UleError {} |