Char type in predicate and keyword iterator moved from associated type to generic

This commit is contained in:
Andrew Golovashevich 2025-11-17 22:39:28 +03:00
parent 4cd77ba975
commit a0b74a8ba6
4 changed files with 40 additions and 38 deletions

View File

@ -17,11 +17,12 @@ impl<'source, 'pos, I: SourceIterator<'source, 'pos>> SourceStreamOverIterator<'
} }
} }
struct CollectFunction<'predicate, C, I: Predicate<C = C>> { struct CollectFunction<'predicate, C, I: Predicate<C>> {
predicate: &'predicate mut I, predicate: &'predicate mut I,
__phantom: PhantomData<C>
} }
impl<'predicate, C, I: Predicate<C = C>> _SourceIteratorCollect::Lambda for CollectFunction<'predicate, C, I> { impl<'predicate, C, I: Predicate<C>> _SourceIteratorCollect::Lambda for CollectFunction<'predicate, C, I> {
type C = C; type C = C;
fn collect<'source>(&mut self, src: &mut impl _SourceIteratorCollect::Context<'source, C = Self::C>) -> bool { fn collect<'source>(&mut self, src: &mut impl _SourceIteratorCollect::Context<'source, C = Self::C>) -> bool {
@ -54,7 +55,7 @@ impl<'source, 'pos, I: SourceIterator<'source, 'pos>> SourceStream<'source, 'pos
type P = I::P; type P = I::P;
type CS = I::CS; type CS = I::CS;
fn skip(&mut self, predicate: &mut impl Predicate<C = I::C>) -> bool { fn skip(&mut self, predicate: &mut impl Predicate<I::C>) -> bool {
match self._iter.current() { match self._iter.current() {
Some(c) => match predicate.check(c) { Some(c) => match predicate.check(c) {
false => return false, false => return false,
@ -74,8 +75,8 @@ impl<'source, 'pos, I: SourceIterator<'source, 'pos>> SourceStream<'source, 'pos
} }
} }
fn collect(&mut self, predicate: &mut impl Predicate<C = I::C>) -> CollectResult<I::CS> { fn collect(&mut self, predicate: &mut impl Predicate<I::C>) -> CollectResult<I::CS> {
return self._iter.collect(&mut CollectFunction { predicate }); return self._iter.collect(&mut CollectFunction { predicate, __phantom: PhantomData::default() });
} }
fn pos(&self) -> I::P { fn pos(&self) -> I::P {

View File

@ -1,9 +1,7 @@
use std::slice::Iter; use std::slice::Iter;
use std::str::Chars; use std::str::Chars;
impl<'keyword> crate::KeywordComparatorIterator<'keyword> for Chars<'keyword> { impl<'keyword> crate::KeywordComparatorIterator<'keyword, char> for Chars<'keyword> {
type C = char;
fn consume(&mut self, e: char) -> bool { fn consume(&mut self, e: char) -> bool {
return self.next().is_some_and(|a| a == e); return self.next().is_some_and(|a| a == e);
} }
@ -15,7 +13,7 @@ impl<'keyword> crate::Keyword<'keyword> for &'keyword str {
fn startComparation( fn startComparation(
&self, &self,
expectedLen: usize, expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = char>> { ) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }
@ -30,7 +28,7 @@ impl<'keyword> crate::Keyword<'keyword> for &'keyword String {
fn startComparation( fn startComparation(
&self, &self,
expectedLen: usize, expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = char>> { ) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }
@ -38,9 +36,8 @@ impl<'keyword> crate::Keyword<'keyword> for &'keyword String {
return Option::Some(self.chars()); return Option::Some(self.chars());
} }
} }
impl<'keyword, T: PartialEq> crate::KeywordComparatorIterator<'keyword> for Iter<'keyword, T> { impl<'keyword, C: PartialEq> crate::KeywordComparatorIterator<'keyword, C> for Iter<'keyword, C> {
type C = T; fn consume(&mut self, e: C) -> bool {
fn consume(&mut self, e: T) -> bool {
return self.next().is_some_and(|a| a.eq(&e)); return self.next().is_some_and(|a| a.eq(&e));
} }
} }
@ -51,7 +48,7 @@ impl<'keyword, T: PartialEq> crate::Keyword<'keyword> for &'keyword [T] {
fn startComparation( fn startComparation(
&self, &self,
expectedLen: usize, expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = T>> { ) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }
@ -66,7 +63,7 @@ impl<'keyword, T: PartialEq, const SZ: usize> crate::Keyword<'keyword> for &'key
fn startComparation( fn startComparation(
&self, &self,
expectedLen: usize, expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C=T>> { ) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
if (self.len() != expectedLen) { if (self.len() != expectedLen) {
return Option::None; return Option::None;
} }

View File

@ -3,9 +3,8 @@
mod _keyword_impls; mod _keyword_impls;
// mod macros; // mod macros;
pub trait Predicate { pub trait Predicate<C> {
type C; fn check(&mut self, chr: C) -> bool;
fn check(&mut self, chr: Self::C) -> bool;
} }
pub trait Pos<'pos> {} pub trait Pos<'pos> {}
@ -13,9 +12,8 @@ pub trait Pos<'pos> {}
impl Pos<'static> for () {} impl Pos<'static> for () {}
impl Pos<'static> for usize {} impl Pos<'static> for usize {}
pub trait KeywordComparatorIterator<'keyword> { pub trait KeywordComparatorIterator<'keyword, C> {
type C; fn consume(&mut self, c: C) -> bool;
fn consume(&mut self, c: Self::C) -> bool;
} }
pub trait Keyword<'keyword> { pub trait Keyword<'keyword> {
@ -23,7 +21,7 @@ pub trait Keyword<'keyword> {
fn startComparation( fn startComparation(
&self, &self,
expectedLen: usize, expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'keyword, C=Self::C>>; ) -> Option<impl KeywordComparatorIterator<'keyword, Self::C>>;
} }
pub trait CollectedSubstring<'source> { pub trait CollectedSubstring<'source> {
@ -45,8 +43,8 @@ pub trait SourceStream<'source, 'pos> {
/** /**
* Returns `true` if the end of stream reached. * Returns `true` if the end of stream reached.
*/ */
fn skip(&mut self, predicate: &mut impl Predicate<C=Self::C>) -> bool; fn skip(&mut self, predicate: &mut impl Predicate<Self::C>) -> bool;
fn collect(&mut self, predicate: &mut impl Predicate<C=Self::C>) -> CollectResult<Self::CS>; fn collect(&mut self, predicate: &mut impl Predicate<Self::C>) -> CollectResult<Self::CS>;
fn pos(&self) -> Self::P; fn pos(&self) -> Self::P;

View File

@ -2,6 +2,7 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use source_stream_0::{CollectResult, CollectedSubstring, Pos, Predicate, SourceStream}; use source_stream_0::{CollectResult, CollectedSubstring, Pos, Predicate, SourceStream};
use std::marker::PhantomData;
pub trait StreamConversions_Char<C> { pub trait StreamConversions_Char<C> {
type WC; type WC;
@ -21,27 +22,31 @@ pub trait StreamConversions_Substring<'source, C, CS: CollectedSubstring<'source
} }
pub trait StreamConversions<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C = C>>: pub trait StreamConversions<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C = C>>:
StreamConversions_Char<C> + StreamConversions_Pos<'pos, P> + StreamConversions_Substring<'source, C, CS> StreamConversions_Char<C>
+ StreamConversions_Pos<'pos, P>
+ StreamConversions_Substring<'source, C, CS>
{ {
} }
struct PredicateWrapper<'predicate, I: Predicate> { struct PredicateWrapper<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<W::WC>> {
_orig: &'predicate mut I, _orig: &'predicate mut I,
__phantom: PhantomData<(C, W)>,
} }
impl<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<C = W::WC>> impl<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<W::WC>>
PredicateWrapper<'predicate, I> PredicateWrapper<'predicate, C, W, I>
{ {
fn wrap(pred: &'predicate mut I) -> Self { fn wrap(pred: &'predicate mut I) -> Self {
return PredicateWrapper { _orig: pred }; return PredicateWrapper {
_orig: pred,
__phantom: PhantomData::default(),
};
} }
} }
impl<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<C = W::WC>> Predicate impl<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<W::WC>> Predicate<C>
for PredicateWrapper<'predicate, I> for PredicateWrapper<'predicate, C, W, I>
{ {
type C = C;
fn check(&mut self, chr: C) -> bool { fn check(&mut self, chr: C) -> bool {
return self._orig.check(W::wrapChar(chr)); return self._orig.check(W::wrapChar(chr));
} }
@ -57,6 +62,7 @@ pub struct SourceStreamWrapper<
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>, I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> { > {
_src: I, _src: I,
__phantom: PhantomData<(&'source (), &'pos (), W)>,
} }
impl< impl<
@ -64,7 +70,7 @@ impl<
'pos, 'pos,
C, C,
P: Pos<'pos>, P: Pos<'pos>,
CS: CollectedSubstring<'source>, CS: CollectedSubstring<'source, C = C>,
W: StreamConversions<'source, 'pos, C, P, CS>, W: StreamConversions<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>, I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> SourceStream<'source, 'pos> for SourceStreamWrapper<'source, 'pos, C, P, CS, W, I> > SourceStream<'source, 'pos> for SourceStreamWrapper<'source, 'pos, C, P, CS, W, I>
@ -73,11 +79,11 @@ impl<
type P = W::WP; type P = W::WP;
type CS = W::WCS; type CS = W::WCS;
fn skip(&mut self, predicate: &mut impl Predicate<C = W::WC>) -> bool { fn skip(&mut self, predicate: &mut impl Predicate<W::WC>) -> bool {
self._src.skip(&mut PredicateWrapper::wrap(predicate)) self._src.skip(&mut PredicateWrapper::wrap(predicate))
} }
fn collect(&mut self, predicate: &mut impl Predicate<C = W::WC>) -> CollectResult<W::WCS> { fn collect(&mut self, predicate: &mut impl Predicate<W::WC>) -> CollectResult<W::WCS> {
match self._src.collect(&mut PredicateWrapper::wrap(predicate)) { match self._src.collect(&mut PredicateWrapper::wrap(predicate)) {
CollectResult::EOF => return CollectResult::EOF, CollectResult::EOF => return CollectResult::EOF,
CollectResult::NotMatches => return CollectResult::NotMatches, CollectResult::NotMatches => return CollectResult::NotMatches,