From 4b5ce32613ea28004615b0d86ee9e5e9e8611d78 Mon Sep 17 00:00:00 2001 From: CrazyboyQCD Date: Wed, 18 Dec 2024 10:16:28 +0800 Subject: [PATCH 1/4] chore: simplify `JsString::as_str` --- core/string/src/lib.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/core/string/src/lib.rs b/core/string/src/lib.rs index b64004a008f..35572cbf506 100644 --- a/core/string/src/lib.rs +++ b/core/string/src/lib.rs @@ -341,26 +341,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(), - )) + (*h).ptr } else { - JsStr::utf16(std::slice::from_raw_parts( - (*h).ptr.cast(), - (*h).tagged_len.len(), - )) + (&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)) } } } From 48153a040a71cd45d2014cf699ed3f85614121d2 Mon Sep 17 00:00:00 2001 From: CrazyboyQCD Date: Wed, 18 Dec 2024 10:18:49 +0800 Subject: [PATCH 2/4] chore: prefer `raw` keyword instead of `addr_of*` since it is soft deprecated --- core/string/src/builder.rs | 4 ++-- core/string/src/lib.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/string/src/builder.rs b/core/string/src/builder.rs index 7a2154e4383..4c41db6b700 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}, }; @@ -143,7 +143,7 @@ impl JsStringBuilder { 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. diff --git a/core/string/src/lib.rs b/core/string/src/lib.rs index 35572cbf506..27f1be5c3cd 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, }; @@ -347,9 +347,9 @@ impl JsString { let ptr = if (*h).refcount.read_only == 0 { let h = h.cast::(); (*h).ptr - } else { + } else { (&raw const (*h).data).cast::() - }; + }; if is_latin1 { JsStr::latin1(std::slice::from_raw_parts(ptr, len)) @@ -394,7 +394,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 @@ -801,7 +801,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. From 8a16f4e3146f4fe188f2266818699ca468d9a9d6 Mon Sep 17 00:00:00 2001 From: CrazyboyQCD Date: Wed, 18 Dec 2024 10:19:36 +0800 Subject: [PATCH 3/4] chore: mark some `&mut self` tagged methods `const` --- core/string/src/builder.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/string/src/builder.rs b/core/string/src/builder.rs index 4c41db6b700..d1707865147 100644 --- a/core/string/src/builder.rs +++ b/core/string/src/builder.rs @@ -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,7 +140,7 @@ 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 { (&raw mut (*self.inner.as_ptr()).data).cast() } @@ -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); From 3be713aeaa2c5166f36628c835e783e3ac9f6f4a Mon Sep 17 00:00:00 2001 From: CrazyboyQCD Date: Wed, 18 Dec 2024 10:37:18 +0800 Subject: [PATCH 4/4] chore: add `clippy::cast_ptr_alignment` to `JsString::as_str` --- core/string/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/core/string/src/lib.rs b/core/string/src/lib.rs index 27f1be5c3cd..f134817c024 100644 --- a/core/string/src/lib.rs +++ b/core/string/src/lib.rs @@ -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) => {