Well that depends.
I think that you can always implement this safely for types without padding bytes (e.g. repr(packed)
, or maybe even repr(C)
?), and also for all types if the implementation does not touch the padding bytes in which case the size of the [u8]
might be smaller than the size of T
.
If you are talking about implementing FromBits<T> for [u8]
by memcpy
ing all the bytes for types with Rust layout including padding bytes then, as @hanna-kruppe says, that will depend on whether reading a padding byte is a read from uninitialized memory or not (cc @ubsan @RalfJung).
In any case, given that the layout of types with Rust layout is unspecified (e.g. the compiler can reorder fields at will), you will run into issues when serializing/deserializing with different Rust versions on top of the endianness issues that you get when serializing/deserializing on different architectures.
At that point you might as well restrict that blanket impl to repr(C)
types and call it a day.