Function plain::from_bytes [] [src]

pub fn from_bytes<T>(bytes: &[u8]) -> Result<&T, Error> where
    T: Plain

Safely converts a byte slice to a reference.

However, if the byte slice is not long enough to contain target type, or if it doesn't satisfy the type's alignment requirements, the function returns an error.

The function will not fail when the byte slice is longer than necessary, since it is a common practice to interpret the beginning of a slice as a fixed-size header.

In many cases it is preferrable to allocate a value/slice of the target type and use copy_from_bytes() to copy data instead. That way, any issues with alignment are implicitly avoided.