-
Notifications
You must be signed in to change notification settings - Fork 128
Description
I have a &mut MaybeUninit<T>. T: FromBytes and is very large. I want to initialize it with data from an io::Read::read method in a closure and output the initialized &mut T.
Today, my options are:
FromBytes::read_from_io, unpack theResult, andwritethat to the&mut MaybeUninit<T>. However, I've already proven that return-value optimization doesn't optimize away the temporary objects and memory copies, which is why I'm using a&mut MaybeUninit<T>in the first place. We're trying to do zero copies after all 😄- Use the
FromZeros::zeromethod to zero out all of the bytes, and thenassume_init_mut().as_mut_bytes()to pass intoread. This requires usingunsafe.
There are two improvements to zerocopy I can consider to solve this problem:
-
Provide a zeroing method on
FromBytesthat outputs a&mut [u8]. This would be valuable for my above situation and for an extra reason: it allows users to get a[u8]out of a non-IntoBytestype by first ensuring all of the padding bytes are zeroed. While the&mutexists, the object cannot be overwritten with aTcontaining padding. -
As part of Support
MaybeUninit<T: ?Sized>viaKnownLayout#1797, add azeromethod to the zerocopyMaybeUninitthat outputs&mut Tjust likewritedoes. That'd solve this particular problem.