Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

Crate bytemuck

Cratebytemuck 

Source
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:

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 foralloc related types such asBox and Vec.
  • zeroable_maybe_uninit andzeroable_atomics: Provide moreZeroableimpls.
  • pod_saturating: Provide morePod andZeroable impls.
  • wasm_simd andaarch64_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 thezeroed function.

§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§

allocationextern_crate_alloc
Stuff to boost things in thealloc crate.
checked
Checked versions of the casting functions exposed in crate rootthat supportCheckedBitPattern types.

Macros§

offset_of
Find the offset in bytes of the given$field of$Type. Requires analready initialized$instance value to work with.

Enums§

PodCastError
The things that can go wrong when casting betweenPod data forms.

Traits§

AnyBitPattern
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”.
PodInOption
Trait for types which arePod when wrapped inOption.
TransparentWrapper
A trait which indicates that a type is a#[repr(transparent)] wrapperaround theInner value.
Zeroable
Trait for types that can be safely created withzeroed.
ZeroableInOption
Trait for types which areZeroable when wrapped inOption.

Functions§

bytes_of
Re-interprets&T as&[u8].
bytes_of_mut
Re-interprets&mut T as&mut [u8].
cast
CastA intoB
cast_mut
Cast&mut A into&mut B.
cast_ref
Cast&A into&B.
cast_slice
Cast&[A] into&[B].
cast_slice_mut
Cast&mut [A] into&mut [B].
fill_zeroes
Fill all bytes ofslice with zeroes (seeZeroable).
from_bytes
Re-interprets&[u8] as&T.
from_bytes_mut
Re-interprets&mut [u8] as&mut T.
must_castmust_cast
CastA intoB if infalliable, or fail to compile.
must_cast_mutmust_cast
Convert a&mut A into&mut B if infalliable, or fail to compile.
must_cast_refmust_cast
Convert&A into&B if infalliable, or fail to compile.
must_cast_slicemust_cast
Convert&[A] into&[B] (possibly with a change in length) ifinfalliable, or fail to compile.
must_cast_slice_mutmust_cast
Convert&mut [A] into&mut [B] (possibly with a change in length) ifinfalliable, or fail to compile.
pod_align_to
Asalign_to,but safe because of thePod bound.
pod_align_to_mut
Asalign_to_mut,but safe because of thePod bound.
pod_read_unaligned
Reads the slice into aT value.
try_cast
Try to castA intoB.
try_cast_mut
Try to convert a&mut A into&mut B.
try_cast_ref
Try to convert a&A into&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 aT.
write_zeroes
Fill all bytes oftarget with zeroes (seeZeroable).
zeroedconst_zeroed
Same asZeroable::zeroed, but as aconst fn const.

Derive Macros§

AnyBitPatternderive
Derive theAnyBitPattern trait for a struct
ByteEqderive
Derive thePartialEq andEq trait for a type
ByteHashderive
Derive theHash trait for a type
CheckedBitPatternderive
Derive theCheckedBitPattern trait for a struct or enum.
Contiguousderive
Derive theContiguous trait for an enum
NoUninitderive
Derive theNoUninit trait for a struct or enum
Podderive
Derive thePod trait for a struct
TransparentWrapperderive
Derive theTransparentWrapper trait for a struct
Zeroablederive
Derive theZeroable trait for a type.

[8]ページ先頭

©2009-2025 Movatter.jp