tokio-rs / io-uring

The `io_uring` library for Rust
Apache License 2.0
1.16k stars 129 forks source link

Add mutable RecvMsgOut #261

Closed SUPERCILEX closed 2 months ago

SUPERCILEX commented 7 months ago

Parsing control data usually requires mutation to be safe (so parsing the same slice doesn't return multiple OwnedFds that point to the same FD).

I had to make the buffer fields public and use unsafe which is annoying, but since rust can't prove we don't alias the buffer it's the only way to get multiple slices out of it AFAIK.

quininer commented 6 months ago

I don't really like this macro, is it possible to implement it using generics?

like

pub struct RecvMsgOut<BUF: MsgBuf> {
    // ...
}

trait MsgBuf {
    type Pointer: Copy;

    fn from_raw_parts(ptr: Self::Pointer, len: usize) -> Self;
    fn as_ptr(&self) -> Self::Pointer;
}

impl MsgBuf for &[u8] {
    type Pointer = *const u8;

    // ...
}

impl MsgBuf for &mut [u8] {
    type Pointer = *mut u8;

    // ...
}
SUPERCILEX commented 6 months ago

Sorry, I'm a little unclear on what you're suggesting. What does the public API look like? Do we still have public fields with different mutability? Or are we keeping the public methods and implementing mutable variants when the generic parse input is a mut slice? And then does that mean we use unsafe to create the slices rather than store them in the struct?

SUPERCILEX commented 2 months ago

Closing as I'm probably the only one using this. The canonical implementation now lives in https://github.com/SUPERCILEX/clipboard-history/blob/9662f08b43502feafceec0c694237498bc7ea127/server/src/io_uring.rs#L28