Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 81 additions & 33 deletions library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,54 @@ where
}
}

/// Trait used to allow indirect implementation of `Write` for `Cursor<Self>`.
/// Since [`Cursor`] is not a foundational type, it is not possible to implement
/// `Write` for `Cursor<T>` if `Write` is defined in `libcore` and `T` is in a
/// downstream crate (e.g., `liballoc` or `libstd`).
///
/// Methods are identical in purpose and meaning to their `Write` namesakes.
trait WriteThroughCursor: Sized {
Comment thread
bushrat011899 marked this conversation as resolved.
fn write(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<usize>;
fn write_vectored(this: &mut Cursor<Self>, bufs: &[IoSlice<'_>]) -> io::Result<usize>;
fn is_write_vectored(this: &Cursor<Self>) -> bool;
fn write_all(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<()>;
fn write_all_vectored(this: &mut Cursor<Self>, bufs: &mut [IoSlice<'_>]) -> io::Result<()>;
fn flush(this: &mut Cursor<Self>) -> io::Result<()>;
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<W: WriteThroughCursor> Write for Cursor<W> {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
WriteThroughCursor::write(self, buf)
}

#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
WriteThroughCursor::write_vectored(self, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
WriteThroughCursor::is_write_vectored(self)
}

#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
WriteThroughCursor::write_all(self, buf)
}

#[inline]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
WriteThroughCursor::write_all_vectored(self, bufs)
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
WriteThroughCursor::flush(self)
}
}

// Non-resizing write implementation
#[inline]
fn slice_write(pos_mut: &mut u64, slice: &mut [u8], buf: &[u8]) -> io::Result<usize> {
Expand Down Expand Up @@ -348,117 +396,117 @@ impl Write for Cursor<&mut [u8]> {
}

#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
impl<A> Write for Cursor<&mut Vec<u8, A>>
impl<A> WriteThroughCursor for &mut Vec<u8, A>

@clarfonthey clarfonthey Jul 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So, main concern is this unfortunately isn't going to be shown under the implementations for Write, and I'm not really sure what the best way to deal with that is. rustdoc doesn't really seem to have a method to create "fake" impls for docs only, so maybe there should be some kind of issue tracking a way to deal with this.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think there would need to be some kind of "flatten" feature for listing implementations. Ignoring instability for a moment, adding this trait would hide the Write implementations for:

  • Cursor<&mut Vec<u8, A>> where A: Allocator
  • Cursor<Vec<u8, A>> where A: Allocator
  • Cursor<Box<[u8], A>> where A: Allocator

And would instead show:

  • Cursor<T> where T: WriteThroughCursor

Which would require users to visit WriteThroughCursor to see it is implemented for:

  • &mut Vec<u8, A>
  • Vec<u8, A>
  • Box<[u8], A>

Ideally, we could do something like:

#[doc(flatten = "T")]
impl<T: WriteThroughCursor> Write for Cursor<T> { /* ... */ }

To recreate the original flat documentation.

where
A: Allocator,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let (pos, inner) = self.into_parts_mut();
fn write(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<usize> {
let (pos, inner) = this.into_parts_mut();
vec_write_all(pos, inner, buf)
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let (pos, inner) = self.into_parts_mut();
fn write_vectored(this: &mut Cursor<Self>, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let (pos, inner) = this.into_parts_mut();
vec_write_all_vectored(pos, inner, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
fn is_write_vectored(_this: &Cursor<Self>) -> bool {
true
}

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
let (pos, inner) = self.into_parts_mut();
fn write_all(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<()> {
let (pos, inner) = this.into_parts_mut();
vec_write_all(pos, inner, buf)?;
Ok(())
}

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
let (pos, inner) = self.into_parts_mut();
fn write_all_vectored(this: &mut Cursor<Self>, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
let (pos, inner) = this.into_parts_mut();
vec_write_all_vectored(pos, inner, bufs)?;
Ok(())
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
fn flush(_this: &mut Cursor<Self>) -> io::Result<()> {
Ok(())
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<A> Write for Cursor<Vec<u8, A>>
impl<A> WriteThroughCursor for Vec<u8, A>
where
A: Allocator,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let (pos, inner) = self.into_parts_mut();
fn write(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<usize> {
let (pos, inner) = this.into_parts_mut();
vec_write_all(pos, inner, buf)
}

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let (pos, inner) = self.into_parts_mut();
fn write_vectored(this: &mut Cursor<Self>, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let (pos, inner) = this.into_parts_mut();
vec_write_all_vectored(pos, inner, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
fn is_write_vectored(_this: &Cursor<Self>) -> bool {
true
}

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
let (pos, inner) = self.into_parts_mut();
fn write_all(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<()> {
let (pos, inner) = this.into_parts_mut();
vec_write_all(pos, inner, buf)?;
Ok(())
}

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
let (pos, inner) = self.into_parts_mut();
fn write_all_vectored(this: &mut Cursor<Self>, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
let (pos, inner) = this.into_parts_mut();
vec_write_all_vectored(pos, inner, bufs)?;
Ok(())
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
fn flush(_this: &mut Cursor<Self>) -> io::Result<()> {
Ok(())
}
}

#[stable(feature = "cursor_box_slice", since = "1.5.0")]
impl<A> Write for Cursor<Box<[u8], A>>
impl<A> WriteThroughCursor for Box<[u8], A>
where
A: Allocator,
{
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let (pos, inner) = self.into_parts_mut();
fn write(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<usize> {
let (pos, inner) = this.into_parts_mut();
slice_write(pos, inner, buf)
}

#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let (pos, inner) = self.into_parts_mut();
fn write_vectored(this: &mut Cursor<Self>, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
let (pos, inner) = this.into_parts_mut();
slice_write_vectored(pos, inner, bufs)
}

#[inline]
fn is_write_vectored(&self) -> bool {
fn is_write_vectored(_this: &Cursor<Self>) -> bool {
true
}

#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
let (pos, inner) = self.into_parts_mut();
fn write_all(this: &mut Cursor<Self>, buf: &[u8]) -> io::Result<()> {
let (pos, inner) = this.into_parts_mut();
slice_write_all(pos, inner, buf)
}

#[inline]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
let (pos, inner) = self.into_parts_mut();
fn write_all_vectored(this: &mut Cursor<Self>, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
let (pos, inner) = this.into_parts_mut();
slice_write_all_vectored(pos, inner, bufs)
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
fn flush(_this: &mut Cursor<Self>) -> io::Result<()> {
Ok(())
}
}
Expand Down
Loading