Skip to content

Implement Write for Cursor<W> where W: AsMut<[u8]> #853

Description

@bushrat011899

Proposal

Problem statement

Currently, std::io::Write is implemented for std::io::Cursor<W>, where W is one of:

  • &mut [u8]
  • for<const N: usize> [u8; N]
  • for<A: Allocator> alloc::boxed::Box<[u8], A>
  • for<A: Allocator> alloc::vec::Vec<u8, A>
  • for<A: Allocator> &mut alloc::vec::Vec<u8, A>

To move Write and Cursor to core::io, rust-lang/rust#158537 introduced an indirection trait to allow alloc::io to provide the Vec and Box implementations for Cursor. This causes documentation issues, as the indirection trait is not meant for public visibility nor implementation. Further, this is incongruent with how Seek, Read, and BufRead work with Cursor, as they are all blanket implemented for AsRef<[u8]>.

Motivating examples or use cases

I am unaware of anyone requesting Cursor support Write for types other than the original 5. I am proposing this as fix for the documentation and cross-crate implementation issues currently encountered with core::io, that just so happens to extend the public API.

Solution sketch

SpecCursorWrite is shown below as a specialization trait to allow the Vec and &mut Vec implementations to extend, not just write within the existing slice. It is perma-unstable as an implementation detail.

// core::io

// perma-unstable
#[doc(hidden)]
#[unstable(feature = "core_io_internals", reason = "exposed only for specialization", issue = "none")]
#[rustc_specialization_trait]
pub trait SpecCursorWrite: Sized + AsMut<[u8]> { /* ... */ }

// backdated as it replaces implementations as far back as 1.0.0
#[stable(feature = "rust1", since = "1.0.0")]
impl<W> core::io::Write for core::io::Cursor<W> where W: AsMut<[u8]> { /* default ... */ }

// backdated as it replaces implementations as far back as 1.0.0
#[stable(feature = "rust1", since = "1.0.0")]
impl<W> core::io::Write for core::io::Cursor<W> where W: SpecCursorWrite { /* ... */ }

// alloc::io

#[doc(hidden)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> core::io::SpecCursorWrite for alloc::vec::Vec<u8, A> where A: core::alloc::Allocator { /* ... */ }

#[doc(hidden)]
#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
impl<A> core::io::SpecCursorWrite for &mut alloc::vec::Vec<u8, A> where A: core::alloc::Allocator { /* ... */ }

Alternatives

  • Do nothing
    • Functionality has not changed due to the indirection trait, but documentation has degraded. This could instead be addressed by changes to rustdoc (e.g., notable implementers/implementations)
  • Stabilize and/or publicize WriteThroughCursor
    • Intended as an implementation detail, and it effectively duplicates the Write trait where self is instead Cursor<Self>.
  • Move Vec and Box to core
    • Not possible in the near or mid term.
  • Move Write and/or Cursor to alloc::io
    • Would add a new blocker to core::io traits which couldn't be resolved with supertrait auto-impls, as is currently proposed for (eventually) moving Read and BufRead into core::io.

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libsRelevant to the libraries subteam, which will review and decide on the PR/issue.api-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions