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

IntoBytes for MaybeUninit<T> #2172

Open
kartva opened this issue Dec 20, 2024 · 1 comment
Open

IntoBytes for MaybeUninit<T> #2172

kartva opened this issue Dec 20, 2024 · 1 comment
Labels
customer-request Documents customer requests.

Comments

@kartva
Copy link

kartva commented Dec 20, 2024

(not attaching the name of my project since it's still a prototype)

When building a zerocopy network protocol, I want to be able to have a function with the following signature:

pub unsafe fn send(&mut self, fill: impl FnOnce(&mut MaybeUninit<Payload>)) {
        let mem = &mut packet[packet_start_offset..];
        fill(MaybeUninit::<Payload>::mut_from_bytes(mem).unwrap());
        ...
}

so that users can just write to the uninit data and thus write to the underlying byte buffer.

Are there any reasons for why this isn't possible / desirable?

@kartva kartva added the customer-request Documents customer requests. label Dec 20, 2024
@kupiakos
Copy link
Contributor

The reason this isn't directly possible is because one can pass this in for fill: |x| *x = MaybeUninit::uninit(), thus deinitializing previously initialized memory in packet.

You want something like "out" references:

use uninit::extension_traits::{AsOut, ManuallyDropMut};
use uninit::out_ref::Out;

pub unsafe fn send(&mut self, fill: impl FnOnce(Out<'_, Payload>)) {
        let payload = Payload::mut_from_bytes(&mut packet[packet_start_offset..]).unwrap();
        fill(MaybeUninit::<Payload>::mut_from_bytes(payload.manually_drop_mut().as_out()).unwrap());
        ...
}

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

2 participants