Trait plain::Plain [] [src]

pub unsafe trait Plain {
    fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>
    where
        Self: Sized
, { ... } fn slice_from_bytes(bytes: &[u8]) -> Result<&[Self], Error>
    where
        Self: Sized
, { ... } fn slice_from_bytes_len(bytes: &[u8], len: usize) -> Result<&[Self], Error>
    where
        Self: Sized
, { ... } fn from_mut_bytes(bytes: &mut [u8]) -> Result<&mut Self, Error>
    where
        Self: Sized
, { ... } fn slice_from_mut_bytes(bytes: &mut [u8]) -> Result<&mut [Self], Error>
    where
        Self: Sized
, { ... } fn slice_from_mut_bytes_len(
        bytes: &mut [u8],
        len: usize
    ) -> Result<&mut [Self], Error>
    where
        Self: Sized
, { ... } fn as_bytes(&self) -> &[u8] { ... } fn as_mut_bytes(&mut self) -> &mut [u8] { ... } }

A trait for plain reinterpretable data.

A type can be Plain if it is #repr(C) and only contains data with no possible invalid values. Specifically, bool, char, enums, tuples, pointers and references are not Plain. On the other hand, arrays of a Plain type, and structures where all members are plain, are usually okay.

On top of this, implicit padding may technically be uninitialized bytes, therefore reading them might constitute undefined behavior by definition. As such, you currently must not apply Plain to structures with implicit padding. I.e. the size of the whole struct (as returned by mem::size_of) must be equal to the sum of sizes of its fields. The easiest way to assure this is learning padding rules for #repr(C) and explicitly provide padding as dummy fields where appropriate.

All methods of this trait are implemented automatically as wrappers for crate-level funtions.

Provided Methods

Implementors