Currently Bytes only implements From<Vec<u8>>, and bytemuck doesn't provide conversion from Vec<T> of arbitrary alignment to Vec<u8>.
I could use a Vec<u8> as a Vec<T>.
Pushing becomes std::io::Write::write(bytemuck::bytes_of(&t)).
And view the Vec<u8> as &[T] using bytemuck::cast_slice().
But it is a little bit inconvenient.
This may actually fail in some cases.
what makes your type not able to use cast_vec()?
bytes::Bytes::from_owner takes impl AsRef<[u8]>, so you could write a custom wrapper implementing it to convert Vec<T> into bytes::Bytes like this:
Thanks. This is exactly what I need.
try_cast_vec() only works when
- The start and end content type of the
Vecmust have the exact same alignment.
Thanks for replying. But leaking is not what I need.