Skip to content

Commit

Permalink
minor improvements, add some inline annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Jul 12, 2020
1 parent a5be761 commit 5aaf65a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/iter.rs
Expand Up @@ -142,6 +142,7 @@ where
{
type Item = (S, String);

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(num, boxed_str)| {
(S::from_usize(num), Pin::into_inner(boxed_str).into_string())
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
@@ -1,7 +1,6 @@
#![doc(html_root_url = "https://docs.rs/crate/string-interner/0.8.0")]
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(missing_docs)]
#![allow(unused_attributes)] // TODO: remove

//! Caches strings efficiently, with minimal memory footprint and associates them with unique symbols.
//! These symbols allow constant time comparisons and look-ups to the underlying interned strings.
Expand Down Expand Up @@ -123,12 +122,14 @@ impl PinnedStr {
}

impl Hash for PinnedStr {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state)
}
}

impl PartialEq for PinnedStr {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_str() == other.as_str()
}
Expand Down Expand Up @@ -401,7 +402,7 @@ where
}
}

impl<T, S> core::iter::Extend<T> for StringInterner<S>
impl<T, S> Extend<T> for StringInterner<S>
where
S: Symbol,
T: Into<String> + AsRef<str>,
Expand Down
14 changes: 8 additions & 6 deletions src/symbol.rs
Expand Up @@ -14,7 +14,7 @@ pub trait Symbol: Copy + Ord + Eq {
/// # Note
///
/// Implementations panic if the operation cannot succeed.
fn from_usize(val: usize) -> Self;
fn from_usize(index: usize) -> Self;

/// Returns the `usize` representation of `self`.
fn to_usize(self) -> usize;
Expand All @@ -36,19 +36,21 @@ impl Symbol for DefaultSymbol {
/// # Panics
///
/// If the given `usize` is greater than `u32::MAX - 1`.
fn from_usize(val: usize) -> Self {
#[inline]
fn from_usize(index: usize) -> Self {
assert!(
val < core::u32::MAX as usize,
index < core::u32::MAX as usize,
"{} is out of bounds for the default symbol",
val
index
);
Self(
NonZeroU32::new((val + 1) as u32)
NonZeroU32::new((index as u32) + 1)
// Due to the assert above we can assume that this always succeeds.
.unwrap_or_else(|| unsafe { core::hint::unreachable_unchecked() }),
.unwrap_or_else(|| unsafe { ::core::hint::unreachable_unchecked() }),
)
}

#[inline]
fn to_usize(self) -> usize {
(self.0.get() as usize) - 1
}
Expand Down

0 comments on commit 5aaf65a

Please sign in to comment.