diff --git a/crates/oxc_data_structures/Cargo.toml b/crates/oxc_data_structures/Cargo.toml index 4c07418e518b2..a627f093965ca 100644 --- a/crates/oxc_data_structures/Cargo.toml +++ b/crates/oxc_data_structures/Cargo.toml @@ -29,6 +29,6 @@ default = [] all = ["assert_unchecked", "code_buffer", "inline_string", "rope", "stack"] assert_unchecked = [] code_buffer = ["assert_unchecked"] -inline_string = [] +inline_string = ["assert_unchecked"] rope = ["dep:ropey"] stack = ["assert_unchecked"] diff --git a/crates/oxc_data_structures/src/inline_string.rs b/crates/oxc_data_structures/src/inline_string.rs index 1b08e823efcc9..a1745996fe194 100644 --- a/crates/oxc_data_structures/src/inline_string.rs +++ b/crates/oxc_data_structures/src/inline_string.rs @@ -5,6 +5,8 @@ use std::{ ops::{Add, AddAssign, Deref}, }; +use crate::assert_unchecked; + /// Short inline string. /// /// `CAPACITY` determines the maximum length of the string. @@ -26,6 +28,7 @@ use std::{ /// * `FixedSizeString<7, u8>` = 8 bytes /// * `FixedSizeString<8, usize>` = 16 bytes (on 64-bit platforms) /// * `FixedSizeString<12, u32>` = 16 bytes +#[derive(Clone, Copy)] #[repr(C)] pub struct InlineString { bytes: [u8; CAPACITY], @@ -118,9 +121,10 @@ impl InlineString { #[inline] pub fn as_str(&self) -> &str { // SAFETY: If safety conditions of `push_unchecked` have been upheld, - // slice cannot be out of bounds, and contents of that slice is a valid UTF-8 string + // `self.len <= CAPACITY`, and contents of slice of `bytes` is a valid UTF-8 string unsafe { - let slice = self.bytes.get_unchecked(..self.len.to_usize()); + assert_unchecked!(self.len.to_usize() <= CAPACITY); + let slice = &self.bytes[..self.len.to_usize()]; std::str::from_utf8_unchecked(slice) } }