Trait plain::Plain

source ·
pub unsafe trait Plain {
    // Provided methods
    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 copy_from_bytes(&mut self, bytes: &[u8]) -> Result<(), Error> { ... }
}
Expand description

A trait for plain data types that can be safely read from a byte slice.

A type can be Plain if it is #repr(C) and only contains data with no possible invalid values. Types that can’t be Plain include, but are not limited to, bool, char, enums, tuples, pointers and references.

At this moment, Drop types are also not legal, because compiler adds a special “drop flag” into the type. This is slated to change in the future.

On the other hand, arrays of a Plain type, and structures where all members are plain (and not Drop), are okay.

Structures that are not #repr(C), while not necessarily illegal in principle, are largely useless because they don’t have a stable layout. For example, the compiler is allowed to reorder fields arbitrarily.

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

Provided Methods§

source

fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>where Self: Sized,

source

fn slice_from_bytes(bytes: &[u8]) -> Result<&[Self], Error>where Self: Sized,

source

fn slice_from_bytes_len(bytes: &[u8], len: usize) -> Result<&[Self], Error>where Self: Sized,

source

fn from_mut_bytes(bytes: &mut [u8]) -> Result<&mut Self, Error>where Self: Sized,

source

fn slice_from_mut_bytes(bytes: &mut [u8]) -> Result<&mut [Self], Error>where Self: Sized,

source

fn slice_from_mut_bytes_len( bytes: &mut [u8], len: usize ) -> Result<&mut [Self], Error>where Self: Sized,

source

fn copy_from_bytes(&mut self, bytes: &[u8]) -> Result<(), Error>

Implementations on Foreign Types§

source§

impl Plain for i8

source§

impl Plain for i16

source§

impl Plain for i32

source§

impl Plain for i64

source§

impl Plain for isize

source§

impl Plain for u8

source§

impl Plain for u16

source§

impl Plain for u32

source§

impl Plain for u64

source§

impl Plain for usize

source§

impl<S> Plain for [S]where S: Plain,

Implementors§