Trait ByteValued

Source
pub unsafe trait ByteValued:
    Copy
    + Send
    + Sync {
    // Provided methods
    fn from_slice(data: &[u8]) -> Option<&Self> { ... }
    fn from_mut_slice(data: &mut [u8]) -> Option<&mut Self> { ... }
    fn from_reader<R: Read>(read: R) -> Result<Self> { ... }
    fn as_slice(&self) -> &[u8]  { ... }
    fn as_mut_slice(&mut self) -> &mut [u8]  { ... }
}
Expand description

Types for which it is safe to initialize from raw data.

Implementing this trait guarantees that it is safe to instantiate the struct with random data.

§Safety

A type T is ByteValued if it can be initialized by reading its contents from a byte array. This is generally true for all plain-old-data structs. It is notably not true for any type that includes a reference.

It is unsafe for T to be ByteValued if T contains implicit padding. (LLVM considers access to implicit padding to be undefined behavior, which can cause UB when working with T. For details on structure padding in Rust, see https://doc.rust-lang.org/reference/type-layout.html#the-c-representation.

Provided Methods§

Source

fn from_slice(data: &[u8]) -> Option<&Self>

Converts a slice of raw data into a reference of Self.

The value of data is not copied. Instead a reference is made from the given slice. The value of Self will depend on the representation of the type in memory, and may change in an unstable fashion.

This will return None if the length of data does not match the size of Self, or if the data is not aligned for the type of Self.

Source

fn from_mut_slice(data: &mut [u8]) -> Option<&mut Self>

Converts a mutable slice of raw data into a mutable reference of Self.

Because Self is made from a reference to the mutable slice, mutations to the returned reference are immediately reflected in data. The value of the returned Self` will depend on the representation of the type in memory, and may change in an unstable fashion.

This will return None if the length of data does not match the size of Self, or if the data is not aligned for the type of Self.

Source

fn from_reader<R: Read>(read: R) -> Result<Self>

Creates an instance of Self by copying raw data from an io::Read stream.

Source

fn as_slice(&self) -> &[u8]

Converts a reference to self into a slice of bytes.

The value of self is not copied. Instead, the slice is made from a reference to self. The value of bytes in the returned slice will depend on the representation of the type in memory, and may change in an unstable fashion.

Source

fn as_mut_slice(&mut self) -> &mut [u8]

Converts a mutable reference to self into a mutable slice of bytes.

Because the slice is made from a reference to self, mutations to the returned slice are immediately reflected in self. The value of bytes in the returned slice will depend on the representation of the type in memory, and may change in an unstable fashion.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§