Function plain::slice_from_bytes [−][src]
pub fn slice_from_bytes<T>(bytes: &[u8]) -> Result<&[T], Error> where
T: Plain, Similar to from_bytes(),
except that the output is a slice of T, instead
of a reference to a single T. All concerns about
alignment also apply here, but size is handled
differently.
The result slice’s length is set to be
bytes.len() / size_of::<T>(), and there
are no requirements for input size. I.e.
the result may be empty slice, and the input
slice doesn’t necessarily have to end on T’s
boundary. The latter has pragmatic reasons: If the
length of the array is not known in advance,
e.g. if it’s terminated by a special element,
it’s perfectly legal to turn the whole rest
of data into &[T] and set the proper length
after inspecting the array.
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.