Allow only implementing Read::read_buf
by WaffleLapkin · Pull Request #106643 · rust-lang/rust (original) (raw)
For a summary:
This extends std::io::Read
by allowing users to implement Read
by only providing a Read::read_buf
impl, and not a Read::read
impl. It does this using the unstable attribute rustc_must_implement_one_of
1.
#[rustc_must_implement_one_of(read, read_buf)] pub trait std::io::Read { // (new) fn read(&mut self, buf: &mut [u8]) -> Result { let mut buf = BorrowedBuf::from(buf); self.read_buf(buf.unfilled()).map(|()| buf.len()) } // (existing) fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()> { default_read_buf(|b| self.read(b), buf) } }
This does not make rustc_must_implement_must_of
user-visible, since read_buf
is unstable (altho IMO it could easily be forgotten when stabilizing).
Whether or not we want to start using the #[rustc_must_implement_one_of(...)]
attribute on the stdlib is a question that's split between lang (if they think the language feature has a path to stabilization) and libs-api (if they are okay with using it in that case). I'm on neither of these teams, and don't feel that strongly (but weakly, I think it probably needs a t-libs-api meeting to discuss after asking t-lang if it's okay start experimenting with)
r? @joshtriplett (who is on both relevant teams)
Footnotes
- Which allows a trait to have a pair (or perhaps more?) of functions each of which are implemented in terms of the other, so long as the implementation user provides an impl of at least one of them for their type. I cannot seem to find a tracking method for it, but do recall discussions involving it in the past. ↩