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

Implement BufMut for &mut [MaybeUninit<u8>] #597

Merged
merged 1 commit into from
Feb 10, 2023
Merged

Conversation

mina86
Copy link
Contributor

@mina86 mina86 commented Feb 9, 2023

This will allow using BufMut without having to initialise memory
first. In particular, this will allow simple integration with tokio:

#![feature(maybe_uninit_slice)]

use tokio::io::AsyncReadExt;

let mut tokio_file = /* ... */;
let mut buf = [MaybeUninit::uninit(); 4096];
let len = tokio_file.read_buf(&mut &mut buf[..]);
// SAFETY: read initialised first len bytes of the buffer.
let buf = unsafe {
    MaybeUninit::slice_assume_init_mut(&buf[..len])
};

src/buf/buf_mut.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few more comments on the tests, but otherwise it looks good to me.

tests/test_buf_mut.rs Outdated Show resolved Hide resolved
tests/test_buf_mut.rs Outdated Show resolved Hide resolved
tests/test_buf_mut.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mina86
Copy link
Contributor Author

mina86 commented Feb 9, 2023

Ha, sorry, was actually in the middle of redoing tests. Now &mut [u8] and &mut [MaybeUninit<u8>] have the same tests.

This will allow using BufMut without having to initialise memory
first.  In particular, this will allow simple integration with tokio:

    #![feature(maybe_uninit_slice)]

    use tokio::io::AsyncReadExt;

    let mut tokio_file = /* ... */;
    let mut buf = [MaybeUninit::uninit(); 4096];
    let len = tokio_file.read_buf(&mut &mut buf[..]);
    // SAFETY: read initialised first len bytes of the buffer.
    let buf = unsafe {
        MaybeUninit::slice_assume_init_mut(&buf[..len])
    };
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@Darksonn Darksonn merged commit b29112c into tokio-rs:master Feb 10, 2023
@Darksonn Darksonn mentioned this pull request Sep 6, 2023
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 this pull request may close these issues.

None yet

2 participants