-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
321 additions
and
296 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::{borrow::Borrow, ptr::NonNull}; | ||
|
||
/// Wrapper for an interned str pointer, required to | ||
/// quickly check using a hash if a string is inside an [`Interner`]. | ||
/// | ||
/// # Safety | ||
/// | ||
/// This struct could cause Undefined Behaviour on: | ||
/// - Use without ensuring the referenced memory is still allocated. | ||
/// - Construction of an [`InternedStr`] from an invalid [`NonNull<str>`]. | ||
/// | ||
/// In general, this should not be used outside of an [`Interner`]. | ||
#[derive(Debug, Clone)] | ||
pub(super) struct InternedStr { | ||
ptr: NonNull<str>, | ||
} | ||
|
||
impl InternedStr { | ||
/// Create a new interned string from the given `str`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Not maintaining the invariants specified on the struct definition | ||
/// could cause Undefined Behaviour. | ||
#[inline] | ||
pub(super) unsafe fn new(ptr: NonNull<str>) -> Self { | ||
Self { ptr } | ||
} | ||
|
||
/// Returns a shared reference to the underlying string. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Not maintaining the invariants specified on the struct definition | ||
/// could cause Undefined Behaviour. | ||
#[inline] | ||
pub(super) unsafe fn as_str(&self) -> &str { | ||
// SAFETY: The user must verify the invariants | ||
// specified on the struct definition. | ||
unsafe { self.ptr.as_ref() } | ||
} | ||
} | ||
|
||
impl std::hash::Hash for InternedStr { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
// SAFETY: The user must verify the invariants | ||
// specified in the struct definition. | ||
unsafe { | ||
self.as_str().hash(state); | ||
} | ||
} | ||
} | ||
|
||
impl Eq for InternedStr {} | ||
|
||
impl PartialEq for InternedStr { | ||
fn eq(&self, other: &Self) -> bool { | ||
// SAFETY: The user must verify the invariants | ||
// specified in the struct definition. | ||
unsafe { self.as_str() == other.as_str() } | ||
} | ||
} | ||
|
||
impl Borrow<str> for InternedStr { | ||
fn borrow(&self) -> &str { | ||
// SAFETY: The user must verify the invariants | ||
// specified in the struct definition. | ||
unsafe { self.as_str() } | ||
} | ||
} |
Oops, something went wrong.