diff --git a/matcher/src/chars.rs b/matcher/src/chars.rs index d13a246..d07a2cc 100644 --- a/matcher/src/chars.rs +++ b/matcher/src/chars.rs @@ -27,8 +27,8 @@ pub(crate) trait Char: Copy + Eq + Ord + fmt::Display { pub(crate) struct AsciiChar(pub u8); impl AsciiChar { - pub fn cast(bytes: &[u8]) -> &[AsciiChar] { - unsafe { &*(bytes as *const [u8] as *const [AsciiChar]) } + pub fn cast(bytes: &[u8]) -> &[Self] { + unsafe { &*(bytes as *const [u8] as *const [Self]) } } } @@ -40,7 +40,7 @@ impl fmt::Display for AsciiChar { impl PartialEq for char { fn eq(&self, other: &AsciiChar) -> bool { - other.0 as char == *self + other.0 as Self == *self } } @@ -111,7 +111,7 @@ impl Char for char { fn char_class_and_normalize(mut self, config: &Config) -> (Self, CharClass) { if self.is_ascii() { let (c, class) = AsciiChar(self as u8).char_class_and_normalize(config); - return (c.0 as char, class); + return (c.0 as Self, class); } let char_class = char_class_non_ascii(self); #[cfg(feature = "unicode-casefold")] diff --git a/matcher/src/config.rs b/matcher/src/config.rs index eca7ae3..bdedff9 100644 --- a/matcher/src/config.rs +++ b/matcher/src/config.rs @@ -32,7 +32,7 @@ impl Config { /// The default config for nucleo, implemented as a constant since /// Default::default can not be called in a const context pub const DEFAULT: Self = { - Config { + Self { delimiter_chars: b"/,:;|", bonus_boundary_white: BONUS_BOUNDARY + 2, bonus_boundary_delimiter: BONUS_BOUNDARY + 1, diff --git a/matcher/src/lib.rs b/matcher/src/lib.rs index 3e8874c..2313502 100644 --- a/matcher/src/lib.rs +++ b/matcher/src/lib.rs @@ -158,7 +158,7 @@ pub struct Matcher { // this is just here for convenience not sure if we should implement this impl Clone for Matcher { fn clone(&self) -> Self { - Matcher { + Self { config: self.config.clone(), slab: MatrixSlab::new(), } @@ -175,7 +175,7 @@ impl std::fmt::Debug for Matcher { impl Default for Matcher { fn default() -> Self { - Matcher { + Self { config: Config::DEFAULT, slab: MatrixSlab::new(), } diff --git a/matcher/src/matrix.rs b/matcher/src/matrix.rs index a91ed95..516ba72 100644 --- a/matcher/src/matrix.rs +++ b/matcher/src/matrix.rs @@ -24,7 +24,7 @@ struct MatrixLayout { _phantom: PhantomData, } impl MatrixLayout { - fn new(haystack_len: usize, needle_len: usize) -> MatrixLayout { + fn new(haystack_len: usize, needle_len: usize) -> Self { assert!(haystack_len >= needle_len); assert!(haystack_len <= u32::MAX as usize); let mut layout = Layout::from_size_align(0, 1).unwrap(); @@ -45,7 +45,7 @@ impl MatrixLayout { (layout, score_off) = layout.extend(score_layout).unwrap(); let matrix_off; (layout, matrix_off) = layout.extend(matrix_layout).unwrap(); - MatrixLayout { + Self { haystack_len, needle_len, layout, @@ -151,7 +151,7 @@ impl MatrixSlab { let Some(ptr) = NonNull::new(ptr) else { handle_alloc_error(layout) }; - MatrixSlab(ptr.cast()) + Self(ptr.cast()) } pub(crate) fn alloc( diff --git a/matcher/src/pattern.rs b/matcher/src/pattern.rs index 495feed..a4e0cfa 100644 --- a/matcher/src/pattern.rs +++ b/matcher/src/pattern.rs @@ -101,8 +101,8 @@ impl Atom { normalize: Normalization, kind: AtomKind, escape_whitespace: bool, - ) -> Atom { - Atom::new_inner(needle, case, normalize, kind, escape_whitespace, false) + ) -> Self { + Self::new_inner(needle, case, normalize, kind, escape_whitespace, false) } fn new_inner( @@ -112,7 +112,7 @@ impl Atom { kind: AtomKind, escape_whitespace: bool, append_dollar: bool, - ) -> Atom { + ) -> Self { let mut ignore_case; let mut normalize; #[cfg(feature = "unicode-normalization")] @@ -227,7 +227,7 @@ impl Atom { } Utf32String::Unicode(needle_.into_boxed_slice()) }; - Atom { + Self { kind, needle, negative: false, @@ -239,7 +239,7 @@ impl Atom { /// Parse a pattern atom from a string. Some special trailing and leading /// characters can be used to control the atom kind. See [`AtomKind`] for /// details. - pub fn parse(raw: &str, case: CaseMatching, normalize: Normalization) -> Atom { + pub fn parse(raw: &str, case: CaseMatching, normalize: Normalization) -> Self { let mut atom = raw; let invert = match atom.as_bytes() { [b'!', ..] => { @@ -290,7 +290,7 @@ impl Atom { kind = AtomKind::Substring } - let mut pattern = Atom::new_inner(atom, case, normalize, kind, true, append_dollar); + let mut pattern = Self::new_inner(atom, case, normalize, kind, true, append_dollar); pattern.negative = invert; pattern } @@ -429,27 +429,27 @@ impl Pattern { case_matching: CaseMatching, normalize: Normalization, kind: AtomKind, - ) -> Pattern { + ) -> Self { let atoms = pattern_atoms(pattern) .filter_map(|pat| { let pat = Atom::new(pat, case_matching, normalize, kind, true); (!pat.needle.is_empty()).then_some(pat) }) .collect(); - Pattern { atoms } + Self { atoms } } /// Creates a pattern where each word is matched individually (whitespaces /// can be escaped with `\`). And `$`, `!`, `'` and `^` at word boundaries will /// cause different matching behaviour (see [`AtomKind`]). These can be /// escaped with backslash. - pub fn parse(pattern: &str, case_matching: CaseMatching, normalize: Normalization) -> Pattern { + pub fn parse(pattern: &str, case_matching: CaseMatching, normalize: Normalization) -> Self { let atoms = pattern_atoms(pattern) .filter_map(|pat| { let pat = Atom::parse(pat, case_matching, normalize); (!pat.needle.is_empty()).then_some(pat) }) .collect(); - Pattern { atoms } + Self { atoms } } /// Convenience function to easily match (and sort) a (relatively small) diff --git a/matcher/src/utf32_str.rs b/matcher/src/utf32_str.rs index 664dae7..bb64028 100644 --- a/matcher/src/utf32_str.rs +++ b/matcher/src/utf32_str.rs @@ -105,11 +105,11 @@ impl<'a> Utf32Str<'a> { /// Convenience method to construct a `Utf32Str` from a normal UTF-8 str pub fn new(str: &'a str, buf: &'a mut Vec) -> Self { if has_ascii_graphemes(str) { - Utf32Str::Ascii(str.as_bytes()) + Self::Ascii(str.as_bytes()) } else { buf.clear(); buf.extend(crate::chars::graphemes(str)); - Utf32Str::Unicode(buf) + Self::Unicode(buf) } } @@ -117,8 +117,8 @@ impl<'a> Utf32Str<'a> { #[inline] pub fn len(self) -> usize { match self { - Utf32Str::Unicode(codepoints) => codepoints.len(), - Utf32Str::Ascii(ascii_bytes) => ascii_bytes.len(), + Self::Unicode(codepoints) => codepoints.len(), + Self::Ascii(ascii_bytes) => ascii_bytes.len(), } } @@ -126,15 +126,15 @@ impl<'a> Utf32Str<'a> { #[inline] pub fn is_empty(self) -> bool { match self { - Utf32Str::Unicode(codepoints) => codepoints.is_empty(), - Utf32Str::Ascii(ascii_bytes) => ascii_bytes.is_empty(), + Self::Unicode(codepoints) => codepoints.is_empty(), + Self::Ascii(ascii_bytes) => ascii_bytes.is_empty(), } } /// Creates a slice with a string that contains the characters in /// the specified **character range**. #[inline] - pub fn slice(self, range: impl RangeBounds) -> Utf32Str<'a> { + pub fn slice(self, range: impl RangeBounds) -> Self { let start = match range.start_bound() { Bound::Included(&start) => start, Bound::Excluded(&start) => start + 1, @@ -146,8 +146,8 @@ impl<'a> Utf32Str<'a> { Bound::Unbounded => self.len(), }; match self { - Utf32Str::Ascii(bytes) => Utf32Str::Ascii(&bytes[start..end]), - Utf32Str::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]), + Self::Ascii(bytes) => Self::Ascii(&bytes[start..end]), + Self::Unicode(codepoints) => Self::Unicode(&codepoints[start..end]), } } @@ -155,11 +155,11 @@ impl<'a> Utf32Str<'a> { #[inline] pub(crate) fn leading_white_space(self) -> usize { match self { - Utf32Str::Ascii(bytes) => bytes + Self::Ascii(bytes) => bytes .iter() .position(|b| !b.is_ascii_whitespace()) .unwrap_or(0), - Utf32Str::Unicode(codepoints) => codepoints + Self::Unicode(codepoints) => codepoints .iter() .position(|c| !c.is_whitespace()) .unwrap_or(0), @@ -170,12 +170,12 @@ impl<'a> Utf32Str<'a> { #[inline] pub(crate) fn trailing_white_space(self) -> usize { match self { - Utf32Str::Ascii(bytes) => bytes + Self::Ascii(bytes) => bytes .iter() .rev() .position(|b| !b.is_ascii_whitespace()) .unwrap_or(0), - Utf32Str::Unicode(codepoints) => codepoints + Self::Unicode(codepoints) => codepoints .iter() .rev() .position(|c| !c.is_whitespace()) @@ -186,7 +186,7 @@ impl<'a> Utf32Str<'a> { /// Same as `slice` but accepts a u32 range for convenience since /// those are the indices returned by the matcher. #[inline] - pub fn slice_u32(self, range: impl RangeBounds) -> Utf32Str<'a> { + pub fn slice_u32(self, range: impl RangeBounds) -> Self { let start = match range.start_bound() { Bound::Included(&start) => start as usize, Bound::Excluded(&start) => start as usize + 1, @@ -198,8 +198,8 @@ impl<'a> Utf32Str<'a> { Bound::Unbounded => self.len(), }; match self { - Utf32Str::Ascii(bytes) => Utf32Str::Ascii(&bytes[start..end]), - Utf32Str::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]), + Self::Ascii(bytes) => Self::Ascii(&bytes[start..end]), + Self::Unicode(codepoints) => Self::Unicode(&codepoints[start..end]), } } @@ -215,8 +215,8 @@ impl<'a> Utf32Str<'a> { /// Returns the `n`th character in this string, zero-indexed pub fn get(self, n: u32) -> char { match self { - Utf32Str::Ascii(bytes) => bytes[n as usize] as char, - Utf32Str::Unicode(codepoints) => codepoints[n as usize], + Self::Ascii(bytes) => bytes[n as usize] as char, + Self::Unicode(codepoints) => codepoints[n as usize], } } @@ -225,8 +225,8 @@ impl<'a> Utf32Str<'a> { /// Panics if the string is empty. pub(crate) fn last(self) -> char { match self { - Utf32Str::Ascii(bytes) => bytes[bytes.len() - 1] as char, - Utf32Str::Unicode(codepoints) => codepoints[codepoints.len() - 1], + Self::Ascii(bytes) => bytes[bytes.len() - 1] as char, + Self::Unicode(codepoints) => codepoints[codepoints.len() - 1], } } @@ -235,16 +235,16 @@ impl<'a> Utf32Str<'a> { /// Panics if the string is empty. pub(crate) fn first(self) -> char { match self { - Utf32Str::Ascii(bytes) => bytes[0] as char, - Utf32Str::Unicode(codepoints) => codepoints[0], + Self::Ascii(bytes) => bytes[0] as char, + Self::Unicode(codepoints) => codepoints[0], } } /// Returns an iterator over the characters in this string pub fn chars(self) -> Chars<'a> { match self { - Utf32Str::Ascii(bytes) => Chars::Ascii(bytes.iter()), - Utf32Str::Unicode(codepoints) => Chars::Unicode(codepoints.iter()), + Self::Ascii(bytes) => Chars::Ascii(bytes.iter()), + Self::Unicode(codepoints) => Chars::Unicode(codepoints.iter()), } } } @@ -318,8 +318,8 @@ impl Utf32String { #[inline] pub fn len(&self) -> usize { match self { - Utf32String::Unicode(codepoints) => codepoints.len(), - Utf32String::Ascii(ascii_bytes) => ascii_bytes.len(), + Self::Unicode(codepoints) => codepoints.len(), + Self::Ascii(ascii_bytes) => ascii_bytes.len(), } } @@ -327,8 +327,8 @@ impl Utf32String { #[inline] pub fn is_empty(&self) -> bool { match self { - Utf32String::Unicode(codepoints) => codepoints.is_empty(), - Utf32String::Ascii(ascii_bytes) => ascii_bytes.is_empty(), + Self::Unicode(codepoints) => codepoints.is_empty(), + Self::Ascii(ascii_bytes) => ascii_bytes.is_empty(), } } @@ -347,8 +347,8 @@ impl Utf32String { Bound::Unbounded => self.len(), }; match self { - Utf32String::Ascii(bytes) => Utf32Str::Ascii(&bytes.as_bytes()[start..end]), - Utf32String::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]), + Self::Ascii(bytes) => Utf32Str::Ascii(&bytes.as_bytes()[start..end]), + Self::Unicode(codepoints) => Utf32Str::Unicode(&codepoints[start..end]), } } @@ -367,10 +367,8 @@ impl Utf32String { Bound::Unbounded => self.len() as u32, }; match self { - Utf32String::Ascii(bytes) => { - Utf32Str::Ascii(&bytes.as_bytes()[start as usize..end as usize]) - } - Utf32String::Unicode(codepoints) => { + Self::Ascii(bytes) => Utf32Str::Ascii(&bytes.as_bytes()[start as usize..end as usize]), + Self::Unicode(codepoints) => { Utf32Str::Unicode(&codepoints[start as usize..end as usize]) } } diff --git a/src/boxcar.rs b/src/boxcar.rs index 9b48809..5c6031d 100644 --- a/src/boxcar.rs +++ b/src/boxcar.rs @@ -50,7 +50,7 @@ pub(crate) struct Vec { impl Vec { /// Constructs a new, empty `Vec` with the specified capacity and matcher columns. - pub fn with_capacity(capacity: u32, columns: u32) -> Vec { + pub fn with_capacity(capacity: u32, columns: u32) -> Self { assert_ne!(columns, 0, "there must be atleast one matcher column"); let init = match capacity { 0 => 0, @@ -65,7 +65,7 @@ impl Vec { *bucket = unsafe { Bucket::alloc(len, columns) }; } - Vec { + Self { buckets: buckets.map(Bucket::new), inflight: AtomicU64::new(0), columns, @@ -147,7 +147,7 @@ impl Vec { // eagerly allocate the next bucket if we are close to the end of this one if index == (location.bucket_len - (location.bucket_len >> 3)) { if let Some(next_bucket) = self.buckets.get(location.bucket as usize + 1) { - Vec::get_or_alloc(next_bucket, location.bucket_len << 1, self.columns); + Self::get_or_alloc(next_bucket, location.bucket_len << 1, self.columns); } } @@ -157,7 +157,7 @@ impl Vec { // the bucket has not been allocated yet if entries.is_null() { - entries = Vec::get_or_alloc(bucket, location.bucket_len, self.columns); + entries = Self::get_or_alloc(bucket, location.bucket_len, self.columns); } unsafe { @@ -218,14 +218,14 @@ impl Vec { { // This might be the last bucket, hence the check if let Some(next_bucket) = self.buckets.get(end_location.bucket as usize + 1) { - Vec::get_or_alloc(next_bucket, end_location.bucket_len << 1, self.columns); + Self::get_or_alloc(next_bucket, end_location.bucket_len << 1, self.columns); } } let mut bucket = unsafe { self.buckets.get_unchecked(start_location.bucket as usize) }; let mut entries = bucket.entries.load(Ordering::Acquire); if entries.is_null() { - entries = Vec::get_or_alloc( + entries = Self::get_or_alloc( bucket, Location::bucket_len(start_location.bucket), self.columns, @@ -249,7 +249,7 @@ impl Vec { entries = bucket.entries.load(Ordering::Acquire); if entries.is_null() { - entries = Vec::get_or_alloc( + entries = Self::get_or_alloc( bucket, Location::bucket_len(location.bucket), self.columns, @@ -531,7 +531,7 @@ impl Bucket { let layout = Entry::::layout(cols); let arr_layout = Self::layout(len, layout); for i in 0..len { - let entry = Bucket::get(entries, i, cols); + let entry = Self::get(entries, i, cols); if *(*entry).active.get_mut() { ptr::drop_in_place((*(*entry).slot.get()).as_mut_ptr()); for matcher_col in Entry::matcher_cols_raw(entry, cols) { @@ -548,8 +548,8 @@ impl Bucket { ptr.add(layout.size() * idx as usize) as *mut Entry } - fn new(entries: *mut Entry) -> Bucket { - Bucket { + fn new(entries: *mut Entry) -> Self { + Self { entries: AtomicPtr::new(entries), } } @@ -573,7 +573,7 @@ impl Entry { } unsafe fn matcher_cols_raw<'a>( - ptr: *mut Entry, + ptr: *mut Self, cols: u32, ) -> &'a [UnsafeCell>] { // this whole thing looks weird. The reason we do this is that @@ -585,7 +585,7 @@ impl Entry { slice::from_raw_parts(ptr, cols as usize) } - unsafe fn matcher_cols_mut<'a>(ptr: *mut Entry, cols: u32) -> &'a mut [Utf32String] { + unsafe fn matcher_cols_mut<'a>(ptr: *mut Self, cols: u32) -> &'a mut [Utf32String] { // this whole thing looks weird. The reason we do this is that // we must make sure the pointer retains its provenance which may (or may not?) // be lost if we used tail.as_ptr() @@ -597,7 +597,7 @@ impl Entry { // # Safety // // Value must be initialized. - unsafe fn read<'a>(ptr: *mut Entry, cols: u32) -> Item<'a, T> { + unsafe fn read<'a>(ptr: *mut Self, cols: u32) -> Item<'a, T> { // this whole thing looks weird. The reason we do this is that // we must make sure the pointer retains its provenance which may (or may not?) // be lost if we used tail.as_ptr() @@ -629,14 +629,14 @@ const SKIP: u32 = 32; const SKIP_BUCKET: u32 = (u32::BITS - SKIP.leading_zeros()) - 1; impl Location { - fn of(index: u32) -> Location { + fn of(index: u32) -> Self { let skipped = index.checked_add(SKIP).expect("exceeded maximum length"); let bucket = u32::BITS - skipped.leading_zeros(); let bucket = bucket - (SKIP_BUCKET + 1); - let bucket_len = Location::bucket_len(bucket); + let bucket_len = Self::bucket_len(bucket); let entry = skipped ^ bucket_len; - Location { + Self { bucket, bucket_len, entry, @@ -780,7 +780,7 @@ mod tests { fn extend_over_max_capacity() { let vec = Vec::::with_capacity(1, 1); let count = MAX_ENTRIES as usize + 2; - let iter = std::iter::repeat(0).take(count); + let iter = std::iter::repeat_n(0, count); assert!(std::panic::catch_unwind(|| vec.extend(iter, |_, _| {})).is_err()); } } diff --git a/src/lib.rs b/src/lib.rs index b896d07..0ef2496 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,7 +63,7 @@ pub struct Injector { impl Clone for Injector { fn clone(&self) -> Self { - Injector { + Self { items: self.items.clone(), notify: self.notify.clone(), } @@ -258,17 +258,17 @@ enum State { impl State { fn matcher_item_refs(self) -> usize { match self { - State::Cleared => 1, - State::Init | State::Fresh => 2, + Self::Cleared => 1, + Self::Init | Self::Fresh => 2, } } fn canceled(self) -> bool { - self != State::Fresh + self != Self::Fresh } fn cleared(self) -> bool { - self != State::Fresh + self != Self::Fresh } } diff --git a/src/pattern.rs b/src/pattern.rs index 816b0a3..456bb5a 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -56,7 +56,7 @@ impl MultiPattern { .0 .atoms .last() - .map_or(true, |last| !last.negative) + .is_none_or(|last| !last.negative) { self.cols[column].1 = Status::Update; } else { diff --git a/src/worker.rs b/src/worker.rs index 97164d6..7b2ccb5 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -64,7 +64,7 @@ impl Worker { let matchers = (0..worker_threads) .map(|_| UnsafeCell::new(nucleo_matcher::Matcher::new(config.clone()))) .collect(); - let worker = Worker { + let worker = Self { running: false, matchers: Matchers(matchers), last_snapshot: 0,