Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add std::io::IoSlice::as_slice method #124680

Closed
wants to merge 1 commit into from

Conversation

nolanderc
Copy link
Contributor

Fixes #124659.

This allows getting a slice with the same lifetime as the wrapped bytes.

impl<'a> IoSlice<'a> {
    pub fn as_slice(&self) -> &'a [u8] {
        // ...
    }
}

This works as the IoSlice is essentially just a transparent wrapper around &'a [u8], but with a different layout. Since &[u8] implements Copy, this is essentially the same as just copying that &[u8] out of the IoSlice.

@rustbot
Copy link
Collaborator

rustbot commented May 3, 2024

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 3, 2024
@rust-log-analyzer

This comment has been minimized.

@joboet
Copy link
Member

joboet commented May 3, 2024

CC #111277, which ended up choosing into_bytes as name.
Also see the (accepted) ACP rust-lang/libs-team#93.

This allows getting a slice with the same lifetime as the wrapped bytes.
@nolanderc nolanderc force-pushed the ioslice-as-bytes branch from 420aca8 to a3a7d5c Compare May 3, 2024 22:00
@nolanderc
Copy link
Contributor Author

Alright, missed that previous discussion. Closing this in favor of #111277

@nolanderc nolanderc closed this May 3, 2024
@rust-log-analyzer
Copy link
Collaborator

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)
---- library/std/src/io/mod.rs - io::IoSlice<'a>::as_slice (line 1397) stdout ----
error[E0308]: mismatched types
##[error]    --> library/std/src/io/mod.rs:1406:20
     |
12   | buf = IoSlice::new(buf.as_slice()[1..4]);
     |       ------------ ^^^^^^^^^^^^^^^^^^^^ expected `&[u8]`, found `[u8]`
     |       arguments to this function are incorrect
     |
note: associated function defined here
    --> /checkout/library/std/src/io/mod.rs:1384:12
    --> /checkout/library/std/src/io/mod.rs:1384:12
     |
1384 |     pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
help: consider borrowing here
     |
     |
12   | buf = IoSlice::new(&buf.as_slice()[1..4]);

error: aborting due to 1 previous error

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

failures:
    library/std/src/io/mod.rs - io::IoSlice<'a>::as_slice (line 1397)

test result: FAILED. 1148 passed; 1 failed; 20 ignored; 0 measured; 0 filtered out; finished in 31.93s

error: doctest failed, to rerun pass `-p std --doc`
  local time: Fri May  3 22:21:01 UTC 2024
  network time: Fri, 03 May 2024 22:21:01 GMT
##[error]Process completed with exit code 1.
Post job cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-solid Operating System: SOLID O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IoSlice: the Deref impl is too restrictive on the lifetime of the slice, an alternative is needed.
5 participants