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

Make ReservedSpace more flexible #254

Closed
nolanderc opened this issue Apr 29, 2024 · 1 comment · Fixed by #256
Closed

Make ReservedSpace more flexible #254

nolanderc opened this issue Apr 29, 2024 · 1 comment · Fixed by #256
Milestone

Comments

@nolanderc
Copy link
Contributor

nolanderc commented Apr 29, 2024

The only way to use ReservedSpace currently is to use the std::io::Write implementation. However, this assumes that the entire value can be written in one go. This prevents certain patterns, such as having a header in the value which contains the a checksum of the remaining bytes.

There should be some mechanism to reference arbitrary bytes within the space. Some options (naming TBD):

  1. Add a method to get all previously written bytes: fn written_bytes_mut(&mut self) -> &mut [u8]. This is safe as the bytes are known to be initialized.
  2. Add a method to zero-initialize and get a mutable view of the entire space: fn zero_bytes_mut(&mut self) -> &mut [u8]. This would mark the entire space as written.
  3. Add a method fn as_bytes_mut_uninit(&mut self) -> &mut [MaybeUninit<u8>]. Can be used to write arbitrary data in any order. Needs a complementary method to mark them as written: unsafe fn assume_written(&mut self, count: usize), which is unsafe because caller must guarantee that all bytes 0..count have been initialized.
@Kerollmops
Copy link
Member

Thank you for this issue. It also summarizes some of my previous ideas. I wanted to only expose a &mut [MaybeUninit<u8>] to users and let them write to it using the MaybeUninit::write_slice method. Unfortunately, it's still unstable...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants