Skip to content

Update docs for str::as_bytes_mut. #74381

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jul 16, 2020
Merged

Update docs for str::as_bytes_mut. #74381

merged 1 commit into from
Jul 16, 2020

Conversation

mbrubeck
Copy link
Contributor

  • Add "Safety" section describing UTF-8 invariant.

  • Remove mention of from_utf8_mut. It is not necessary to call
    a function to convert the byte slice back to a string slice. The
    original string becomes accessible again after the byte slice is
    no longer used (as shown in the example code).

@rust-highfive
Copy link
Contributor

r? @Mark-Simulacrum

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

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 15, 2020
/// # Safety
///
/// The caller must ensure that the content of the slice remains
/// valid UTF-8.
Copy link
Member

Choose a reason for hiding this comment

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

Per #71033, I think this is actually (potentially) too strict. We should be able to say something like "the slice must be valid UTF-8 when its lifetime ends" or something like that.

cc @RalfJung

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to reflect the change from #71033. Any suggestions for clearer or more precise wording are welcome.

* Add "Safety" section describing UTF-8 invariant.

* Remove mention of `from_utf8_mut`.  It is not necessary to call
  a function to convert the byte slice back to a string slice.  The
  original string becomes accessible again after the byte slice is
  no longer used (as shown in the example code).
@Mark-Simulacrum
Copy link
Member

@bors r+ rollup

I think in theory we could relax even further -- e.g., calling this method on a non-UTF-8 str seems fine. But I think this is good enough for now and does not expand scope, so does not need T-libs/lang signoff.

@bors
Copy link
Collaborator

bors commented Jul 15, 2020

📌 Commit 6da69ec has been approved by Mark-Simulacrum

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 15, 2020
Manishearth added a commit to Manishearth/rust that referenced this pull request Jul 16, 2020
Update docs for str::as_bytes_mut.

* Add "Safety" section describing UTF-8 invariant.

* Remove mention of `from_utf8_mut`.  It is not necessary to call
  a function to convert the byte slice back to a string slice.  The
  original string becomes accessible again after the byte slice is
  no longer used (as shown in the example code).
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 16, 2020
…arth

Rollup of 7 pull requests

Successful merges:

 - rust-lang#73421 (Clarify effect of orphan rule changes on From/Into)
 - rust-lang#74037 (Update reference to CONTRIBUTING.md)
 - rust-lang#74203 (Enforce the static symbol order.)
 - rust-lang#74295 (Add and fix BTreeMap comments)
 - rust-lang#74352 (Use local links in the alloc docs.)
 - rust-lang#74377 (Move libstd's default feature to libtest)
 - rust-lang#74381 (Update docs for str::as_bytes_mut.)

Failed merges:

r? @ghost
/// # Safety
///
/// The caller must ensure that the content of the slice is valid UTF-8
/// before the borrow ends and the underlying `str` is used.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
/// before the borrow ends and the underlying `str` is used.
/// before the borrow ends and the underlying `str` is used again.

/// The caller must ensure that the content of the slice is valid UTF-8
/// before the borrow ends and the underlying `str` is used.
///
/// Use of a `str` whose contents are not valid UTF-8 is undefined behavior.
Copy link
Member

Choose a reason for hiding this comment

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

At some point we might want to be more clear about language-UB vs library-UB, but for now this wording seems fine to me.

@bors bors merged commit a77813b into rust-lang:master Jul 16, 2020
@cuviper cuviper added this to the 1.47.0 milestone May 2, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants