From 5aaf65a0e893b2104f88e0a99560e902d9a0b9aa Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Sun, 12 Jul 2020 16:05:54 +0200 Subject: [PATCH] minor improvements, add some inline annotations --- src/iter.rs | 1 + src/lib.rs | 5 +++-- src/symbol.rs | 14 ++++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/iter.rs b/src/iter.rs index 2136865..a0c6833 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -142,6 +142,7 @@ where { type Item = (S, String); + #[inline] fn next(&mut self) -> Option { self.iter.next().map(|(num, boxed_str)| { (S::from_usize(num), Pin::into_inner(boxed_str).into_string()) diff --git a/src/lib.rs b/src/lib.rs index 5d18d54..1ef1fa9 100644 --- a/src/lib.rs +++ b/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. @@ -123,12 +122,14 @@ impl PinnedStr { } impl Hash for PinnedStr { + #[inline] fn hash(&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() } @@ -401,7 +402,7 @@ where } } -impl core::iter::Extend for StringInterner +impl Extend for StringInterner where S: Symbol, T: Into + AsRef, diff --git a/src/symbol.rs b/src/symbol.rs index fe9db53..d98a854 100644 --- a/src/symbol.rs +++ b/src/symbol.rs @@ -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; @@ -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 }