Trait data_model::DataInit

source ·
pub unsafe trait DataInit: Copy + Send + Sync {
    fn from_slice(data: &[u8]) -> Option<&Self> { ... }
    fn read_from_prefix(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]  { ... }
}
👎Deprecated: This type was created when there’s no suitable POD (plain-old-data) types in the rust ecosystem. It does not verify the safety of treating a data structure as POD and multiple incorrect and unsound usage have occured previously. Users should use zerocopy crate as alternative.
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 DataInit 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 DataInit 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§

👎Deprecated: This type was created when there’s no suitable POD (plain-old-data) types in the rust ecosystem. It does not verify the safety of treating a data structure as POD and multiple incorrect and unsound usage have occured previously. Users should use zerocopy crate as alternative.

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.

👎Deprecated: This type was created when there’s no suitable POD (plain-old-data) types in the rust ecosystem. It does not verify the safety of treating a data structure as POD and multiple incorrect and unsound usage have occured previously. Users should use zerocopy crate as alternative.

Copies the value of Self from the beginning of a slice of raw data.

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

👎Deprecated: This type was created when there’s no suitable POD (plain-old-data) types in the rust ecosystem. It does not verify the safety of treating a data structure as POD and multiple incorrect and unsound usage have occured previously. Users should use zerocopy crate as alternative.

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.

👎Deprecated: This type was created when there’s no suitable POD (plain-old-data) types in the rust ecosystem. It does not verify the safety of treating a data structure as POD and multiple incorrect and unsound usage have occured previously. Users should use zerocopy crate as alternative.

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

👎Deprecated: This type was created when there’s no suitable POD (plain-old-data) types in the rust ecosystem. It does not verify the safety of treating a data structure as POD and multiple incorrect and unsound usage have occured previously. Users should use zerocopy crate as alternative.

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.

👎Deprecated: This type was created when there’s no suitable POD (plain-old-data) types in the rust ecosystem. It does not verify the safety of treating a data structure as POD and multiple incorrect and unsound usage have occured previously. Users should use zerocopy crate as alternative.

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.

Implementations on Foreign Types§

Implementors§