Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/oxc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
8 changes: 6 additions & 2 deletions crates/oxc_data_structures/src/inline_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<const CAPACITY: usize, Len: UnsignedInt> {
bytes: [u8; CAPACITY],
Expand Down Expand Up @@ -118,9 +121,10 @@ impl<const CAPACITY: usize, Len: UnsignedInt> InlineString<CAPACITY, Len> {
#[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)
}
}
Expand Down
Loading