pub unsafe trait TryFromBytes {
    // Provided methods
    fn try_from_ref(bytes: &[u8]) -> Option<&Self>
       where Self: KnownLayout + NoCell { ... }
    fn try_from_mut(bytes: &mut [u8]) -> Option<&mut Self>
       where Self: KnownLayout + NoCell { ... }
    fn try_read_from(bytes: &[u8]) -> Option<Self>
       where Self: Sized { ... }
}
Expand description

Types for which some bit patterns are valid.

A memory region of the appropriate length which contains initialized bytes can be viewed as a TryFromBytes type so long as the runtime value of those bytes corresponds to a valid instance of that type. For example, bool is TryFromBytes; zerocopy can transmute a [u8] into a bool so long as it first checks that the value of the [u8] is [0] or [1].

§Implementation

Do not implement this trait yourself! Instead, use #[derive(TryFromBytes)] (requires the derive Cargo feature); e.g.:

#[derive(TryFromBytes)]
struct MyStruct {
    ...
}

#[derive(TryFromBytes)]
#[repr(u8)]
enum MyEnum {
    ...
}

#[derive(TryFromBytes, NoCell)]
union MyUnion {
    ...
}

This derive ensures that the runtime check of whether bytes correspond to a valid instance is sound. You must implement this trait via the derive.

§What is a “valid instance”?

In Rust, each type has bit validity, which refers to the set of bit patterns which may appear in an instance of that type. It is impossible for safe Rust code to produce values which violate bit validity (ie, values outside of the “valid” set of bit patterns). If unsafe code produces an invalid value, this is considered undefined behavior.

Rust’s bit validity rules are currently being decided, which means that some types have three classes of bit patterns: those which are definitely valid, and whose validity is documented in the language; those which may or may not be considered valid at some point in the future; and those which are definitely invalid.

Zerocopy takes a conservative approach, and only considers a bit pattern to be valid if its validity is a documenteed guarantee provided by the language.

For most use cases, Rust’s current guarantees align with programmers’ intuitions about what ought to be valid. As a result, zerocopy’s conservatism should not affect most users. One notable exception is unions, whose bit validity is very up in the air; zerocopy does not permit implementing TryFromBytes for any union type.

If you are negatively affected by lack of support for a particular type, we encourage you to let us know by filing an issue.

§TryFromBytes is not symmetrical with IntoBytes

There are some types which implement both TryFromBytes and IntoBytes, but for which TryFromBytes is not guaranteed to accept all byte sequences produced by IntoBytes. In other words, for some T: TryFromBytes + IntoBytes, there exist values of t: T such that TryFromBytes::try_from_ref(t.as_bytes()) == None. Code should not generally assume that values produced by IntoBytes will necessarily be accepted as valid by TryFromBytes.

§Safety

On its own, T: TryFromBytes does not make any guarantees about the layout or representation of T. It merely provides the ability to perform a validity check at runtime via methods like try_from_ref.

You must not rely on the #[doc(hidden)] internals of TryFromBytes. Future releases of zerocopy may make backwards-breaking changes to these items, including changes that only affect soundness, which may cause code which uses those items to silently become unsound.

Provided Methods§

source

fn try_from_ref(bytes: &[u8]) -> Option<&Self>
where Self: KnownLayout + NoCell,

Attempts to interpret a byte slice as a Self.

try_from_ref validates that bytes contains a valid Self, and that it satisfies Self’s alignment requirement. If it does, then bytes is reinterpreted as a Self.

Note that Rust’s bit validity rules are still being decided. As such, there exist types whose bit validity is ambiguous. See here for a discussion of how these cases are handled.

source

fn try_from_mut(bytes: &mut [u8]) -> Option<&mut Self>
where Self: KnownLayout + NoCell,

Attempts to interpret a mutable byte slice as a Self.

try_from_mut validates that bytes contains a valid Self, and that it satisfies Self’s alignment requirement. If it does, then bytes is reinterpreted as a Self.

Note that Rust’s bit validity rules are still being decided. As such, there exist types whose bit validity is ambiguous. See here for a discussion of how these cases are handled.

source

fn try_read_from(bytes: &[u8]) -> Option<Self>
where Self: Sized,

Attempts to read a Self from a byte slice.

try_read_from validates that bytes contains a valid Self. If it does, those bytes are read out of bytes and reinterpreted as a Self.

Note that Rust’s bit validity rules are still being decided. As such, there exist types whose bit validity is ambiguous. See here for a discussion of how these cases are handled.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl TryFromBytes for Option<NonZeroI8>

source§

impl TryFromBytes for Option<NonZeroI16>

source§

impl TryFromBytes for Option<NonZeroI32>

source§

impl TryFromBytes for Option<NonZeroI64>

source§

impl TryFromBytes for Option<NonZeroI128>

source§

impl TryFromBytes for Option<NonZeroIsize>

source§

