rust-lang / rust-lang/libs-team

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

Open
#853 6 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api-change-proposal
Dominant language
Rust
Stars
178
Forks
28
Avg merge
15m
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the initial implementation in rust-lang/rust#160960 and the core_io/alloc_io tracking issue #154046; the proposal identifies core::io and alloc::io as the entry points. Done means the API team reaches a decision and the approved Cursor Write implementation and related alloc integrations are implemented and reviewed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.