rust icon indicating copy to clipboard operation
rust copied to clipboard

Add IoSlice::as_bytes

Open Lucretiel opened this issue 1 year ago • 19 comments

This PR proposes to add IoSlice::as_bytes. While IoSlice already has Deref<Target=[u8]>, this uses the lifetime of the IoSlice itself, throwing away the larger 'a lifetime; as_bytes allows for getting the original slice with the original lifetime.

Lucretiel avatar May 06 '23 04:05 Lucretiel

r? @m-ou-se

(rustbot has picked a reviewer for you, use r? to override)

rustbot avatar May 06 '23 04:05 rustbot

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

rustbot avatar May 06 '23 04:05 rustbot

My own personal use case for this is an attempt to write my own write_all_vectored. I ran into an issue where, given an &mut [IoSlice<'a>], it's not possible to rewrite those slices in-place as the write proceeds, because slicing an IoSlice borrows it (instead of using the original 'a lifetime), which precludes rewriting it in-place.

Lucretiel avatar May 06 '23 04:05 Lucretiel

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- src/io/mod.rs - io::IoSlice<'a>::as_bytes (line 1358) stdout ----
error[E0658]: use of unstable library feature 'io_slice_as_bytes'
 --> src/io/mod.rs:1364:21
  |
9 | let tail = io_slice.as_bytes()[3..];
  |
  = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
  = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable


error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/io/mod.rs:1364:5
  |
9 | let tail = io_slice.as_bytes()[3..];
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature
  = help: unsized locals are gated as an unstable feature
help: consider borrowing here
  |
9 | let tail = &io_slice.as_bytes()[3..];

error[E0308]: mismatched types
    --> src/io/mod.rs:1367:25
     |
     |
12   | io_slice = IoSlice::new(tail);
     |            |            |
     |            |            |
     |            |            expected `&[u8]`, found `[u8]`
     |            |            help: consider borrowing here: `&tail`
     |
note: associated function defined here
    --> /checkout/library/std/src/io/mod.rs:1262:12
     |
     |
1262 |     pub fn new(buf: &'a [u8]) -> IoSlice<'a> {

error[E0658]: use of unstable library feature 'io_slice_as_bytes'
  --> src/io/mod.rs:1369:21
   |
   |
14 | assert_eq!(io_slice.as_bytes(), b"def");
   |
   = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
   = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable

---
    src/io/mod.rs - io::IoSlice<'a>::as_bytes (line 1358)

test result: FAILED. 1116 passed; 1 failed; 17 ignored; 0 measured; 0 filtered out; finished in 16.26s

error: doctest failed, to rerun pass `-p std --doc`

rust-log-analyzer avatar May 06 '23 05:05 rust-log-analyzer

Looks like the classic sorts of failures where an innate method is added and starts to take precedence over a trait method. Maybe as_inner, instead?

Lucretiel avatar May 06 '23 05:05 Lucretiel

How about into_bytes? That fits well with methods like OccupiedEntry::into_mut.

joboet avatar May 06 '23 11:05 joboet

@rustbot label +T-libs-api -T-libs

aDotInTheVoid avatar May 06 '23 15:05 aDotInTheVoid

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
..............................................i...............................

failures:

---- src/io/mod.rs - io::IoSlice<'a>::into_bytes (line 1358) stdout ----
error[E0599]: no method named `as_bytes` found for struct `IoSlice<'_>` in the current scope
  |
  |
9 | let tail = io_slice.as_bytes()[3..];
  |                     ^^^^^^^^ method not found in `IoSlice<'_>`

error[E0599]: no method named `as_bytes` found for struct `IoSlice<'_>` in the current scope
   |
   |
14 | assert_eq!(io_slice.as_bytes(), b"def");
   |                     ^^^^^^^^ method not found in `IoSlice<'_>`
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0599`.
Couldn't compile the test.
Couldn't compile the test.

failures:
    src/io/mod.rs - io::IoSlice<'a>::into_bytes (line 1358)

test result: FAILED. 1116 passed; 1 failed; 17 ignored; 0 measured; 0 filtered out; finished in 14.65s

error: doctest failed, to rerun pass `-p std --doc`

rust-log-analyzer avatar May 06 '23 17:05 rust-log-analyzer

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- src/io/mod.rs - io::IoSlice<'a>::into_bytes (line 1358) stdout ----
error[E0658]: use of unstable library feature 'io_slice_as_bytes'
 --> src/io/mod.rs:1364:21
  |
9 | let tail = io_slice.into_bytes()[3..];
  |
  = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
  = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable


error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/io/mod.rs:1364:5
  |
9 | let tail = io_slice.into_bytes()[3..];
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature
  = help: unsized locals are gated as an unstable feature
help: consider borrowing here
  |
9 | let tail = &io_slice.into_bytes()[3..];

error[E0308]: mismatched types
    --> src/io/mod.rs:1367:25
     |
     |
12   | io_slice = IoSlice::new(tail);
     |            |            |
     |            |            |
     |            |            expected `&[u8]`, found `[u8]`
     |            |            help: consider borrowing here: `&tail`
     |
note: associated function defined here
    --> /checkout/library/std/src/io/mod.rs:1262:12
     |
     |
1262 |     pub fn new(buf: &'a [u8]) -> IoSlice<'a> {

error[E0658]: use of unstable library feature 'io_slice_as_bytes'
  --> src/io/mod.rs:1369:21
   |
   |
14 | assert_eq!(io_slice.into_bytes(), b"def");
   |
   = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
   = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable

---
    src/io/mod.rs - io::IoSlice<'a>::into_bytes (line 1358)

test result: FAILED. 1116 passed; 1 failed; 17 ignored; 0 measured; 0 filtered out; finished in 15.41s

error: doctest failed, to rerun pass `-p std --doc`

rust-log-analyzer avatar May 08 '23 21:05 rust-log-analyzer

:umbrella: The latest upstream changes (presumably #118154) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Nov 23 '23 02:11 bors

@Lucretiel if you can address the CI failure and the conflicts, we can put this for review

Dylan-DPC avatar Feb 09 '24 15:02 Dylan-DPC

Taking a look now, thanks for your patience!

Lucretiel avatar Feb 14 '24 17:02 Lucretiel

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
GITHUB_ACTION=__run_7
GITHUB_ACTIONS=true
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=Lucretiel
GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=master
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_0c8b8a5e-6629-4de0-9cb5-c830f01c16d8
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_PATH=/home/runner/work/_temp/_github_workflow/event.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=io-slice-as-bytes
GITHUB_JOB=pr
GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_0c8b8a5e-6629-4de0-9cb5-c830f01c16d8
GITHUB_REF=refs/pull/111277/merge
GITHUB_REF_NAME=111277/merge
GITHUB_REF_PROTECTED=false
---
GITHUB_SERVER_URL=https://github.com
GITHUB_SHA=c9e4874dececf104bf1c20ceb79c4fe897592969
GITHUB_STATE=/home/runner/work/_temp/_runner_file_commands/save_state_0c8b8a5e-6629-4de0-9cb5-c830f01c16d8
GITHUB_STEP_SUMMARY=/home/runner/work/_temp/_runner_file_commands/step_summary_0c8b8a5e-6629-4de0-9cb5-c830f01c16d8
GITHUB_TRIGGERING_ACTOR=Lucretiel
GITHUB_WORKFLOW_REF=rust-lang/rust/.github/workflows/ci.yml@refs/pull/111277/merge
GITHUB_WORKFLOW_SHA=c9e4874dececf104bf1c20ceb79c4fe897592969
GITHUB_WORKSPACE=/home/runner/work/rust/rust
GOROOT_1_19_X64=/opt/hostedtoolcache/go/1.19.13/x64
---
---- library/std/src/io/mod.rs - io::IoSlice<'a>::into_bytes (line 1499) stdout ----
error[E0658]: use of unstable library feature 'io_slice_as_bytes'
##[error] --> library/std/src/io/mod.rs:1505:21
  |
error: doctest failed, to rerun pass `-p std --doc`
Build completed unsuccessfully in 0:05:14
9 | let tail = io_slice.into_bytes()[3..];
  |
  = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
  = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable
  = note: this compiler was built on 2024-02-04; consider upgrading it if it is out of date
  = note: this compiler was built on 2024-02-04; consider upgrading it if it is out of date

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
##[error] --> library/std/src/io/mod.rs:1505:5
  |
9 | let tail = io_slice.into_bytes()[3..];
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature
  = help: unsized locals are gated as an unstable feature
help: consider borrowing here
  |
9 | let tail = &io_slice.into_bytes()[3..];

error[E0308]: mismatched types
##[error]    --> library/std/src/io/mod.rs:1508:25
     |
     |
12   | io_slice = IoSlice::new(tail);
     |            ------------ ^^^^ expected `&[u8]`, found `[u8]`
     |            arguments to this function are incorrect
     |
note: associated function defined here
    --> /checkout/library/std/src/io/mod.rs:1400:12
    --> /checkout/library/std/src/io/mod.rs:1400:12
     |
1400 |     pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
help: consider borrowing here
     |
     |
12   | io_slice = IoSlice::new(&tail);

error[E0658]: use of unstable library feature 'io_slice_as_bytes'
##[error]  --> library/std/src/io/mod.rs:1510:21
   |
   |
14 | assert_eq!(io_slice.into_bytes(), b"def");
   |
   = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
   = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable
   = note: this compiler was built on 2024-02-04; consider upgrading it if it is out of date

rust-log-analyzer avatar Feb 14 '24 18:02 rust-log-analyzer

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
GITHUB_ACTION=__run_7
GITHUB_ACTIONS=true
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=Lucretiel
GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=master
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_e8238a67-1c36-448d-b55f-80ef543825f4
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_PATH=/home/runner/work/_temp/_github_workflow/event.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=io-slice-as-bytes
GITHUB_JOB=pr
GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_e8238a67-1c36-448d-b55f-80ef543825f4
GITHUB_REF=refs/pull/111277/merge
GITHUB_REF_NAME=111277/merge
GITHUB_REF_PROTECTED=false
---
GITHUB_SERVER_URL=https://github.com
GITHUB_SHA=909df5258dd32997b63ae7418cef2ff9474b81e9
GITHUB_STATE=/home/runner/work/_temp/_runner_file_commands/save_state_e8238a67-1c36-448d-b55f-80ef543825f4
GITHUB_STEP_SUMMARY=/home/runner/work/_temp/_runner_file_commands/step_summary_e8238a67-1c36-448d-b55f-80ef543825f4
GITHUB_TRIGGERING_ACTOR=Lucretiel
GITHUB_WORKFLOW_REF=rust-lang/rust/.github/workflows/ci.yml@refs/pull/111277/merge
GITHUB_WORKFLOW_SHA=909df5258dd32997b63ae7418cef2ff9474b81e9
GITHUB_WORKSPACE=/home/runner/work/rust/rust
GOROOT_1_19_X64=/opt/hostedtoolcache/go/1.19.13/x64
---
#13 3.653 Building wheels for collected packages: reuse
#13 3.654   Building wheel for reuse (pyproject.toml): started
#13 3.985   Building wheel for reuse (pyproject.toml): finished with status 'done'
#13 3.986   Created wheel for reuse: filename=reuse-1.1.0-cp310-cp310-manylinux_2_35_x86_64.whl size=181117 sha256=f5f58750481f69515c2c0d1d503daf565e2565c370d07fc6aeb95fe3498b4269
#13 3.987   Stored in directory: /tmp/pip-ephem-wheel-cache-ge2ielii/wheels/c2/3c/b9/1120c2ab4bd82694f7e6f0537dc5b9a085c13e2c69a8d0c76d
#13 3.989 Installing collected packages: boolean-py, binaryornot, setuptools, reuse, python-debian, markupsafe, license-expression, jinja2, chardet
#13 4.013   Attempting uninstall: setuptools
#13 4.014     Found existing installation: setuptools 59.6.0
#13 4.015     Not uninstalling setuptools at /usr/lib/python3/dist-packages, outside environment /usr
---
.............................iiiii..................................................i... 1144/1160
................

failures:
error: doctest failed, to rerun pass `-p std --doc`
---- library/std/src/io/mod.rs - io::IoSlice<'a>::into_bytes (line 1499) stdout ----
error[E0658]: use of unstable library feature 'io_slice_as_bytes'
##[error] --> library/std/src/io/mod.rs:1505:22
  |
  |
9 | let tail = &io_slice.into_bytes()[3..];
  |
Build completed unsuccessfully in 0:05:23
  = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
  = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable
  = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable
  = note: this compiler was built on 2024-02-04; consider upgrading it if it is out of date

error[E0658]: use of unstable library feature 'io_slice_as_bytes'
##[error]  --> library/std/src/io/mod.rs:1510:21
   |
14 | assert_eq!(io_slice.into_bytes(), b"def");
   |
   = note: see issue #111277 <https://github.com/rust-lang/rust/issues/111277> for more information
   = help: add `#![feature(io_slice_as_bytes)]` to the crate attributes to enable
   = note: this compiler was built on 2024-02-04; consider upgrading it if it is out of date

rust-log-analyzer avatar Feb 14 '24 19:02 rust-log-analyzer

Doctests are failing because of the use of an unstable library feature (the one added in this PR); can someone walk me through how to fix?

Lucretiel avatar Feb 14 '24 23:02 Lucretiel

can someone walk me through how to fix?

Add the #![feature to the doctests. Eg

aDotInTheVoid avatar Feb 15 '24 12:02 aDotInTheVoid

Can we add the same for IoSliceMut? It looks like it has the same lifetime issue.

Mark-Simulacrum avatar May 13 '24 14:05 Mark-Simulacrum

Can we add the same for IoSliceMut? It looks like it has the same lifetime issue.

I'm actually pretty sure we can't, though I'd be happy to be proven wrong. A function resembling fn as_bytes(&self) -> &'a [u8] would end the self borrow when it returns, which would allow aliasing the mutable region. The best you can do is into_bytes_mut(self) -> &'a mut [u8], which doesn't offer many advantages over just dropping the IoSliceMut.

Lucretiel avatar May 13 '24 17:05 Lucretiel

In any case, this is (finally) ready to merge, just needs approval from someone.

Lucretiel avatar May 13 '24 17:05 Lucretiel