Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5493b7b

Browse files
committed
make bots happier
1 parent 6353683 commit 5493b7b

File tree

1 file changed

+20
-10
lines changed
  • turbopack/crates/turbo-rcstr/src

1 file changed

+20
-10
lines changed

turbopack/crates/turbo-rcstr/src/lib.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ unsafe impl Sync for RcStr {}
8181

8282
// Marks a payload that is stored in an Arc
8383
const DYNAMIC_TAG: u8 = 0b_00;
84-
const DYNAMIC_LOCATION: u8 = 0b_0;
84+
const PREHASHED_STRING_LOCATION: u8 = 0b_0;
8585
// Marks a payload that has been leaked since it has a static lifetime
8686
const STATIC_TAG: u8 = 0b_10;
8787
// The payload is stored inline
@@ -107,21 +107,22 @@ impl RcStr {
107107
#[inline(never)]
108108
pub fn as_str(&self) -> &str {
109109
match self.location() {
110-
DYNAMIC_LOCATION => self.dynamic_as_str(),
110+
PREHASHED_STRING_LOCATION => self.prehashed_string_as_str(),
111111
INLINE_LOCATION => self.inline_as_str(),
112112
_ => unsafe { debug_unreachable!() },
113113
}
114114
}
115115

116116
fn inline_as_str(&self) -> &str {
117+
debug_assert!(self.location() == INLINE_LOCATION);
117118
let len = (self.unsafe_data.tag_byte() & LEN_MASK) >> LEN_OFFSET;
118119
let src = self.unsafe_data.data();
119120
unsafe { std::str::from_utf8_unchecked(&src[..(len as usize)]) }
120121
}
121122

122-
// Extract the str reference from a string stored in a dynamic location
123-
fn dynamic_as_str(&self) -> &str {
124-
debug_assert!(self.location() == DYNAMIC_LOCATION);
123+
// Extract the str reference from a string stored in a PrehashedString
124+
fn prehashed_string_as_str(&self) -> &str {
125+
debug_assert!(self.location() == PREHASHED_STRING_LOCATION);
125126
unsafe { dynamic::deref_from(self.unsafe_data).value.as_str() }
126127
}
127128

@@ -143,7 +144,7 @@ impl RcStr {
143144
}
144145
}
145146
INLINE_TAG => self.inline_as_str().to_string(),
146-
STATIC_TAG => self.dynamic_as_str().to_string(),
147+
STATIC_TAG => self.prehashed_string_as_str().to_string(),
147148
_ => unsafe { debug_unreachable!() },
148149
}
149150
}
@@ -278,6 +279,8 @@ impl Clone for RcStr {
278279
#[inline(always)]
279280
fn clone(&self) -> Self {
280281
let alias = self.unsafe_data;
282+
// We only need to increment the ref count for DYNAMIC_TAG values
283+
// For STATIC_TAG and INLINE_TAG we can just copy the value.
281284
if alias.tag_byte() & TAG_MASK == DYNAMIC_TAG {
282285
unsafe {
283286
let arc = dynamic::restore_arc(alias);
@@ -305,7 +308,7 @@ impl PartialEq for RcStr {
305308
}
306309
// They can still be equal if they are both stored on the heap
307310
match (self.location(), other.location()) {
308-
(DYNAMIC_LOCATION, DYNAMIC_LOCATION) => {
311+
(PREHASHED_STRING_LOCATION, PREHASHED_STRING_LOCATION) => {
309312
let l = unsafe { deref_from(self.unsafe_data) };
310313
let r = unsafe { deref_from(other.unsafe_data) };
311314
l.hash == r.hash && l.value == r.value
@@ -334,7 +337,7 @@ impl Ord for RcStr {
334337
impl Hash for RcStr {
335338
fn hash<H: Hasher>(&self, state: &mut H) {
336339
match self.location() {
337-
DYNAMIC_LOCATION => {
340+
PREHASHED_STRING_LOCATION => {
338341
let l = unsafe { deref_from(self.unsafe_data) };
339342
state.write_u64(l.hash);
340343
state.write_u8(0xff);
@@ -362,8 +365,15 @@ impl<'de> Deserialize<'de> for RcStr {
362365

363366
impl Drop for RcStr {
364367
fn drop(&mut self) {
365-
if self.tag() == DYNAMIC_TAG {
366-
unsafe { drop(dynamic::restore_arc(self.unsafe_data)) }
368+
match self.tag() {
369+
DYNAMIC_TAG => unsafe { drop(dynamic::restore_arc(self.unsafe_data)) },
370+
STATIC_TAG => {
371+
// do nothing, these are never deallocated
372+
}
373+
INLINE_TAG => {
374+
// do nothing, these payloads need no drop logic
375+
}
376+
_ => unsafe { debug_unreachable!() },
367377
}
368378
}
369379
}

0 commit comments

Comments
 (0)