Is this a good method to get raw bytes of a POD?

I need do some low-level operations to some plain old data structure (actuall, I need to send some data through NIC).

So I figured out following method to convert a POD to byte slice:

pub fn to_u8_slice<'a, T>(src:&'a T)->&'a [u8]
where T:Sized
    let sz=std::mem::size_of::<T>();
    unsafe{slice::from_raw_parts(src as *const T as *const u8, sz)}

Is this a good method and is there other more standard way?


Unfortunately there is no such thing as POD in Rust (pleae correct me if I'm wrong!)

However, there is the #[repr(C)] attribute for. (You can read more about this at Stackoverflow).

If you annotate your structure with that, rust will guarantee, that the memory layout will be the same as it would be in C. This may include padding bytes!.

Depends on what you want to do, either sending just some bytes over network or doing nasty stuff like embedded register adressing, it may or may not work.
You should expand your question for clarify, what you are going to achieve.

My advice would be to define a trait (e.g. ToBinary) which then you can implement on your types that you want to send and they define how they should look like (similar to serializing/deserializing).