Expand description
This crate gives small utilities for casting between plain data types.
§Basics
Data comes in five basic forms in Rust, so we have five basic castingfunctions:
Tusescast&Tusescast_ref&mut Tusescast_mut&[T]usescast_slice&mut [T]usescast_slice_mut
Depending on the function, theNoUninit and/orAnyBitPattern traitsare used to maintain memory safety.
Historical Note: When the crate first started thePod trait was usedinstead, and so you may hear people refer to that, but it has the strongestrequirements and people eventually wanted the more fine-grained system, sohere we are. All types that implPod have a blanket impl to also supportNoUninit andAnyBitPattern. The traits unfortunately do not have aperfectly clean hierarchy for semver reasons.
§Failures
Some casts will never fail, and other casts might fail.
cast::<u32, f32>always works (andf32::from_bits).cast_ref::<[u8; 4], u32>might fail if the specific array referencegiven at runtime doesn’t have alignment 4.
In addition to the “normal” forms of each function, which will panic oninvalid input, there’s alsotry_ versions which will return aResult.
If you would like to statically ensure that a cast will work at runtime youcan use themust_cast crate feature and themust_ casting functions. A“must cast” that can’t be statically known to be valid will cause acompilation error (and sometimes a very hard to read compilation error).
§Using Your Own Types
All the functions listed above are guarded by thePod trait, which is asub-trait of theZeroable trait.
If you enable the crate’sderive feature then these traits can be derivedon your own types. The derive macros will perform the necessary checks onyour type declaration, and trigger an error if your type does not qualify.
The derive macros might not cover all edge cases, and sometimes they willerror when actually everything is fine. As a last resort you can impl thesetraits manually. However, these traits areunsafe, and you shouldcarefully read the requirements before using a manual implementation.
§Cargo Features
The crate supports Rust 1.34 when no features are enabled, and so there’scargo features for thing that you might consider “obvious”.
The cargo featuresdo not promise any particular MSRV, and they mayincrease their MSRV in new versions.
derive: Provide derive macros for the various traits.extern_crate_alloc: Provide utilities forallocrelated types such asBox and Vec.zeroable_maybe_uninitandzeroable_atomics: Provide moreZeroableimpls.pod_saturating: Provide morePodandZeroableimpls.wasm_simdandaarch64_simd: Support more SIMD types.min_const_generics: Provides appropriate impls for arrays of all lengthsinstead of just for a select list of array lengths.must_cast: Provides themust_functions, which will compile error ifthe requested cast can’t be statically verified.const_zeroed: Provides a const version of thezeroedfunction.
§Related Crates
pack1, which containsbytemuck-compatiblepacked little-endian, big-endian and native-endian integer and floatingpoint number types.
Re-exports§
pub use checked::CheckedBitPattern;pub useallocation::*;extern_crate_alloc
Modules§
- allocation
extern_crate_alloc - Stuff to boost things in the
alloccrate. - checked
- Checked versions of the casting functions exposed in crate rootthat support
CheckedBitPatterntypes.
Macros§
- offset_
of - Find the offset in bytes of the given
$fieldof$Type. Requires analready initialized$instancevalue to work with.
Enums§
- PodCast
Error - The things that can go wrong when casting between
Poddata forms.
Traits§
- AnyBit
Pattern - Marker trait for “plain old data” types that are valid for any bit pattern.
- Contiguous
- A trait indicating that:
- NoUninit
- Marker trait for “plain old data” types with no uninit (or padding) bytes.
- Pod
- Marker trait for “plain old data”.
- PodIn
Option - Trait for types which arePod when wrapped inOption.
- Transparent
Wrapper - A trait which indicates that a type is a
#[repr(transparent)]wrapperaround theInnervalue. - Zeroable
- Trait for types that can be safely created with
zeroed. - Zeroable
InOption - Trait for types which areZeroable when wrapped inOption.
Functions§
- bytes_
of - Re-interprets
&Tas&[u8]. - bytes_
of_ mut - Re-interprets
&mut Tas&mut [u8]. - cast
- Cast
AintoB - cast_
mut - Cast
&mut Ainto&mut B. - cast_
ref - Cast
&Ainto&B. - cast_
slice - Cast
&[A]into&[B]. - cast_
slice_ mut - Cast
&mut [A]into&mut [B]. - fill_
zeroes - Fill all bytes of
slicewith zeroes (seeZeroable). - from_
bytes - Re-interprets
&[u8]as&T. - from_
bytes_ mut - Re-interprets
&mut [u8]as&mut T. - must_
cast must_cast - Cast
AintoBif infalliable, or fail to compile. - must_
cast_ mut must_cast - Convert a
&mut Ainto&mut Bif infalliable, or fail to compile. - must_
cast_ ref must_cast - Convert
&Ainto&Bif infalliable, or fail to compile. - must_
cast_ slice must_cast - Convert
&[A]into&[B](possibly with a change in length) ifinfalliable, or fail to compile. - must_
cast_ slice_ mut must_cast - Convert
&mut [A]into&mut [B](possibly with a change in length) ifinfalliable, or fail to compile. - pod_
align_ to - As
align_to,but safe because of thePodbound. - pod_
align_ to_ mut - As
align_to_mut,but safe because of thePodbound. - pod_
read_ unaligned - Reads the slice into a
Tvalue. - try_
cast - Try to cast
AintoB. - try_
cast_ mut - Try to convert a
&mut Ainto&mut B. - try_
cast_ ref - Try to convert a
&Ainto&B. - try_
cast_ slice - Try to convert
&[A]into&[B](possibly with a change in length). - try_
cast_ slice_ mut - Try to convert
&mut [A]into&mut [B](possibly with a change inlength). - try_
from_ bytes - Re-interprets
&[u8]as&T. - try_
from_ bytes_ mut - Re-interprets
&mut [u8]as&mut T. - try_
pod_ read_ unaligned - Reads from the bytes as if they were a
T. - write_
zeroes - Fill all bytes of
targetwith zeroes (seeZeroable). - zeroed
const_zeroed - Same as
Zeroable::zeroed, but as aconst fnconst.
Derive Macros§
- AnyBit
Pattern derive - Derive the
AnyBitPatterntrait for a struct - ByteEq
derive - Derive the
PartialEqandEqtrait for a type - Byte
Hash derive - Derive the
Hashtrait for a type - Checked
BitPattern derive - Derive the
CheckedBitPatterntrait for a struct or enum. - Contiguous
derive - Derive the
Contiguoustrait for an enum - NoUninit
derive - Derive the
NoUninittrait for a struct or enum - Pod
derive - Derive the
Podtrait for a struct - Transparent
Wrapper derive - Derive the
TransparentWrappertrait for a struct - Zeroable
derive - Derive the
Zeroabletrait for a type.