Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
royaltm committed Jun 20, 2024
1 parent a1b1175 commit 474f2b9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/backend/bucket/fixed_str.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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]) },
))
}
}
3 changes: 2 additions & 1 deletion src/backend/bucket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InternedStr> {
self.try_reserve_head(string.len())?;
Ok(self.head
Ok(self
.head
.push_str(string)
.expect("encountered invalid head capacity (2)"))
}
Expand Down
4 changes: 2 additions & 2 deletions src/backend/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
}
}
Expand Down
21 changes: 13 additions & 8 deletions src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<<B as Backend>::Symbol> {
pub fn try_get_or_intern_static(
&mut self,
string: &'static str,
) -> Result<<B as Backend>::Symbol> {
self.try_get_or_intern_using(string, B::try_intern_static)
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 474f2b9

Please sign in to comment.