/rust/registry/src/index.crates.io-6f17d22bba15001f/bytemuck-1.23.1/src/lib.rs
Line | Count | Source (jump to first uncovered line) |
1 | | #![no_std] |
2 | | #![warn(missing_docs)] |
3 | | #![allow(unused_mut)] |
4 | | #![allow(clippy::match_like_matches_macro)] |
5 | | #![allow(clippy::uninlined_format_args)] |
6 | | #![allow(clippy::result_unit_err)] |
7 | | #![allow(clippy::type_complexity)] |
8 | | #![cfg_attr(feature = "nightly_docs", feature(doc_cfg))] |
9 | | #![cfg_attr(feature = "nightly_portable_simd", feature(portable_simd))] |
10 | | #![cfg_attr(feature = "nightly_float", feature(f16, f128))] |
11 | | #![cfg_attr( |
12 | | all( |
13 | | feature = "nightly_stdsimd", |
14 | | any(target_arch = "x86_64", target_arch = "x86") |
15 | | ), |
16 | | feature(stdarch_x86_avx512) |
17 | | )] |
18 | | |
19 | | //! This crate gives small utilities for casting between plain data types. |
20 | | //! |
21 | | //! ## Basics |
22 | | //! |
23 | | //! Data comes in five basic forms in Rust, so we have five basic casting |
24 | | //! functions: |
25 | | //! |
26 | | //! * `T` uses [`cast`] |
27 | | //! * `&T` uses [`cast_ref`] |
28 | | //! * `&mut T` uses [`cast_mut`] |
29 | | //! * `&[T]` uses [`cast_slice`] |
30 | | //! * `&mut [T]` uses [`cast_slice_mut`] |
31 | | //! |
32 | | //! Depending on the function, the [`NoUninit`] and/or [`AnyBitPattern`] traits |
33 | | //! are used to maintain memory safety. |
34 | | //! |
35 | | //! **Historical Note:** When the crate first started the [`Pod`] trait was used |
36 | | //! instead, and so you may hear people refer to that, but it has the strongest |
37 | | //! requirements and people eventually wanted the more fine-grained system, so |
38 | | //! here we are. All types that impl `Pod` have a blanket impl to also support |
39 | | //! `NoUninit` and `AnyBitPattern`. The traits unfortunately do not have a |
40 | | //! perfectly clean hierarchy for semver reasons. |
41 | | //! |
42 | | //! ## Failures |
43 | | //! |
44 | | //! Some casts will never fail, and other casts might fail. |
45 | | //! |
46 | | //! * `cast::<u32, f32>` always works (and [`f32::from_bits`]). |
47 | | //! * `cast_ref::<[u8; 4], u32>` might fail if the specific array reference |
48 | | //! given at runtime doesn't have alignment 4. |
49 | | //! |
50 | | //! In addition to the "normal" forms of each function, which will panic on |
51 | | //! invalid input, there's also `try_` versions which will return a `Result`. |
52 | | //! |
53 | | //! If you would like to statically ensure that a cast will work at runtime you |
54 | | //! can use the `must_cast` crate feature and the `must_` casting functions. A |
55 | | //! "must cast" that can't be statically known to be valid will cause a |
56 | | //! compilation error (and sometimes a very hard to read compilation error). |
57 | | //! |
58 | | //! ## Using Your Own Types |
59 | | //! |
60 | | //! All the functions listed above are guarded by the [`Pod`] trait, which is a |
61 | | //! sub-trait of the [`Zeroable`] trait. |
62 | | //! |
63 | | //! If you enable the crate's `derive` feature then these traits can be derived |
64 | | //! on your own types. The derive macros will perform the necessary checks on |
65 | | //! your type declaration, and trigger an error if your type does not qualify. |
66 | | //! |
67 | | //! The derive macros might not cover all edge cases, and sometimes they will |
68 | | //! error when actually everything is fine. As a last resort you can impl these |
69 | | //! traits manually. However, these traits are `unsafe`, and you should |
70 | | //! carefully read the requirements before using a manual implementation. |
71 | | //! |
72 | | //! ## Cargo Features |
73 | | //! |
74 | | //! The crate supports Rust 1.34 when no features are enabled, and so there's |
75 | | //! cargo features for thing that you might consider "obvious". |
76 | | //! |
77 | | //! The cargo features **do not** promise any particular MSRV, and they may |
78 | | //! increase their MSRV in new versions. |
79 | | //! |
80 | | //! * `derive`: Provide derive macros for the various traits. |
81 | | //! * `extern_crate_alloc`: Provide utilities for `alloc` related types such as |
82 | | //! Box and Vec. |
83 | | //! * `zeroable_maybe_uninit` and `zeroable_atomics`: Provide more [`Zeroable`] |
84 | | //! impls. |
85 | | //! * `pod_saturating`: Provide more [`Pod`] and [`Zeroable`] impls. |
86 | | //! * `wasm_simd` and `aarch64_simd`: Support more SIMD types. |
87 | | //! * `min_const_generics`: Provides appropriate impls for arrays of all lengths |
88 | | //! instead of just for a select list of array lengths. |
89 | | //! * `must_cast`: Provides the `must_` functions, which will compile error if |
90 | | //! the requested cast can't be statically verified. |
91 | | //! * `const_zeroed`: Provides a const version of the `zeroed` function. |
92 | | //! |
93 | | //! ## Related Crates |
94 | | //! |
95 | | //! - [`pack1`](https://docs.rs/pack1), which contains `bytemuck`-compatible |
96 | | //! packed little-endian, big-endian and native-endian integer and floating |
97 | | //! point number types. |
98 | | |
99 | | #[cfg(all(target_arch = "aarch64", feature = "aarch64_simd"))] |
100 | | use core::arch::aarch64; |
101 | | #[cfg(all(target_arch = "wasm32", feature = "wasm_simd"))] |
102 | | use core::arch::wasm32; |
103 | | #[cfg(target_arch = "x86")] |
104 | | use core::arch::x86; |
105 | | #[cfg(target_arch = "x86_64")] |
106 | | use core::arch::x86_64; |
107 | | // |
108 | | use core::{ |
109 | | marker::*, |
110 | | mem::{align_of, size_of}, |
111 | | num::*, |
112 | | ptr::*, |
113 | | }; |
114 | | |
115 | | // Used from macros to ensure we aren't using some locally defined name and |
116 | | // actually are referencing libcore. This also would allow pre-2018 edition |
117 | | // crates to use our macros, but I'm not sure how important that is. |
118 | | #[doc(hidden)] |
119 | | pub use ::core as __core; |
120 | | |
121 | | #[cfg(not(feature = "min_const_generics"))] |
122 | | macro_rules! impl_unsafe_marker_for_array { |
123 | | ( $marker:ident , $( $n:expr ),* ) => { |
124 | | $(unsafe impl<T> $marker for [T; $n] where T: $marker {})* |
125 | | } |
126 | | } |
127 | | |
128 | | /// A macro to transmute between two types without requiring knowing size |
129 | | /// statically. |
130 | | macro_rules! transmute { |
131 | | ($val:expr) => { |
132 | | ::core::mem::transmute_copy(&::core::mem::ManuallyDrop::new($val)) |
133 | | }; |
134 | | // This arm is for use in const contexts, where the borrow required to use |
135 | | // transmute_copy poses an issue since the compiler hedges that the type |
136 | | // being borrowed could have interior mutability. |
137 | | ($srcty:ty; $dstty:ty; $val:expr) => {{ |
138 | | #[repr(C)] |
139 | | union Transmute<A, B> { |
140 | | src: ::core::mem::ManuallyDrop<A>, |
141 | | dst: ::core::mem::ManuallyDrop<B>, |
142 | | } |
143 | | ::core::mem::ManuallyDrop::into_inner( |
144 | | Transmute::<$srcty, $dstty> { src: ::core::mem::ManuallyDrop::new($val) } |
145 | | .dst, |
146 | | ) |
147 | | }}; |
148 | | } |
149 | | |
150 | | /// A macro to implement marker traits for various simd types. |
151 | | /// #[allow(unused)] because the impls are only compiled on relevant platforms |
152 | | /// with relevant cargo features enabled. |
153 | | #[allow(unused)] |
154 | | macro_rules! impl_unsafe_marker_for_simd { |
155 | | ($(#[cfg($cfg_predicate:meta)])? unsafe impl $trait:ident for $platform:ident :: {}) => {}; |
156 | | ($(#[cfg($cfg_predicate:meta)])? unsafe impl $trait:ident for $platform:ident :: { $first_type:ident $(, $types:ident)* $(,)? }) => { |
157 | | $( #[cfg($cfg_predicate)] )? |
158 | | $( #[cfg_attr(feature = "nightly_docs", doc(cfg($cfg_predicate)))] )? |
159 | | unsafe impl $trait for $platform::$first_type {} |
160 | | $( #[cfg($cfg_predicate)] )? // To prevent recursion errors if nothing is going to be expanded anyway. |
161 | | impl_unsafe_marker_for_simd!($( #[cfg($cfg_predicate)] )? unsafe impl $trait for $platform::{ $( $types ),* }); |
162 | | }; |
163 | | } |
164 | | |
165 | | /// A macro for conditionally const-ifying a function. |
166 | | /// #[allow(unused)] because currently it is only used with the `must_cast` feature. |
167 | | #[allow(unused)] |
168 | | macro_rules! maybe_const_fn { |
169 | | ( |
170 | | #[cfg($cfg_predicate:meta)] |
171 | | $(#[$attr:meta])* |
172 | | $vis:vis $(unsafe $($unsafe:lifetime)?)? fn $name:ident $($rest:tt)* |
173 | | ) => { |
174 | | #[cfg($cfg_predicate)] |
175 | | $(#[$attr])* |
176 | | $vis const $(unsafe $($unsafe)?)? fn $name $($rest)* |
177 | | |
178 | | #[cfg(not($cfg_predicate))] |
179 | | $(#[$attr])* |
180 | | $vis $(unsafe $($unsafe)?)? fn $name $($rest)* |
181 | | }; |
182 | | } |
183 | | |
184 | | #[cfg(feature = "extern_crate_std")] |
185 | | extern crate std; |
186 | | |
187 | | #[cfg(feature = "extern_crate_alloc")] |
188 | | extern crate alloc; |
189 | | #[cfg(feature = "extern_crate_alloc")] |
190 | | #[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_alloc")))] |
191 | | pub mod allocation; |
192 | | #[cfg(feature = "extern_crate_alloc")] |
193 | | pub use allocation::*; |
194 | | |
195 | | mod anybitpattern; |
196 | | pub use anybitpattern::*; |
197 | | |
198 | | pub mod checked; |
199 | | pub use checked::CheckedBitPattern; |
200 | | |
201 | | mod internal; |
202 | | |
203 | | mod zeroable; |
204 | | pub use zeroable::*; |
205 | | mod zeroable_in_option; |
206 | | pub use zeroable_in_option::*; |
207 | | |
208 | | mod pod; |
209 | | pub use pod::*; |
210 | | mod pod_in_option; |
211 | | pub use pod_in_option::*; |
212 | | |
213 | | #[cfg(feature = "must_cast")] |
214 | | mod must; |
215 | | #[cfg(feature = "must_cast")] |
216 | | #[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "must_cast")))] |
217 | | pub use must::*; |
218 | | |
219 | | mod no_uninit; |
220 | | pub use no_uninit::*; |
221 | | |
222 | | mod contiguous; |
223 | | pub use contiguous::*; |
224 | | |
225 | | mod offset_of; |
226 | | // ^ no import, the module only has a macro_rules, which are cursed and don't |
227 | | // follow normal import/export rules. |
228 | | |
229 | | mod transparent; |
230 | | pub use transparent::*; |
231 | | |
232 | | #[cfg(feature = "derive")] |
233 | | #[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "derive")))] |
234 | | pub use bytemuck_derive::{ |
235 | | AnyBitPattern, ByteEq, ByteHash, CheckedBitPattern, Contiguous, NoUninit, |
236 | | Pod, TransparentWrapper, Zeroable, |
237 | | }; |
238 | | |
239 | | /// The things that can go wrong when casting between [`Pod`] data forms. |
240 | | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
241 | | pub enum PodCastError { |
242 | | /// You tried to cast a reference into a reference to a type with a higher |
243 | | /// alignment requirement but the input reference wasn't aligned. |
244 | | TargetAlignmentGreaterAndInputNotAligned, |
245 | | /// If the element size of a slice changes, then the output slice changes |
246 | | /// length accordingly. If the output slice wouldn't be a whole number of |
247 | | /// elements, then the conversion fails. |
248 | | OutputSliceWouldHaveSlop, |
249 | | /// When casting an individual `T`, `&T`, or `&mut T` value the |
250 | | /// source size and destination size must be an exact match. |
251 | | SizeMismatch, |
252 | | /// For this type of cast the alignments must be exactly the same and they |
253 | | /// were not so now you're sad. |
254 | | /// |
255 | | /// This error is generated **only** by operations that cast allocated types |
256 | | /// (such as `Box` and `Vec`), because in that case the alignment must stay |
257 | | /// exact. |
258 | | AlignmentMismatch, |
259 | | } |
260 | | #[cfg(not(target_arch = "spirv"))] |
261 | | impl core::fmt::Display for PodCastError { |
262 | 0 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
263 | 0 | write!(f, "{:?}", self) |
264 | 0 | } |
265 | | } |
266 | | #[cfg(feature = "extern_crate_std")] |
267 | | #[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_std")))] |
268 | | impl std::error::Error for PodCastError {} |
269 | | |
270 | | // Rust 1.81+ |
271 | | #[cfg(all(feature = "impl_core_error", not(feature = "extern_crate_std")))] |
272 | | impl core::error::Error for PodCastError {} |
273 | | |
274 | | /// Re-interprets `&T` as `&[u8]`. |
275 | | /// |
276 | | /// Any ZST becomes an empty slice, and in that case the pointer value of that |
277 | | /// empty slice might not match the pointer value of the input reference. |
278 | | #[inline] |
279 | 0 | pub fn bytes_of<T: NoUninit>(t: &T) -> &[u8] { |
280 | 0 | unsafe { internal::bytes_of(t) } |
281 | 0 | } |
282 | | |
283 | | /// Re-interprets `&mut T` as `&mut [u8]`. |
284 | | /// |
285 | | /// Any ZST becomes an empty slice, and in that case the pointer value of that |
286 | | /// empty slice might not match the pointer value of the input reference. |
287 | | #[inline] |
288 | 0 | pub fn bytes_of_mut<T: NoUninit + AnyBitPattern>(t: &mut T) -> &mut [u8] { |
289 | 0 | unsafe { internal::bytes_of_mut(t) } |
290 | 0 | } |
291 | | |
292 | | /// Re-interprets `&[u8]` as `&T`. |
293 | | /// |
294 | | /// ## Panics |
295 | | /// |
296 | | /// This is like [`try_from_bytes`] but will panic on error. |
297 | | #[inline] |
298 | | #[cfg_attr(feature = "track_caller", track_caller)] |
299 | 0 | pub fn from_bytes<T: AnyBitPattern>(s: &[u8]) -> &T { |
300 | 0 | unsafe { internal::from_bytes(s) } |
301 | 0 | } |
302 | | |
303 | | /// Re-interprets `&mut [u8]` as `&mut T`. |
304 | | /// |
305 | | /// ## Panics |
306 | | /// |
307 | | /// This is like [`try_from_bytes_mut`] but will panic on error. |
308 | | #[inline] |
309 | | #[cfg_attr(feature = "track_caller", track_caller)] |
310 | 0 | pub fn from_bytes_mut<T: NoUninit + AnyBitPattern>(s: &mut [u8]) -> &mut T { |
311 | 0 | unsafe { internal::from_bytes_mut(s) } |
312 | 0 | } |
313 | | |
314 | | /// Reads from the bytes as if they were a `T`. |
315 | | /// |
316 | | /// Unlike [`from_bytes`], the slice doesn't need to respect alignment of `T`, |
317 | | /// only sizes must match. |
318 | | /// |
319 | | /// ## Failure |
320 | | /// * If the `bytes` length is not equal to `size_of::<T>()`. |
321 | | #[inline] |
322 | 0 | pub fn try_pod_read_unaligned<T: AnyBitPattern>( |
323 | 0 | bytes: &[u8], |
324 | 0 | ) -> Result<T, PodCastError> { |
325 | 0 | unsafe { internal::try_pod_read_unaligned(bytes) } |
326 | 0 | } |
327 | | |
328 | | /// Reads the slice into a `T` value. |
329 | | /// |
330 | | /// Unlike [`from_bytes`], the slice doesn't need to respect alignment of `T`, |
331 | | /// only sizes must match. |
332 | | /// |
333 | | /// ## Panics |
334 | | /// * This is like `try_pod_read_unaligned` but will panic on failure. |
335 | | #[inline] |
336 | | #[cfg_attr(feature = "track_caller", track_caller)] |
337 | 0 | pub fn pod_read_unaligned<T: AnyBitPattern>(bytes: &[u8]) -> T { |
338 | 0 | unsafe { internal::pod_read_unaligned(bytes) } |
339 | 0 | } Unexecuted instantiation: bytemuck::pod_read_unaligned::<_> Unexecuted instantiation: bytemuck::pod_read_unaligned::<[f32; 3]> Unexecuted instantiation: bytemuck::pod_read_unaligned::<[f32; 4]> Unexecuted instantiation: bytemuck::pod_read_unaligned::<[f32; 3]> Unexecuted instantiation: bytemuck::pod_read_unaligned::<[f32; 4]> |
340 | | |
341 | | /// Re-interprets `&[u8]` as `&T`. |
342 | | /// |
343 | | /// ## Failure |
344 | | /// |
345 | | /// * If the slice isn't aligned for the new type |
346 | | /// * If the slice's length isn’t exactly the size of the new type |
347 | | #[inline] |
348 | 0 | pub fn try_from_bytes<T: AnyBitPattern>(s: &[u8]) -> Result<&T, PodCastError> { |
349 | 0 | unsafe { internal::try_from_bytes(s) } |
350 | 0 | } |
351 | | |
352 | | /// Re-interprets `&mut [u8]` as `&mut T`. |
353 | | /// |
354 | | /// ## Failure |
355 | | /// |
356 | | /// * If the slice isn't aligned for the new type |
357 | | /// * If the slice's length isn’t exactly the size of the new type |
358 | | #[inline] |
359 | 0 | pub fn try_from_bytes_mut<T: NoUninit + AnyBitPattern>( |
360 | 0 | s: &mut [u8], |
361 | 0 | ) -> Result<&mut T, PodCastError> { |
362 | 0 | unsafe { internal::try_from_bytes_mut(s) } |
363 | 0 | } |
364 | | |
365 | | /// Cast `A` into `B` |
366 | | /// |
367 | | /// ## Panics |
368 | | /// |
369 | | /// * This is like [`try_cast`], but will panic on a size mismatch. |
370 | | #[inline] |
371 | | #[cfg_attr(feature = "track_caller", track_caller)] |
372 | 0 | pub fn cast<A: NoUninit, B: AnyBitPattern>(a: A) -> B { |
373 | 0 | unsafe { internal::cast(a) } |
374 | 0 | } Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<_, _> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 3], [u8; 4]> Unexecuted instantiation: bytemuck::cast::<[u8; 4], [u8; 4]> |
375 | | |
376 | | /// Cast `&mut A` into `&mut B`. |
377 | | /// |
378 | | /// ## Panics |
379 | | /// |
380 | | /// This is [`try_cast_mut`] but will panic on error. |
381 | | #[inline] |
382 | | #[cfg_attr(feature = "track_caller", track_caller)] |
383 | 0 | pub fn cast_mut<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>( |
384 | 0 | a: &mut A, |
385 | 0 | ) -> &mut B { |
386 | 0 | unsafe { internal::cast_mut(a) } |
387 | 0 | } |
388 | | |
389 | | /// Cast `&A` into `&B`. |
390 | | /// |
391 | | /// ## Panics |
392 | | /// |
393 | | /// This is [`try_cast_ref`] but will panic on error. |
394 | | #[inline] |
395 | | #[cfg_attr(feature = "track_caller", track_caller)] |
396 | 0 | pub fn cast_ref<A: NoUninit, B: AnyBitPattern>(a: &A) -> &B { |
397 | 0 | unsafe { internal::cast_ref(a) } |
398 | 0 | } |
399 | | |
400 | | /// Cast `&[A]` into `&[B]`. |
401 | | /// |
402 | | /// ## Panics |
403 | | /// |
404 | | /// This is [`try_cast_slice`] but will panic on error. |
405 | | #[inline] |
406 | | #[cfg_attr(feature = "track_caller", track_caller)] |
407 | 82.1M | pub fn cast_slice<A: NoUninit, B: AnyBitPattern>(a: &[A]) -> &[B] { |
408 | 82.1M | unsafe { internal::cast_slice(a) } |
409 | 82.1M | } Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> bytemuck::cast_slice::<f32, u8> Line | Count | Source | 407 | 82.1M | pub fn cast_slice<A: NoUninit, B: AnyBitPattern>(a: &[A]) -> &[B] { | 408 | 82.1M | unsafe { internal::cast_slice(a) } | 409 | 82.1M | } |
bytemuck::cast_slice::<u8, u8> Line | Count | Source | 407 | 1.91k | pub fn cast_slice<A: NoUninit, B: AnyBitPattern>(a: &[A]) -> &[B] { | 408 | 1.91k | unsafe { internal::cast_slice(a) } | 409 | 1.91k | } |
Unexecuted instantiation: bytemuck::cast_slice::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice::<_, _> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> Unexecuted instantiation: bytemuck::cast_slice::<i8, u8> Unexecuted instantiation: bytemuck::cast_slice::<f64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice::<i32, u8> Unexecuted instantiation: bytemuck::cast_slice::<u32, u8> Unexecuted instantiation: bytemuck::cast_slice::<i16, u8> Unexecuted instantiation: bytemuck::cast_slice::<i64, u8> Unexecuted instantiation: bytemuck::cast_slice::<u64, u8> |
410 | | |
411 | | /// Cast `&mut [A]` into `&mut [B]`. |
412 | | /// |
413 | | /// ## Panics |
414 | | /// |
415 | | /// This is [`try_cast_slice_mut`] but will panic on error. |
416 | | #[inline] |
417 | | #[cfg_attr(feature = "track_caller", track_caller)] |
418 | 11.9k | pub fn cast_slice_mut< |
419 | 11.9k | A: NoUninit + AnyBitPattern, |
420 | 11.9k | B: NoUninit + AnyBitPattern, |
421 | 11.9k | >( |
422 | 11.9k | a: &mut [A], |
423 | 11.9k | ) -> &mut [B] { |
424 | 11.9k | unsafe { internal::cast_slice_mut(a) } |
425 | 11.9k | } Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<_, _> Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> bytemuck::cast_slice_mut::<u8, u8> Line | Count | Source | 418 | 1.44k | pub fn cast_slice_mut< | 419 | 1.44k | A: NoUninit + AnyBitPattern, | 420 | 1.44k | B: NoUninit + AnyBitPattern, | 421 | 1.44k | >( | 422 | 1.44k | a: &mut [A], | 423 | 1.44k | ) -> &mut [B] { | 424 | 1.44k | unsafe { internal::cast_slice_mut(a) } | 425 | 1.44k | } |
Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> bytemuck::cast_slice_mut::<u8, u8> Line | Count | Source | 418 | 1.91k | pub fn cast_slice_mut< | 419 | 1.91k | A: NoUninit + AnyBitPattern, | 420 | 1.91k | B: NoUninit + AnyBitPattern, | 421 | 1.91k | >( | 422 | 1.91k | a: &mut [A], | 423 | 1.91k | ) -> &mut [B] { | 424 | 1.91k | unsafe { internal::cast_slice_mut(a) } | 425 | 1.91k | } |
Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> bytemuck::cast_slice_mut::<u8, u8> Line | Count | Source | 418 | 1.68k | pub fn cast_slice_mut< | 419 | 1.68k | A: NoUninit + AnyBitPattern, | 420 | 1.68k | B: NoUninit + AnyBitPattern, | 421 | 1.68k | >( | 422 | 1.68k | a: &mut [A], | 423 | 1.68k | ) -> &mut [B] { | 424 | 1.68k | unsafe { internal::cast_slice_mut(a) } | 425 | 1.68k | } |
Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> bytemuck::cast_slice_mut::<f32, u8> Line | Count | Source | 418 | 1.44k | pub fn cast_slice_mut< | 419 | 1.44k | A: NoUninit + AnyBitPattern, | 420 | 1.44k | B: NoUninit + AnyBitPattern, | 421 | 1.44k | >( | 422 | 1.44k | a: &mut [A], | 423 | 1.44k | ) -> &mut [B] { | 424 | 1.44k | unsafe { internal::cast_slice_mut(a) } | 425 | 1.44k | } |
Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> bytemuck::cast_slice_mut::<u8, u8> Line | Count | Source | 418 | 3.99k | pub fn cast_slice_mut< | 419 | 3.99k | A: NoUninit + AnyBitPattern, | 420 | 3.99k | B: NoUninit + AnyBitPattern, | 421 | 3.99k | >( | 422 | 3.99k | a: &mut [A], | 423 | 3.99k | ) -> &mut [B] { | 424 | 3.99k | unsafe { internal::cast_slice_mut(a) } | 425 | 3.99k | } |
bytemuck::cast_slice_mut::<u16, u8> Line | Count | Source | 418 | 37 | pub fn cast_slice_mut< | 419 | 37 | A: NoUninit + AnyBitPattern, | 420 | 37 | B: NoUninit + AnyBitPattern, | 421 | 37 | >( | 422 | 37 | a: &mut [A], | 423 | 37 | ) -> &mut [B] { | 424 | 37 | unsafe { internal::cast_slice_mut(a) } | 425 | 37 | } |
Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> bytemuck::cast_slice_mut::<u8, u8> Line | Count | Source | 418 | 956 | pub fn cast_slice_mut< | 419 | 956 | A: NoUninit + AnyBitPattern, | 420 | 956 | B: NoUninit + AnyBitPattern, | 421 | 956 | >( | 422 | 956 | a: &mut [A], | 423 | 956 | ) -> &mut [B] { | 424 | 956 | unsafe { internal::cast_slice_mut(a) } | 425 | 956 | } |
bytemuck::cast_slice_mut::<u16, u8> Line | Count | Source | 418 | 437 | pub fn cast_slice_mut< | 419 | 437 | A: NoUninit + AnyBitPattern, | 420 | 437 | B: NoUninit + AnyBitPattern, | 421 | 437 | >( | 422 | 437 | a: &mut [A], | 423 | 437 | ) -> &mut [B] { | 424 | 437 | unsafe { internal::cast_slice_mut(a) } | 425 | 437 | } |
Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<f32, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 3]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, [u8; 4]> Unexecuted instantiation: bytemuck::cast_slice_mut::<u8, u8> Unexecuted instantiation: bytemuck::cast_slice_mut::<u16, u8> |
426 | | |
427 | | /// As [`align_to`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to), |
428 | | /// but safe because of the [`Pod`] bound. |
429 | | #[inline] |
430 | 0 | pub fn pod_align_to<T: NoUninit, U: AnyBitPattern>( |
431 | 0 | vals: &[T], |
432 | 0 | ) -> (&[T], &[U], &[T]) { |
433 | 0 | unsafe { vals.align_to::<U>() } |
434 | 0 | } |
435 | | |
436 | | /// As [`align_to_mut`](https://doc.rust-lang.org/std/primitive.slice.html#method.align_to_mut), |
437 | | /// but safe because of the [`Pod`] bound. |
438 | | #[inline] |
439 | 0 | pub fn pod_align_to_mut< |
440 | 0 | T: NoUninit + AnyBitPattern, |
441 | 0 | U: NoUninit + AnyBitPattern, |
442 | 0 | >( |
443 | 0 | vals: &mut [T], |
444 | 0 | ) -> (&mut [T], &mut [U], &mut [T]) { |
445 | 0 | unsafe { vals.align_to_mut::<U>() } |
446 | 0 | } |
447 | | |
448 | | /// Try to cast `A` into `B`. |
449 | | /// |
450 | | /// Note that for this particular type of cast, alignment isn't a factor. The |
451 | | /// input value is semantically copied into the function and then returned to a |
452 | | /// new memory location which will have whatever the required alignment of the |
453 | | /// output type is. |
454 | | /// |
455 | | /// ## Failure |
456 | | /// |
457 | | /// * If the types don't have the same size this fails. |
458 | | #[inline] |
459 | 0 | pub fn try_cast<A: NoUninit, B: AnyBitPattern>( |
460 | 0 | a: A, |
461 | 0 | ) -> Result<B, PodCastError> { |
462 | 0 | unsafe { internal::try_cast(a) } |
463 | 0 | } |
464 | | |
465 | | /// Try to convert a `&A` into `&B`. |
466 | | /// |
467 | | /// ## Failure |
468 | | /// |
469 | | /// * If the reference isn't aligned in the new type |
470 | | /// * If the source type and target type aren't the same size. |
471 | | #[inline] |
472 | 0 | pub fn try_cast_ref<A: NoUninit, B: AnyBitPattern>( |
473 | 0 | a: &A, |
474 | 0 | ) -> Result<&B, PodCastError> { |
475 | 0 | unsafe { internal::try_cast_ref(a) } |
476 | 0 | } |
477 | | |
478 | | /// Try to convert a `&mut A` into `&mut B`. |
479 | | /// |
480 | | /// As [`try_cast_ref`], but `mut`. |
481 | | #[inline] |
482 | 0 | pub fn try_cast_mut< |
483 | 0 | A: NoUninit + AnyBitPattern, |
484 | 0 | B: NoUninit + AnyBitPattern, |
485 | 0 | >( |
486 | 0 | a: &mut A, |
487 | 0 | ) -> Result<&mut B, PodCastError> { |
488 | 0 | unsafe { internal::try_cast_mut(a) } |
489 | 0 | } |
490 | | |
491 | | /// Try to convert `&[A]` into `&[B]` (possibly with a change in length). |
492 | | /// |
493 | | /// * `input.as_ptr() as usize == output.as_ptr() as usize` |
494 | | /// * `input.len() * size_of::<A>() == output.len() * size_of::<B>()` |
495 | | /// |
496 | | /// ## Failure |
497 | | /// |
498 | | /// * If the target type has a greater alignment requirement and the input slice |
499 | | /// isn't aligned. |
500 | | /// * If the target element type is a different size from the current element |
501 | | /// type, and the output slice wouldn't be a whole number of elements when |
502 | | /// accounting for the size change (eg: 3 `u16` values is 1.5 `u32` values, so |
503 | | /// that's a failure). |
504 | | /// * Similarly, you can't convert between a [ZST](https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts) |
505 | | /// and a non-ZST. |
506 | | #[inline] |
507 | 0 | pub fn try_cast_slice<A: NoUninit, B: AnyBitPattern>( |
508 | 0 | a: &[A], |
509 | 0 | ) -> Result<&[B], PodCastError> { |
510 | 0 | unsafe { internal::try_cast_slice(a) } |
511 | 0 | } Unexecuted instantiation: bytemuck::try_cast_slice::<u8, u16> Unexecuted instantiation: bytemuck::try_cast_slice::<_, _> |
512 | | |
513 | | /// Try to convert `&mut [A]` into `&mut [B]` (possibly with a change in |
514 | | /// length). |
515 | | /// |
516 | | /// As [`try_cast_slice`], but `&mut`. |
517 | | #[inline] |
518 | 0 | pub fn try_cast_slice_mut< |
519 | 0 | A: NoUninit + AnyBitPattern, |
520 | 0 | B: NoUninit + AnyBitPattern, |
521 | 0 | >( |
522 | 0 | a: &mut [A], |
523 | 0 | ) -> Result<&mut [B], PodCastError> { |
524 | 0 | unsafe { internal::try_cast_slice_mut(a) } |
525 | 0 | } Unexecuted instantiation: bytemuck::try_cast_slice_mut::<_, _> Unexecuted instantiation: bytemuck::try_cast_slice_mut::<u16, u8> |
526 | | |
527 | | /// Fill all bytes of `target` with zeroes (see [`Zeroable`]). |
528 | | /// |
529 | | /// This is similar to `*target = Zeroable::zeroed()`, but guarantees that any |
530 | | /// padding bytes in `target` are zeroed as well. |
531 | | /// |
532 | | /// See also [`fill_zeroes`], if you have a slice rather than a single value. |
533 | | #[inline] |
534 | 0 | pub fn write_zeroes<T: Zeroable>(target: &mut T) { |
535 | | struct EnsureZeroWrite<T>(*mut T); |
536 | | impl<T> Drop for EnsureZeroWrite<T> { |
537 | | #[inline(always)] |
538 | 0 | fn drop(&mut self) { |
539 | 0 | unsafe { |
540 | 0 | core::ptr::write_bytes(self.0, 0u8, 1); |
541 | 0 | } |
542 | 0 | } |
543 | | } |
544 | 0 | unsafe { |
545 | 0 | let guard = EnsureZeroWrite(target); |
546 | 0 | core::ptr::drop_in_place(guard.0); |
547 | 0 | drop(guard); |
548 | 0 | } |
549 | 0 | } |
550 | | |
551 | | /// Fill all bytes of `slice` with zeroes (see [`Zeroable`]). |
552 | | /// |
553 | | /// This is similar to `slice.fill(Zeroable::zeroed())`, but guarantees that any |
554 | | /// padding bytes in `slice` are zeroed as well. |
555 | | /// |
556 | | /// See also [`write_zeroes`], which zeroes all bytes of a single value rather |
557 | | /// than a slice. |
558 | | #[inline] |
559 | 0 | pub fn fill_zeroes<T: Zeroable>(slice: &mut [T]) { |
560 | 0 | if core::mem::needs_drop::<T>() { |
561 | 0 | // If `T` needs to be dropped then we have to do this one item at a time, in |
562 | 0 | // case one of the intermediate drops does a panic. |
563 | 0 | slice.iter_mut().for_each(write_zeroes); |
564 | 0 | } else { |
565 | | // Otherwise we can be really fast and just fill everthing with zeros. |
566 | 0 | let len = slice.len(); |
567 | 0 | unsafe { core::ptr::write_bytes(slice.as_mut_ptr(), 0u8, len) } |
568 | | } |
569 | 0 | } |
570 | | |
571 | | /// Same as [`Zeroable::zeroed`], but as a `const fn` const. |
572 | | #[cfg(feature = "const_zeroed")] |
573 | | #[inline] |
574 | | #[must_use] |
575 | | pub const fn zeroed<T: Zeroable>() -> T { |
576 | | unsafe { core::mem::zeroed() } |
577 | | } |