Skip to content

Commit e410a68

Browse files
authored
Some string cleanups (#4090)
* chore: simplify `JsString::as_str` * chore: prefer `raw` keyword instead of `addr_of*` since it is soft deprecated * chore: mark some `&mut self` tagged methods `const` * chore: add `clippy::cast_ptr_alignment` to `JsString::as_str`
1 parent 0efd651 commit e410a68

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

core/string/src/builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
marker::PhantomData,
1010
mem::ManuallyDrop,
1111
ops::{Add, AddAssign},
12-
ptr::{self, addr_of_mut, NonNull},
12+
ptr::{self, NonNull},
1313
str::{self},
1414
};
1515

@@ -60,7 +60,7 @@ impl<D: Copy> JsStringBuilder<D> {
6060
/// - The elements at `old_len..new_len` must be initialized.
6161
///
6262
#[inline]
63-
pub unsafe fn set_len(&mut self, new_len: usize) {
63+
pub const unsafe fn set_len(&mut self, new_len: usize) {
6464
debug_assert!(new_len <= self.capacity());
6565

6666
self.len = new_len;
@@ -140,10 +140,10 @@ impl<D: Copy> JsStringBuilder<D> {
140140
///
141141
/// Caller should ensure that the inner is allocated.
142142
#[must_use]
143-
unsafe fn data(&self) -> *mut D {
143+
const unsafe fn data(&self) -> *mut D {
144144
// SAFETY:
145145
// Caller should ensure that the inner is allocated.
146-
unsafe { addr_of_mut!((*self.inner.as_ptr()).data).cast() }
146+
unsafe { (&raw mut (*self.inner.as_ptr()).data).cast() }
147147
}
148148

149149
/// Allocates when there is not sufficient capacity.
@@ -205,7 +205,7 @@ impl<D: Copy> JsStringBuilder<D> {
205205
///
206206
/// Caller should ensure the capacity is large enough to hold elements.
207207
#[inline]
208-
pub unsafe fn extend_from_slice_unchecked(&mut self, v: &[D]) {
208+
pub const unsafe fn extend_from_slice_unchecked(&mut self, v: &[D]) {
209209
// SAFETY: Caller should ensure the capacity is large enough to hold elements.
210210
unsafe {
211211
ptr::copy_nonoverlapping(v.as_ptr(), self.data().add(self.len()), v.len());
@@ -294,7 +294,7 @@ impl<D: Copy> JsStringBuilder<D> {
294294
///
295295
/// Caller should ensure the capacity is large enough to hold elements.
296296
#[inline]
297-
pub unsafe fn push_unchecked(&mut self, v: D) {
297+
pub const unsafe fn push_unchecked(&mut self, v: D) {
298298
// SAFETY: Caller should ensure the capacity is large enough to hold elements.
299299
unsafe {
300300
self.data().add(self.len()).write(v);

core/string/src/lib.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::{
4141
hash::{Hash, Hasher},
4242
mem::ManuallyDrop,
4343
process::abort,
44-
ptr::{self, addr_of, addr_of_mut, NonNull},
44+
ptr::{self, NonNull},
4545
str::FromStr,
4646
};
4747

@@ -532,6 +532,7 @@ impl JsString {
532532
/// Obtains the underlying [`&[u16]`][slice] slice of a [`JsString`]
533533
#[inline]
534534
#[must_use]
535+
#[allow(clippy::cast_ptr_alignment)]
535536
pub fn as_str(&self) -> JsStr<'_> {
536537
match self.ptr.unwrap() {
537538
UnwrappedTagged::Ptr(h) => {
@@ -552,26 +553,20 @@ impl JsString {
552553
// which means it is safe to read the `refcount` as `read_only` here.
553554
unsafe {
554555
let h = h.as_ptr();
555-
if (*h).refcount.read_only == 0 {
556+
let tagged_len = (*h).tagged_len;
557+
let len = tagged_len.len();
558+
let is_latin1 = tagged_len.is_latin1();
559+
let ptr = if (*h).refcount.read_only == 0 {
556560
let h = h.cast::<StaticJsString>();
557-
return if (*h).tagged_len.is_latin1() {
558-
JsStr::latin1(std::slice::from_raw_parts(
559-
(*h).ptr,
560-
(*h).tagged_len.len(),
561-
))
562-
} else {
563-
JsStr::utf16(std::slice::from_raw_parts(
564-
(*h).ptr.cast(),
565-
(*h).tagged_len.len(),
566-
))
567-
};
568-
}
561+
(*h).ptr
562+
} else {
563+
(&raw const (*h).data).cast::<u8>()
564+
};
569565

570-
let len = (*h).len();
571-
if (*h).is_latin1() {
572-
JsStr::latin1(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
566+
if is_latin1 {
567+
JsStr::latin1(std::slice::from_raw_parts(ptr, len))
573568
} else {
574-
JsStr::utf16(std::slice::from_raw_parts(addr_of!((*h).data).cast(), len))
569+
JsStr::utf16(std::slice::from_raw_parts(ptr.cast::<u16>(), len))
575570
}
576571
}
577572
}
@@ -611,7 +606,7 @@ impl JsString {
611606

612607
let string = {
613608
// SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer.
614-
let mut data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::<u8>() };
609+
let mut data = unsafe { (&raw mut (*ptr.as_ptr()).data).cast::<u8>() };
615610
for &string in strings {
616611
// SAFETY:
617612
// The sum of all `count` for each `string` equals `full_count`, and since we're
@@ -746,7 +741,7 @@ impl JsString {
746741
let ptr = Self::allocate_inner(count, string.is_latin1());
747742

748743
// SAFETY: `allocate_inner` guarantees that `ptr` is a valid pointer.
749-
let data = unsafe { addr_of_mut!((*ptr.as_ptr()).data).cast::<u8>() };
744+
let data = unsafe { (&raw mut (*ptr.as_ptr()).data).cast::<u8>() };
750745

751746
// SAFETY:
752747
// - We read `count = data.len()` elements from `data`, which is within the bounds of the slice.

0 commit comments

Comments
 (0)