impl TryFromBytes for Option<NonZeroU8>

source§

impl TryFromBytes for Option<NonZeroU16>

source§

impl TryFromBytes for Option<NonZeroU32>

source§

impl TryFromBytes for Option<NonZeroU64>

source§

impl TryFromBytes for Option<NonZeroU128>

source§

impl TryFromBytes for Option<NonZeroUsize>

source§

impl TryFromBytes for bool

source§

impl TryFromBytes for char

source§

impl TryFromBytes for f32

source§

impl TryFromBytes for f64

source§

impl TryFromBytes for i8

source§

impl TryFromBytes for i16

source§

impl TryFromBytes for i32

source§

impl TryFromBytes for i64

source§

impl TryFromBytes for i128

source§

impl TryFromBytes for isize

source§

impl TryFromBytes for str

source§

impl TryFromBytes for u8

source§

impl TryFromBytes for u16

source§

impl TryFromBytes for u32

source§

impl TryFromBytes for u64

source§

impl TryFromBytes for u128

source§

impl TryFromBytes for ()

source§

impl TryFromBytes for usize

source§

impl TryFromBytes for __m128

Available on x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m128d

Available on x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m128i

Available on x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m256

Available on x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m256d

Available on x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m256i

Available on x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m512

Available on crate feature simd-nightly and x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m512bh

Available on crate feature simd-nightly and x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m512d

Available on crate feature simd-nightly and x86-64 and crate feature simd only.
source§

impl TryFromBytes for __m512i

Available on crate feature simd-nightly and x86-64 and crate feature simd only.
source§

impl TryFromBytes for NonZeroI8

source§

impl TryFromBytes for NonZeroI16

source§

impl TryFromBytes for NonZeroI32

source§

impl TryFromBytes for NonZeroI64

source§

impl TryFromBytes for NonZeroI128

source§

impl TryFromBytes for NonZeroIsize

source§

impl TryFromBytes for NonZeroU8

source§

impl TryFromBytes for NonZeroU16

source§

impl TryFromBytes for NonZeroU32

source§

impl TryFromBytes for NonZeroU64

source§

impl TryFromBytes for NonZeroU128

source§

impl TryFromBytes for NonZeroUsize

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<A, B, C, D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: A, _: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<B, C, D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<fn(_: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<B, C, D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: B, _: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<C, D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<fn(_: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<C, D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: C, _: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<D, E, F, G, H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: D, _: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<E, F, G, H, I, J, K, L, M> TryFromBytes for Option<fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<E, F, G, H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: E, _: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<F, G, H, I, J, K, L, M> TryFromBytes for Option<fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<F, G, H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: F, _: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<G, H, I, J, K, L, M> TryFromBytes for Option<fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<G, H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: G, _: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<H, I, J, K, L, M> TryFromBytes for Option<fn(_: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<H, I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: H, _: I, _: J, _: K, _: L) -> M>

source§

impl<I, J, K, L, M> TryFromBytes for Option<fn(_: I, _: J, _: K, _: L) -> M>

source§

impl<I, J, K, L, M> TryFromBytes for Option<extern "C" fn(_: I, _: J, _: K, _: L) -> M>

source§

impl<J, K, L, M> TryFromBytes for Option<fn(_: J, _: K, _: L) -> M>

source§

impl<J, K, L, M> TryFromBytes for Option<extern "C" fn(_: J, _: K, _: L) -> M>

source§

impl<K, L, M> TryFromBytes for Option<fn(_: K, _: L) -> M>

source§

impl<K, L, M> TryFromBytes for Option<extern "C" fn(_: K, _: L) -> M>

source§

impl<L, M> TryFromBytes for Option<fn(_: L) -> M>

source§

impl<L, M> TryFromBytes for Option<extern "C" fn(_: L) -> M>

source§

impl<M> TryFromBytes for Option<fn() -> M>

source§

impl<M> TryFromBytes for Option<extern "C" fn() -> M>

source§

impl<T> TryFromBytes for Option<&T>

source§

impl<T> TryFromBytes for Option<&mut T>

source§

impl<T> TryFromBytes for Option<Box<T>>

Available on crate feature alloc only.
source§

impl<T> TryFromBytes for Option<NonNull<T>>

source§

impl<T> TryFromBytes for *const T

source§

impl<T> TryFromBytes for *mut T

source§

impl<T> TryFromBytes for MaybeUninit<T>

source§

impl<T: TryFromBytes> TryFromBytes for [T]

source§

impl<T: TryFromBytes> TryFromBytes for UnsafeCell<T>

source§

impl<T: TryFromBytes> TryFromBytes for Wrapping<T>

source§

impl<T: TryFromBytes, const N: usize> TryFromBytes for [T; N]

source§

impl<T: ?Sized + TryFromBytes> TryFromBytes for ManuallyDrop<T>

source§

impl<T: ?Sized> TryFromBytes for PhantomData<T>

Implementors§