Rollup merge of #95753 - ChayimFriedman2:patch-1, r=dtolnay

Correct safety reasoning in `str::make_ascii_{lower,upper}case()`

I don't understand why the previous comment was used (it was inserted in #66564), but it doesn't explain why these functions are safe, only why `str::as_bytes{_mut}()` are safe.

If someone thinks they make perfect sense, I'm fine with closing this PR.
This commit is contained in:
Dylan DPC 2022-04-07 11:17:16 +02:00 committed by GitHub
commit 6639604bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2407,7 +2407,7 @@ impl str {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline] #[inline]
pub fn make_ascii_uppercase(&mut self) { pub fn make_ascii_uppercase(&mut self) {
// SAFETY: safe because we transmute two types with the same layout. // SAFETY: changing ASCII letters only does not invalidate UTF-8.
let me = unsafe { self.as_bytes_mut() }; let me = unsafe { self.as_bytes_mut() };
me.make_ascii_uppercase() me.make_ascii_uppercase()
} }
@ -2434,7 +2434,7 @@ impl str {
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")] #[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
#[inline] #[inline]
pub fn make_ascii_lowercase(&mut self) { pub fn make_ascii_lowercase(&mut self) {
// SAFETY: safe because we transmute two types with the same layout. // SAFETY: changing ASCII letters only does not invalidate UTF-8.
let me = unsafe { self.as_bytes_mut() }; let me = unsafe { self.as_bytes_mut() };
me.make_ascii_lowercase() me.make_ascii_lowercase()
} }