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 IoSlice::as_bytes #111277

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Conversation

Lucretiel
Copy link
Contributor

@Lucretiel Lucretiel commented May 6, 2023

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.

ACP: rust-lang/libs-team#93
Fixes #124659

@rustbot
Copy link
Collaborator

rustbot commented May 6, 2023

r? @m-ou-se

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

@rustbot rustbot added 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 6, 2023
@rustbot
Copy link
Collaborator

rustbot commented May 6, 2023

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

@Lucretiel
Copy link
Contributor Author

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.

@rust-log-analyzer

This comment has been minimized.

@Lucretiel
Copy link
Contributor Author

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?

@joboet
Copy link
Contributor

joboet commented May 6, 2023

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

@aDotInTheVoid
Copy link
Member

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

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 6, 2023
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@workingjubilee workingjubilee added the A-io Area: std::io, std::fs, std::net and std::path label Jul 30, 2023
@bors
Copy link
Contributor

bors commented Nov 23, 2023

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

@Dylan-DPC
Copy link
Member

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

@Dylan-DPC Dylan-DPC added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 9, 2024
@Lucretiel
Copy link
Contributor Author

Taking a look now, thanks for your patience!

@rustbot rustbot added O-solid Operating System: SOLID O-unix Operating system: Unix-like labels Feb 14, 2024
@rustbot rustbot added O-wasi Operating system: Wasi, Webassembly System Interface O-windows Operating system: Windows labels Feb 14, 2024
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Lucretiel
Copy link
Contributor Author

Lucretiel commented Feb 14, 2024

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?

@aDotInTheVoid
Copy link
Member

can someone walk me through how to fix?

Add the #![feature to the doctests. Eg

@Dylan-DPC Dylan-DPC added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 16, 2024
@Mark-Simulacrum
Copy link
Member

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

@Lucretiel
Copy link
Contributor Author

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
Copy link
Contributor Author

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

///
/// assert_eq!(io_slice.into_bytes(), b"def");
/// ```
#[unstable(feature = "io_slice_as_bytes", issue = "111277")]
Copy link
Contributor

@tgross35 tgross35 Jul 17, 2024

Choose a reason for hiding this comment

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

The issue number points to this PR; you should open a tracking issue and link it instead.

Also feature gate io_slice_as_bytes -> io_slice_into_bytes if that is the name.

Edit: io_slice_as_slice with the below.

@tgross35
Copy link
Contributor

The ACP at rust-lang/libs-team#93 covers the change here. Two changes from this PR are that the accepted name was as_slice (though presumably this is up for bikeshedding), and that it can be implemented for IoSliceMut too. Could you make these changes, as well as updating the unstable attribute as mentioned above?

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io Area: std::io, std::fs, std::net and std::path 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-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API 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.