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

Tracking issue for `Read::initializer` #42788

Open
sfackler opened this Issue Jun 21, 2017 · 7 comments

Comments

Projects
None yet
6 participants
@sfackler
Copy link
Member

sfackler commented Jun 21, 2017

Implemented in #42002.

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Aug 29, 2017

I'm personally no longer sold on the infrastructure that we've got here I think. I was initially hoping that we'd have some safe way of working with these APIs and such, but in the end it all turned up unsafe. In light of all that I'd personally advocate for much simpler API:

impl Read {
    #[inline]
    unsafe fn may_read_buffer(&self) -> bool { true }
}

I don't think the complexity we have today is worth the cost currently, and I'd imagine that we could start making abstractions like:

fn initialize_buffer_for(r: &Read, buf: &mut [u8]) {
    // ...
}
@tbu-

This comment has been minimized.

Copy link
Contributor

tbu- commented Dec 13, 2017

I wrote a tiny library for reading into uninitialized buffers: https://crates.io/crates/buffer/

Maybe its ideas could be adapted for the standard library. The following is safe code using the library:

let mut vec = Vec::with_capacity(1024);
if reader.read_buffer(&mut vec)?.len() != 0 {
    if vec[0] == 0 {
        // ...
    }
}

Adoption of something like this in the standard library would probably also help with adoption by third party crates, see e.g. carllerche/mio#628.

@seanmonstar

This comment has been minimized.

Copy link
Contributor

seanmonstar commented Feb 21, 2018

An alternative to the current API is to go along the path that TrustedLen did, which is just to have an unsafe trait.

unsafe trait ReadUninit: Read {}

With specialization landing, anyone who cares to can take advantage of ReadUinit:

impl<T: Read> ReadBuf for T {
    fn read_buf<B: BufMut>(&mut self, buf: &mut B) {
        self.read(buf.zeroed())
    }
}

impl<T: ReadUninit> ReadBuf for T {
    fn read_buf<B: BufMut>(&mut self, buf: &mut B) {
        self.read(buf.bytes())
    }
}
@sfackler

This comment has been minimized.

Copy link
Member Author

sfackler commented Feb 21, 2018

That's incompatible with trait objects which are used pretty heavily with Read in particular.

@seanmonstar

This comment has been minimized.

Copy link
Contributor

seanmonstar commented Feb 21, 2018

Specialization is incompatible with trait objects?

@sfackler

This comment has been minimized.

Copy link
Member Author

sfackler commented Feb 21, 2018

If I have a &mut Read, I have no way of checking if it implements ReadUninit or not.

@jethrogb

This comment has been minimized.

Copy link
Contributor

jethrogb commented Jan 28, 2019

@RalfJung suggested a function that takes &mut [MaybeUninit<u8>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.