Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize Rust formatting #59

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 3 additions & 67 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,67 +1,3 @@
max_width = 90 # changed
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
format_code_in_doc_comments = false
comment_width = 80
normalize_comments = true # changed
normalize_doc_attributes = false
# license_template_path = "LICENSE_TEMPLATE" # changed
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Vertical" # changed
imports_granularity="Crate" # changed
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = false # changed
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = true # changed
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = false # changed
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2018" # changed
version = "One"
merge_derives = true
use_try_shorthand = true # changed
use_field_init_shorthand = true # changed
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
unstable_features = true # changed
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Always"
report_fixme = "Always"
ignore = []

# Below are `rustfmt` internal settings
#
# emit_mode = "Files"
# make_backup = false
imports_granularity = "Crate"
imports_layout = "HorizontalVertical"
edition = "2021"
21 changes: 7 additions & 14 deletions benches/setup.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use string_interner::{
backend::{
Backend,
BucketBackend,
BufferBackend,
SimpleBackend,
StringBackend,
},
backend::{Backend, BucketBackend, BufferBackend, SimpleBackend, StringBackend},
DefaultSymbol,
StringInterner,
};

/// Alphabet containing all characters that may be put into a benchmark string.
const ALPHABET: [u8; 64] = [
b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n',
b'o', b'p', b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'A', b'B',
b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P',
b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'0', b'1', b'2', b'3',
b'4', b'5', b'6', b'7', b'8', b'9', b'_', b'-',
b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p',
b'q', b'r', b's', b't', b'u', b'v', b'w', b'x', b'y', b'z', b'A', b'B', b'C', b'D', b'E', b'F',
b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T', b'U', b'V',
b'W', b'X', b'Y', b'Z', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'_', b'-',
];

/// A word builder for benchmark purposes.
Expand All @@ -39,10 +32,10 @@ impl WordBuilder {
'l: for index in &mut self.indices {
if *index == (64 - 1) {
*index = 0;
continue 'l
continue 'l;
}
*index += 1;
return Some(&self.indices[..])
return Some(&self.indices[..]);
}
None
}
Expand Down
6 changes: 2 additions & 4 deletions src/backend/bucket/fixed_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ impl FixedString {
pub fn push_str(&mut self, string: &str) -> Option<InternedStr> {
let len = self.len();
if self.capacity() < len + string.len() {
return None
return None;
}
self.contents.push_str(string);
debug_assert_eq!(self.contents.len(), len + string.len());
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..len + string.len()],
)
core::str::from_utf8_unchecked(&self.contents.as_bytes()[len..len + string.len()])
},
))
}
Expand Down
18 changes: 3 additions & 15 deletions src/backend/bucket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@
mod fixed_str;
mod interned_str;

use self::{
fixed_str::FixedString,
interned_str::InternedStr,
};
use self::{fixed_str::FixedString, interned_str::InternedStr};
use super::Backend;
use crate::{
compat::Vec,
symbol::expect_valid_symbol,
DefaultSymbol,
Symbol,
};
use crate::{compat::Vec, symbol::expect_valid_symbol, DefaultSymbol, Symbol};
#[cfg(not(feature = "std"))]
use alloc::string::String;
use core::{
iter::Enumerate,
marker::PhantomData,
slice,
};
use core::{iter::Enumerate, marker::PhantomData, slice};
/// An interner backend that reduces memory allocations by using string buckets.
///
/// # Note
Expand Down
38 changes: 13 additions & 25 deletions src/backend/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
#![cfg(feature = "backends")]

use super::Backend;
use crate::{
compat::Vec,
symbol::expect_valid_symbol,
DefaultSymbol,
Symbol,
};
use core::{
marker::PhantomData,
mem,
str,
};
use crate::{compat::Vec, symbol::expect_valid_symbol, DefaultSymbol, Symbol};
use core::{marker::PhantomData, mem, str};

