diff --git a/src/language/word_sequence.rs b/src/language/word_sequence.rs index 0db78c1..b240555 100644 --- a/src/language/word_sequence.rs +++ b/src/language/word_sequence.rs @@ -3,10 +3,12 @@ use std::num::NonZeroUsize; use std::ops::{Div, Range}; #[cfg(feature = "pyo3")] +#[allow(clippy::wildcard_imports)] use pyo3::exceptions::*; #[cfg(feature = "pyo3")] use pyo3::prelude::*; #[cfg(feature = "pyo3")] +#[allow(clippy::wildcard_imports)] use pyo3::types::*; #[cfg(feature = "pyo3")] use std::hash::{DefaultHasher, Hasher}; @@ -108,17 +110,18 @@ impl IntoIterator for &WordSequence { } } +#[allow(clippy::module_name_repetitions)] pub trait WordSequenceIndex { type Output; - fn get(&self, word_sequence: &WordSequence) -> Option; + fn get(self, word_sequence: &WordSequence) -> Option; } impl WordSequenceIndex for usize { type Output = &'static str; #[inline] - fn get(&self, word_sequence: &WordSequence) -> Option { + fn get(self, word_sequence: &WordSequence) -> Option { let data_index: Self = self.checked_mul(word_sequence.padded_word_byte_count.into())?; Some( @@ -135,11 +138,25 @@ impl WordSequenceIndex for usize { } } +impl WordSequenceIndex for isize { + type Output = &'static str; + + #[inline] + fn get(self, word_sequence: &WordSequence) -> Option { + let uindex: Option = if self >= 0 { + 0usize.checked_add_signed(self) + } else { + word_sequence.len().checked_add_signed(self) + }; + uindex?.get(word_sequence) + } +} + impl WordSequenceIndex for Range { type Output = WordSequence; #[inline] - fn get(&self, word_sequence: &WordSequence) -> Option { + fn get(self, word_sequence: &WordSequence) -> Option { Some(WordSequence { data: word_sequence.data.get( self.start @@ -195,25 +212,14 @@ impl WordSequence { self.len() } - #[must_use] + #[inline] + #[allow(clippy::needless_pass_by_value)] pub fn __getitem__(&self, index: GetItemArg) -> PyResult { match index { - GetItemArg::Int(index) => { - let uindex: Option = if index >= 0 { - usize::try_from(index).ok() - } else { - self.len().checked_add_signed(index) - }; - match uindex { - None => Err(PyIndexError::new_err("Invalid index")), - Some(index) => match self.get(index) { - None => { - Err(PyIndexError::new_err("Index out of range")) - } - Some(value) => Ok(GetItemResult::Item(value)), - }, - } - } + GetItemArg::Int(index) => match self.get(index) { + None => Err(PyIndexError::new_err("Index out of range")), + Some(value) => Ok(GetItemResult::Item(value)), + }, } } @@ -229,9 +235,9 @@ impl WordSequence { !self.is_empty() } - #[must_use] #[inline] #[pyo3(signature = (arg, /))] + #[allow(clippy::needless_pass_by_value)] pub fn index(&self, arg: ContainsArg) -> PyResult { match arg { ContainsArg::StringArg(string) => { @@ -256,6 +262,7 @@ impl WordSequence { #[must_use] #[inline] + #[pyo3(signature = (arg, /))] pub fn count(&self, arg: ContainsArg) -> u8 { u8::from(self.__contains__(arg)) } diff --git a/src/lib.rs b/src/lib.rs index 7558f95..e422c18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,12 +2,11 @@ #![warn( clippy::missing_const_for_fn, clippy::nursery, - clippy::option_if_let_else, clippy::pedantic, clippy::todo )] #![deny(clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)] -#![allow(clippy::missing_errors_doc)] +#![allow(clippy::missing_errors_doc, clippy::option_if_let_else)] mod language; mod solver; @@ -55,6 +54,7 @@ pub fn solve_crossword( Ok(pattern.solve(language, Some(max_words_to_collect))) } +#[must_use] #[cfg(feature = "pyo3")] #[pyfunction] #[pyo3(signature = (language, word_length))] diff --git a/src/main.rs b/src/main.rs index 0ca2d2a..3ffbf3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,11 @@ #![warn( clippy::missing_const_for_fn, clippy::nursery, - clippy::option_if_let_else, clippy::pedantic, clippy::todo )] #![deny(clippy::indexing_slicing, clippy::panic, clippy::unwrap_used)] -#![allow(clippy::missing_errors_doc)] +#![allow(clippy::missing_errors_doc, clippy::option_if_let_else)] mod language; mod solver;