diff --git a/src/backend/bucket/fixed_str.rs b/src/backend/bucket/fixed_str.rs index b06c3c3..1b15925 100644 --- a/src/backend/bucket/fixed_str.rs +++ b/src/backend/bucket/fixed_str.rs @@ -1,7 +1,7 @@ use super::InternedStr; +use crate::Result; #[cfg(not(feature = "std"))] use alloc::string::String; -use crate::Result; #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct FixedString { @@ -56,9 +56,7 @@ impl FixedString { Some(InternedStr::new( // SAFETY: We convert from bytes to utf8 from which we know through the // input string that they must represent valid utf8. - unsafe { - core::str::from_utf8_unchecked(&self.contents.as_bytes()[len..new_len]) - }, + unsafe { core::str::from_utf8_unchecked(&self.contents.as_bytes()[len..new_len]) }, )) } } diff --git a/src/backend/bucket/mod.rs b/src/backend/bucket/mod.rs index b823368..114f061 100644 --- a/src/backend/bucket/mod.rs +++ b/src/backend/bucket/mod.rs @@ -186,7 +186,8 @@ where /// Interns a new string into the backend and returns a reference to it. unsafe fn try_alloc(&mut self, string: &str) -> Result { self.try_reserve_head(string.len())?; - Ok(self.head + Ok(self + .head .push_str(string) .expect("encountered invalid head capacity (2)")) } diff --git a/src/backend/buffer.rs b/src/backend/buffer.rs index 86c0879..ffd761a 100644 --- a/src/backend/buffer.rs +++ b/src/backend/buffer.rs @@ -212,7 +212,7 @@ where fn calculate_var7_size(value: usize) -> usize { // number of bits to encode // value = 0 would give 0 bits, hence: |1, could be anything up to |0x7F as well - let bits = usize::BITS - (value|1).leading_zeros(); + let bits = usize::BITS - (value | 1).leading_zeros(); // (bits to encode / 7).ceil() ((bits + 6) / 7) as usize } @@ -325,7 +325,7 @@ fn decode_var_usize_cold(buffer: &[u8]) -> Option<(usize, usize)> { #[cfg(test)] mod tests { - use super::{decode_var_usize, encode_var_usize, calculate_var7_size}; + use super::{calculate_var7_size, decode_var_usize, encode_var_usize}; #[cfg(not(feature = "std"))] use alloc::vec::Vec; diff --git a/src/error.rs b/src/error.rs index fc83915..ad72656 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,14 +7,14 @@ pub enum Error { /// The interner already interns the maximum number of strings possible by the chosen symbol type. OutOfSymbols, /// An operation could not be completed, because it failed to allocate enough memory. - OutOfMemory + OutOfMemory, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(match self { Error::OutOfSymbols => "no more symbols available", - Error::OutOfMemory => "out of memory" + Error::OutOfMemory => "out of memory", }) } } diff --git a/src/interner.rs b/src/interner.rs index 423a8da..11dfcfe 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -167,13 +167,15 @@ where /// string deduplication. /// It __does not__ reserve capacity in the backend that stores strings. pub fn try_reserve(&mut self, additional: usize) -> Result<()> { - self.dedup.raw_table_mut().try_reserve(additional, |(symbol, ())| { - // SAFETY: This is safe because we only operate on symbols that - // we receive from our backend making them valid. - let string = unsafe { self.backend.resolve_unchecked(*symbol) }; - make_hash(&self.hasher, string) - }) - .map_err(From::from) + self.dedup + .raw_table_mut() + .try_reserve(additional, |(symbol, ())| { + // SAFETY: This is safe because we only operate on symbols that + // we receive from our backend making them valid. + let string = unsafe { self.backend.resolve_unchecked(*symbol) }; + make_hash(&self.hasher, string) + }) + .map_err(From::from) } /// Returns the symbol for the given string if any. @@ -296,7 +298,10 @@ where /// If the interner already interns the maximum number of strings possible /// by the chosen symbol type or when running out of heap memory. #[inline] - pub fn try_get_or_intern_static(&mut self, string: &'static str) -> Result<::Symbol> { + pub fn try_get_or_intern_static( + &mut self, + string: &'static str, + ) -> Result<::Symbol> { self.try_get_or_intern_using(string, B::try_intern_static) } diff --git a/src/lib.rs b/src/lib.rs index 79269bf..a400adb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,9 +128,9 @@ extern crate std as alloc; mod serde_impl; pub mod backend; +mod error; mod interner; pub mod symbol; -mod error; /// A convenience [`StringInterner`] type based on the [`DefaultBackend`]. #[cfg(feature = "backends")]