bytemuck/lib.rs
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"))]
100use core::arch::aarch64;
101#[cfg(all(target_arch = "wasm32", feature = "wasm_simd"))]
102use core::arch::wasm32;
103#[cfg(target_arch = "x86")]
104use core::arch::x86;
105#[cfg(target_arch = "x86_64")]
106use core::arch::x86_64;
107//
108use 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)]
119pub use ::core as __core;
120
121#[cfg(not(feature = "min_const_generics"))]
122macro_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.
130macro_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)]
154macro_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)]
168macro_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")]
185extern crate std;
186
187#[cfg(feature = "extern_crate_alloc")]
188extern crate alloc;
189#[cfg(feature = "extern_crate_alloc")]
190#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_alloc")))]
191pub mod allocation;
192#[cfg(feature = "extern_crate_alloc")]
193pub use allocation::*;
194
195mod anybitpattern;
196pub use anybitpattern::*;
197
198pub mod checked;
199pub use checked::CheckedBitPattern;
200
201mod internal;
202
203mod zeroable;
204pub use zeroable::*;
205mod zeroable_in_option;
206pub use zeroable_in_option::*;
207
208mod pod;
209pub use pod::*;
210mod pod_in_option;
211pub use pod_in_option::*;
212
213#[cfg(feature = "must_cast")]
214mod must;
215#[cfg(feature = "must_cast")]
216#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "must_cast")))]
217pub use must::*;
218
219mod no_uninit;
220pub use no_uninit::*;
221
222mod contiguous;
223pub use contiguous::*;
224
225mod 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
229mod transparent;
230pub use transparent::*;
231
232#[cfg(feature = "derive")]
233#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "derive")))]
234pub 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)]
241pub 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"))]
261impl core::fmt::Display for PodCastError {
262 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
263 write!(f, "{:?}", self)
264 }
265}
266#[cfg(feature = "extern_crate_std")]
267#[cfg_attr(feature = "nightly_docs", doc(cfg(feature = "extern_crate_std")))]
268impl std::error::Error for PodCastError {}
269
270// Rust 1.81+
271#[cfg(all(feature = "impl_core_error", not(feature = "extern_crate_std")))]
272impl 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]
279pub fn bytes_of<T: NoUninit>(t: &T) -> &[u8] {
280 unsafe { internal::bytes_of(t) }
281}
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]
288pub fn bytes_of_mut<T: NoUninit + AnyBitPattern>(t: &mut T) -> &mut [u8] {
289 unsafe { internal::bytes_of_mut(t) }
290}
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)]
299pub fn from_bytes<T: AnyBitPattern>(s: &[u8]) -> &T {
300 unsafe { internal::from_bytes(s) }
301}
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)]
310pub fn from_bytes_mut<T: NoUninit + AnyBitPattern>(s: &mut [u8]) -> &mut T {
311 unsafe { internal::from_bytes_mut(s) }
312}
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]
322pub fn try_pod_read_unaligned<T: AnyBitPattern>(
323 bytes: &[u8],
324) -> Result<T, PodCastError> {
325 unsafe { internal::try_pod_read_unaligned(bytes) }
326}
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)]
337pub fn pod_read_unaligned<T: AnyBitPattern>(bytes: &[u8]) -> T {
338 unsafe { internal::pod_read_unaligned(bytes) }
339}
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]
348pub fn try_from_bytes<T: AnyBitPattern>(s: &[u8]) -> Result<&T, PodCastError> {
349 unsafe { internal::try_from_bytes(s) }
350}
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]
359pub fn try_from_bytes_mut<T: NoUninit + AnyBitPattern>(
360 s: &mut [u8],
361) -> Result<&mut T, PodCastError> {
362 unsafe { internal::try_from_bytes_mut(s) }
363}
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)]
372pub fn cast<A: NoUninit, B: AnyBitPattern>(a: A) -> B {
373 unsafe { internal::cast(a) }
374}
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)]
383pub fn cast_mut<A: NoUninit + AnyBitPattern, B: NoUninit + AnyBitPattern>(
384 a: &mut A,
385) -> &mut B {
386 unsafe { internal::cast_mut(a) }
387}
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)]
396pub fn cast_ref<A: NoUninit, B: AnyBitPattern>(a: &A) -> &B {
397 unsafe { internal::cast_ref(a) }
398}
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)]
407pub fn cast_slice<A: NoUninit, B: AnyBitPattern>(a: &[A]) -> &[B] {
408 unsafe { internal::cast_slice(a) }
409}
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)]
418pub fn cast_slice_mut<
419 A: NoUninit + AnyBitPattern,
420 B: NoUninit + AnyBitPattern,
421>(
422 a: &mut [A],
423) -> &mut [B] {
424 unsafe { internal::cast_slice_mut(a) }
425}
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]
430pub fn pod_align_to<T: NoUninit, U: AnyBitPattern>(
431 vals: &[T],
432) -> (&[T], &[U], &[T]) {
433 unsafe { vals.align_to::<U>() }
434}
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]
439pub fn pod_align_to_mut<
440 T: NoUninit + AnyBitPattern,
441 U: NoUninit + AnyBitPattern,
442>(
443 vals: &mut [T],
444) -> (&mut [T], &mut [U], &mut [T]) {
445 unsafe { vals.align_to_mut::<U>() }
446}
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]
459pub fn try_cast<A: NoUninit, B: AnyBitPattern>(
460 a: A,
461) -> Result<B, PodCastError> {
462 unsafe { internal::try_cast(a) }
463}
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]
472pub fn try_cast_ref<A: NoUninit, B: AnyBitPattern>(
473 a: &A,
474) -> Result<&B, PodCastError> {
475 unsafe { internal::try_cast_ref(a) }
476}
477
478/// Try to convert a `&mut A` into `&mut B`.
479///
480/// As [`try_cast_ref`], but `mut`.
481#[inline]
482pub fn try_cast_mut<
483 A: NoUninit + AnyBitPattern,
484 B: NoUninit + AnyBitPattern,
485>(
486 a: &mut A,
487) -> Result<&mut B, PodCastError> {
488 unsafe { internal::try_cast_mut(a) }
489}
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]
507pub fn try_cast_slice<A: NoUninit, B: AnyBitPattern>(
508 a: &[A],
509) -> Result<&[B], PodCastError> {
510 unsafe { internal::try_cast_slice(a) }
511}
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]
518pub fn try_cast_slice_mut<
519 A: NoUninit + AnyBitPattern,
520 B: NoUninit + AnyBitPattern,
521>(
522 a: &mut [A],
523) -> Result<&mut [B], PodCastError> {
524 unsafe { internal::try_cast_slice_mut(a) }
525}
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]
534pub 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 fn drop(&mut self) {
539 unsafe {
540 core::ptr::write_bytes(self.0, 0u8, 1);
541 }
542 }
543 }
544 unsafe {
545 let guard = EnsureZeroWrite(target);
546 core::ptr::drop_in_place(guard.0);
547 drop(guard);
548 }
549}
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]
559pub fn fill_zeroes<T: Zeroable>(slice: &mut [T]) {
560 if core::mem::needs_drop::<T>() {
561 // If `T` needs to be dropped then we have to do this one item at a time, in
562 // case one of the intermediate drops does a panic.
563 slice.iter_mut().for_each(write_zeroes);
564 } else {
565 // Otherwise we can be really fast and just fill everthing with zeros.
566 let len = slice.len();
567 unsafe { core::ptr::write_bytes(slice.as_mut_ptr(), 0u8, len) }
568 }
569}
570
571/// Same as [`Zeroable::zeroed`], but as a `const fn` const.
572#[cfg(feature = "const_zeroed")]
573#[inline]
574#[must_use]
575pub const fn zeroed<T: Zeroable>() -> T {
576 unsafe { core::mem::zeroed() }
577}