diff --git a/core/string/src/builder.rs b/core/string/src/builder.rs index 7a2154e4383..d1707865147 100644 --- a/core/string/src/builder.rs +++ b/core/string/src/builder.rs @@ -9,7 +9,7 @@ use std::{ marker::PhantomData, mem::ManuallyDrop, ops::{Add, AddAssign}, - ptr::{self, addr_of_mut, NonNull}, + ptr::{self, NonNull}, str::{self}, }; @@ -60,7 +60,7 @@ impl JsStringBuilder { /// - The elements at `old_len..new_len` must be initialized. /// #[inline] - pub unsafe fn set_len(&mut self, new_len: usize) { + pub const unsafe fn set_len(&mut self, new_len: usize) { debug_assert!(new_len <= self.capacity()); self.len = new_len; @@ -140,10 +140,10 @@ impl JsStringBuilder { /// /// Caller should ensure that the inner is allocated. #[must_use] - unsafe fn data(&self) -> *mut D { + const unsafe fn data(&self) -> *mut D { // SAFETY: // Caller should ensure that the inner is allocated. - unsafe { addr_of_mut!((*self.inner.as_ptr()).data).cast() } + unsafe { (&raw mut (*self.inner.as_ptr()).data).cast() } } /// Allocates when there is not sufficient capacity. @@ -205,7 +205,7 @@ impl JsStringBuilder { /// /// Caller should ensure the capacity is large enough to hold elements. #[inline] - pub unsafe fn extend_from_slice_unchecked(&mut self, v: &[D]) { + pub const unsafe fn extend_from_slice_unchecked(&mut self, v: &[D]) { // SAFETY: Caller should ensure the capacity is large enough to hold elements. unsafe { ptr::copy_nonoverlapping(v.as_ptr(), self.data().add(self.len()), v.len()); @@ -294,7 +294,7 @@ impl JsStringBuilder { /// /// Caller should ensure the capacity is large enough to hold elements. #[inline] - pub unsafe fn push_unchecked(&mut self, v: D) { + pub const unsafe fn push_unchecked(&mut self, v: D) { // SAFETY: Caller should ensure the capacity is large enough to hold elements. unsafe { self.data().add(self.len()).write(v); diff --git a/core/string/src/lib.rs b/core/string/src/lib.rs index b64004a008f..f134817c024 100644 --- a/core/string/src/lib.rs +++ b/core/string/src/lib.rs @@ -45,7 +45,7 @@ use std::{ iter::Peekable, mem::ManuallyDrop, process::abort, - ptr::{self, addr_of, addr_of_mut, NonNull}, + ptr::{self, NonNull}, str::FromStr, }; @@ -321,6 +321,7 @@ impl JsString { /// Obtains the underlying [`&[u16]`][slice] slice of a [`JsString`] #[inline] #[must_use] + #[allow(clippy::cast_ptr_alignment)] pub fn as_str(&self) -> JsStr<'_> { match self.ptr.unwrap() { UnwrappedTagged::Ptr(h) => { @@ -341,26 +342,20 @@ impl JsString { // which means it is safe to read the `refcount` as `read_only` here. unsafe { let h = h.as_ptr(); - if (*h).refcount.read_only == 0 { + let tagged_len = (*h).tagged_len; + let len = tagged_len.len(); + let is_latin1 = tagged_len.is_latin1(); + let ptr = if (*h).refcount.read_only == 0 { let h = h.cast::(); - return if (*h).tagged_len.is_latin1() { - JsStr::latin1(std::slice::from_raw_parts( - (*h).ptr, - (*h).tagged_len.len(), - )) - } else { - JsStr::utf16(std::slice::from_raw_parts( - (*h).ptr.cast(), - (*h).tagged_len.len(), - )) - }; - } + (*h).ptr + } else { + (&raw const (*h).data).cast::() + }; - let len = (*h).len(); - if (*h).is_latin1() { - JsStr::latin1(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len)) + if is_latin1 { + JsStr::latin1(std::slice::from_raw_parts(ptr, len)) } else { - JsStr::utf16(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len)) + JsStr::utf16(std::slice::from_raw_parts(ptr.cast::(), len)) } } } @@ -400,7 +395,7 @@ impl JsString { let string = { // SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer. - let mut data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::() }; + let mut data = unsafe { (&raw mut (*ptr.as_ptr()).data).cast::() }; for &string in strings { // SAFETY: // The sum of all `count` for each `string` equals `full_count`, and since we're @@ -807,7 +802,7 @@ impl JsString { let ptr = Self::allocate_inner(count, string.is_latin1()); // SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer. - let data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::() }; + let data = unsafe { (&raw mut (*ptr.as_ptr()).data).cast::() }; // SAFETY: // - We read `count = data.len()` elements from `data`, which is within the bounds of the slice.