Provide the ability to zero padding bytes and return &[u8]
#494
Labels
compatibility-nonbreaking
Changes that are (likely to be) non-breaking
&[u8]
#494
Progress
KnownLayout
to require that fields implementKnownLayout
toofreeze
intrinsic (RFC 3605) will land and stabilize soon enough that we can rely on it instead; relaxKnownLayout
to not be recursive (and watch out for #1162)KnownLayout
requirement to implement this designDetails
Issues like this one demonstrate that it is sometimes useful to access the bytes of a type which cannot implement
AsBytes
. In these cases, it should be sound to:&[u8]
We would need to teach
KnownLayout
to be able to zero padding, e.g.:The only requirement for a type supporting this operation is that we know where its padding bytes are. The public API for this type could be in
KnownLayout
.As of this writing,
KnownLayout
does not require that a type's fields also beKnownLayout
. We are planning to add that requirement in order to support this design.Open questions
&T
(which we can't modify) into a buffer while initializing any padding bytes in the destination like musli-zerocopy does? See this discussion.fn zero_padding_and_get_bytes(&mut self) -> &[u8]
doesn't allow the type system to "remember" that a value has had its padding zeroed. Perhaps we could introduce a witness type and introduce a method like:fn zero_padding(&mut self) -> &mut AsBytesWitness<Self>
AsBytesWitness
would provide field projection into any field, but would only provide direct mutable access to fields which areAsBytes
(as writing to these fields cannot introduce uninitialized bytes)Related & prior art
freeze
intrinsic and related library functions rust-lang/rfcs#3605The text was updated successfully, but these errors were encountered: