Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing &mut MaybeUninit<T> as &mut [u8] by zeroing #2318

Open
kupiakos opened this issue Feb 7, 2025 · 1 comment
Open

Accessing &mut MaybeUninit<T> as &mut [u8] by zeroing #2318

kupiakos opened this issue Feb 7, 2025 · 1 comment
Labels
customer-request Documents customer requests.

Comments

@kupiakos
Copy link
Contributor

kupiakos commented Feb 7, 2025

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 the Result, and write that 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::zero method to zero out all of the bytes, and then assume_init_mut().as_mut_bytes() to pass into read. This requires using unsafe.

There are two improvements to zerocopy I can consider to solve this problem:

  • Provide a zeroing method on FromBytes that 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-IntoBytes type by first ensuring all of the padding bytes are zeroed. While the &mut exists, the object cannot be overwritten with a T containing padding.

  • As part of Support MaybeUninit<T: ?Sized> via KnownLayout #1797, add a zero method to the zerocopy MaybeUninit that outputs &mut T just like write does. That'd solve this particular problem.

@kupiakos kupiakos added the customer-request Documents customer requests. label Feb 7, 2025
@kupiakos
Copy link
Contributor Author

kupiakos commented Feb 7, 2025

Having this method would also make issues like #2319 less likely, being provided a well-lit path to get a zeroed &mut [u8] out of a &mut T

joshlf added a commit that referenced this issue Feb 20, 2025
Closes #2318

gherrit-pr-id: I38a568578de67f067fab936d17ba57332c6cdfed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-request Documents customer requests.
Projects
None yet
Development

No branches or pull requests

1 participant