/// An interner backend that appends all interned string information in a single buffer.
///
Expand Down Expand Up @@ -207,7 +198,7 @@ fn encode_var_usize(buffer: &mut Vec<u8>, mut value: usize) -> usize {
if value <= 0x7F {
// Shortcut the common case for low value.
buffer.push(value as u8);
return 1
return 1;
}
let mut len_chunks = 0;
loop {
Expand All @@ -217,7 +208,7 @@ fn encode_var_usize(buffer: &mut Vec<u8>, mut value: usize) -> usize {
buffer.push(chunk);
len_chunks += 1;
if value == 0 {
break
break;
}
}
len_chunks
Expand All @@ -236,7 +227,7 @@ fn encode_var_usize(buffer: &mut Vec<u8>, mut value: usize) -> usize {
unsafe fn decode_var_usize_unchecked(buffer: &[u8]) -> (usize, usize) {
let first = unsafe { *buffer.get_unchecked(0) };
if first <= 0x7F_u8 {
return (first as usize, 1)
return (first as usize, 1);
}
let mut result: usize = 0;
let mut i = 0;
Expand All @@ -245,7 +236,7 @@ unsafe fn decode_var_usize_unchecked(buffer: &[u8]) -> (usize, usize) {
let shifted = ((byte & 0x7F_u8) as usize) << ((i * 7) as u32);
result += shifted;
if (byte & 0x80) == 0 {
break
break;
}
i += 1;
}
Expand All @@ -259,7 +250,7 @@ unsafe fn decode_var_usize_unchecked(buffer: &[u8]) -> (usize, usize) {
fn decode_var_usize(buffer: &[u8]) -> Option<(usize, usize)> {
if !buffer.is_empty() && buffer[0] <= 0x7F_u8 {
// Shortcut the common case for low values.
return Some((buffer[0] as usize, 1))
return Some((buffer[0] as usize, 1));
}
let mut result: usize = 0;
let mut i = 0;
Expand All @@ -268,7 +259,7 @@ fn decode_var_usize(buffer: &[u8]) -> Option<(usize, usize)> {
let shifted = ((byte & 0x7F_u8) as usize).checked_shl((i * 7) as u32)?;
result = result.checked_add(shifted)?;
if (byte & 0x80) == 0 {
break
break;
}
i += 1;
}
Expand All @@ -277,10 +268,7 @@ fn decode_var_usize(buffer: &[u8]) -> Option<(usize, usize)> {

#[cfg(test)]
mod tests {
use super::{
decode_var_usize,
encode_var_usize,
};
use super::{decode_var_usize, encode_var_usize};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

Expand Down Expand Up @@ -450,14 +438,14 @@ where

#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.backend.resolve_index_to_str(self.current).and_then(
|(string, next_string_index)| {
self.backend
.resolve_index_to_str(self.current)
.and_then(|(string, next_string_index)| {
let symbol = S::try_from_usize(self.current)?;
self.current = next_string_index;
self.yielded += 1;
Some((symbol, string))
},
)
})
}
}

Expand Down
12 changes: 2 additions & 10 deletions src/backend/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@

use super::Backend;
use crate::{
compat::{
Box,
ToString,
Vec,
},
compat::{Box, ToString, Vec},
symbol::expect_valid_symbol,
DefaultSymbol,
Symbol,
};
use core::{
iter::Enumerate,
marker::PhantomData,
slice,
};
use core::{iter::Enumerate, marker::PhantomData, slice};

/// A simple backend that stores a separate allocation for every interned string.
///
Expand Down
19 changes: 5 additions & 14 deletions src/backend/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@

use super::Backend;
use crate::{
compat::{
String,
Vec,
},
compat::{String, Vec},
symbol::expect_valid_symbol,
DefaultSymbol,
Symbol,
};
use core::{
iter::Enumerate,
marker::PhantomData,
slice,
};
use core::{iter::Enumerate, marker::PhantomData, slice};

/// An interner backend that accumulates all interned string contents into one string.
///
Expand Down Expand Up @@ -66,11 +59,11 @@ where
{
fn eq(&self, other: &Self) -> bool {
if self.ends.len() != other.ends.len() {
return false
return false;
}
for ((_, lhs), (_, rhs)) in self.into_iter().zip(other) {
if lhs != rhs {
return false
return false;
}
}
true
Expand Down Expand Up @@ -117,9 +110,7 @@ where
// method.
// - The spans we use for `(start..end]` ranges are always
// constructed in accordance to valid utf8 byte ranges.
unsafe {
core::str::from_utf8_unchecked(&self.buffer.as_bytes()[span.from..span.to])
}
unsafe { core::str::from_utf8_unchecked(&self.buffer.as_bytes()[span.from..span.to]) }
}

/// Returns the span for the given symbol if any.
Expand Down
5 changes: 1 addition & 4 deletions src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@

pub use ::hashbrown::{
hash_map,
hash_map::{
DefaultHashBuilder,
HashMap,
},
hash_map::{DefaultHashBuilder, HashMap},
};

cfg_if! {
if #[cfg(feature = "std")] {
pub use ::std::{
vec,

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `vec`

Check failure on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Test Memory Consumption (ubuntu-latest)

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Miri

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Miri

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Test Memory Consumption (macos-latest)

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Test Memory Consumption (windows-latest)

unused import: `vec`

Check warning on line 13 in src/compat.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

unused import: `vec`
vec::Vec,
string::{String, ToString},
boxed::Box,
Expand Down
21 changes: 4 additions & 17 deletions src/interner.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
use crate::{
backend::Backend,
compat::{
DefaultHashBuilder,
HashMap,
},
compat::{DefaultHashBuilder, HashMap},
DefaultBackend,
DefaultSymbol,
Symbol,
};
use core::{
fmt,
fmt::{
Debug,
Formatter,
},
hash::{
BuildHasher,
Hash,
Hasher,
},
fmt::{Debug, Formatter},
hash::{BuildHasher, Hash, Hasher},
iter::FromIterator,
};

Expand Down Expand Up @@ -271,10 +261,7 @@ where
/// If the interner already interns the maximum number of strings possible
/// by the chosen symbol type.
#[inline]
pub fn get_or_intern_static(
&mut self,
string: &'static str,
) -> <B as Backend>::Symbol {
pub fn get_or_intern_static(&mut self, string: &'static str) -> <B as Backend>::Symbol {
self.get_or_intern_using(string, B::intern_static)
}

Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,5 @@ pub use self::{
backend::DefaultBackend,
compat::DefaultHashBuilder,
interner::StringInterner,
symbol::{
DefaultSymbol,
Symbol,
},
symbol::{DefaultSymbol, Symbol},
};
Loading
Loading