Simplified _Keyword impls

This commit is contained in:
Andrew Golovashevich 2025-11-09 13:29:58 +03:00
parent dd86157e7e
commit 525ba5d370
2 changed files with 30 additions and 28 deletions

View File

@ -1,63 +1,65 @@
use std::slice::Iter; use std::slice::Iter;
use std::str::Chars; use std::str::Chars;
struct _StringIteratorImpl<'a> { impl crate::_KeywordComparatorIterator<char> for Chars<'_> {
orig: Chars<'a>, fn consume(&mut self, e: char) -> bool {
} return self.next().is_some_and(|a| a == e);
impl crate::_KeywordComparatorIterator<char> for _StringIteratorImpl<'_> {
fn consume(&mut self, e: &char) -> bool {
return self.orig.next().is_some_and(|a| a.eq(e));
} }
} }
impl crate::_Keyword<char> for &str { impl crate::_Keyword<char> for &str {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<char>> { fn startComparation(
&self,
expectedLen: usize,
) -> Option<impl crate::_KeywordComparatorIterator<char>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }
return Option::Some(_StringIteratorImpl { orig: self.chars() }); return Option::Some(self.chars());
} }
} }
impl crate::_Keyword<char> for &String { impl crate::_Keyword<char> for &String {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<char>> { fn startComparation(
&self,
expectedLen: usize,
) -> Option<impl crate::_KeywordComparatorIterator<char>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }
return Option::Some(_StringIteratorImpl { orig: self.chars() }); return Option::Some(self.chars());
} }
} }
impl<T: PartialEq> crate::_KeywordComparatorIterator<T> for Iter<'_, T> {
struct _ArrayIteratorImpl<'a, T> { fn consume(&mut self, e: T) -> bool {
orig: Iter<'a, T>, return self.next().is_some_and(|a| a.eq(&e));
}
impl<T: PartialEq> crate::_KeywordComparatorIterator<T> for _ArrayIteratorImpl<'_, T> {
fn consume(&mut self, e: &T) -> bool {
return self.orig.next().is_some_and(|a| a == e);
} }
} }
impl<T: PartialEq> crate::_Keyword<T> for &[T] { impl<T: PartialEq> crate::_Keyword<T> for &[T] {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<T>> { fn startComparation(
&self,
expectedLen: usize,
) -> Option<impl crate::_KeywordComparatorIterator<T>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }
return Option::Some(_ArrayIteratorImpl { orig: self.iter() }); return Option::Some(self.iter());
} }
} }
impl<T: PartialEq, const SZ: usize> crate::_Keyword<T> for &[T; SZ] { impl<T: PartialEq, const SZ: usize> crate::_Keyword<T> for &[T; SZ] {
fn startComparation(&self, expectedLen: usize) -> Option<impl crate::_KeywordComparatorIterator<T>> { fn startComparation(
&self,
expectedLen: usize,
) -> Option<impl crate::_KeywordComparatorIterator<T>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }
return Option::Some(_ArrayIteratorImpl { orig: self.iter() }); return Option::Some(self.iter());
} }
} }

View File

@ -1,11 +1,11 @@
mod _keyword_impls; mod _keyword_impls;
pub trait Predicate<C> { pub trait Predicate<C> {
fn check(&mut self, chr: &C) -> bool; fn check(&mut self, chr: C) -> bool;
} }
impl<C> Predicate<C> for fn(&C) -> bool { impl<C> Predicate<C> for fn(C) -> bool {
fn check(&mut self, chr: &C) -> bool { fn check(&mut self, chr: C) -> bool {
return self(chr); return self(chr);
} }
} }
@ -13,7 +13,7 @@ impl<C> Predicate<C> for fn(&C) -> bool {
pub trait Pos {} pub trait Pos {}
pub trait _KeywordComparatorIterator<C> { pub trait _KeywordComparatorIterator<C> {
fn consume(&mut self, c: &C) -> bool; fn consume(&mut self, c: C) -> bool;
} }
pub trait _Keyword<C> { pub trait _Keyword<C> {