mirror of https://github.com/boa-dev/boa.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.3 KiB
81 lines
2.3 KiB
2 years ago
|
use core::{hash::Hash, ptr::NonNull};
|
||
2 years ago
|
|
||
|
/// Wrapper for an interned str pointer, required to
|
||
|
/// quickly check using a hash if a string is inside an [`Interner`][`super::Interner`].
|
||
|
///
|
||
|
/// # Safety
|
||
|
///
|
||
|
/// This struct could cause Undefined Behaviour on:
|
||
|
/// - Use without ensuring the referenced memory is still allocated.
|
||
2 years ago
|
/// - Construction of an [`InternedStr`] from an invalid [`NonNull<Char>`] pointer.
|
||
|
/// - Construction of an [`InternedStr`] from a [`NonNull<Char>`] pointer
|
||
|
/// without checking if the pointed memory of the [`NonNull<Char>`] outlives
|
||
|
/// the [`InternedStr`].
|
||
2 years ago
|
///
|
||
|
/// In general, this should not be used outside of an [`Interner`][`super::Interner`].
|
||
2 years ago
|
#[derive(Debug)]
|
||
|
pub(super) struct InternedStr<Char> {
|
||
|
ptr: NonNull<[Char]>,
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
impl<Char> InternedStr<Char> {
|
||
|
/// Create a new interned string from the given `*const u8` pointer,
|
||
|
/// length and encoding kind
|
||
2 years ago
|
///
|
||
|
/// # Safety
|
||
|
///
|
||
|
/// Not maintaining the invariants specified on the struct definition
|
||
|
/// could cause Undefined Behaviour.
|
||
2 years ago
|
pub(super) const unsafe fn new(ptr: NonNull<[Char]>) -> Self {
|
||
2 years ago
|
Self { ptr }
|
||
|
}
|
||
|
|
||
|
/// Returns a shared reference to the underlying string.
|
||
|
///
|
||
|
/// # Safety
|
||
|
///
|
||
|
/// Not maintaining the invariants specified on the struct definition
|
||
|
/// could cause Undefined Behaviour.
|
||
2 years ago
|
pub(super) unsafe fn as_ref(&self) -> &[Char] {
|
||
|
// SAFETY:
|
||
|
// The caller must ensure `ptr` is still valid throughout the
|
||
|
// lifetime of `self`.
|
||
2 years ago
|
unsafe { self.ptr.as_ref() }
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl<Char> Clone for InternedStr<Char> {
|
||
|
fn clone(&self) -> Self {
|
||
1 year ago
|
*self
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl<Char> Copy for InternedStr<Char> {}
|
||
2 years ago
|
|
||
2 years ago
|
impl<Char> Eq for InternedStr<Char> where Char: Eq {}
|
||
|
|
||
|
impl<Char> PartialEq for InternedStr<Char>
|
||
|
where
|
||
|
Char: PartialEq,
|
||
|
{
|
||
2 years ago
|
fn eq(&self, other: &Self) -> bool {
|
||
|
// SAFETY: The caller must verify the invariants
|
||
|
// specified in the struct definition.
|
||
2 years ago
|
unsafe { self.as_ref() == other.as_ref() }
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl<Char> Hash for InternedStr<Char>
|
||
|
where
|
||
|
Char: Hash,
|
||
|
{
|
||
2 years ago
|
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
|
||
2 years ago
|
// SAFETY:
|
||
|
// The caller must ensure `ptr` is still valid throughout the
|
||
|
// lifetime of `self`.
|
||
|
unsafe {
|
||
|
self.as_ref().hash(state);
|
||
|
}
|
||
2 years ago
|
}
|
||
|
}
|