Implementing safe memcpy with Rust.
I'm new to rust so I apologize if this is an obvious question. In C++, if I had a POD type I wanted to read out of a binary buffer I can do the following to safely "cast" the buffer to the given type:
template<typename T>
T buffer_cast(const char* buff, size_t len)
{
if(len < sizeof(T)) {//throw}
T tmp;
memcpy(&tmp, buff, sizeof tmp);
return tmp;
}
Looking around it seems like in rust the options are to use the ByteOrder library which doesn't seem to be able to read entire structs or do the equivalent of reinterpret_cast using transmute which wont necessarily guarantee alignment like the above example does. What's the idiomatic way to do this in rust?
Posted Aug 29, 2016, 11:49 PM UTC
75