Char type in predicate and keyword iterator moved from associated type to generic
This commit is contained in:
parent
4cd77ba975
commit
a0b74a8ba6
@ -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,
|
||||
__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;
|
||||
|
||||
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 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() {
|
||||
Some(c) => match predicate.check(c) {
|
||||
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> {
|
||||
return self._iter.collect(&mut CollectFunction { predicate });
|
||||
fn collect(&mut self, predicate: &mut impl Predicate<I::C>) -> CollectResult<I::CS> {
|
||||
return self._iter.collect(&mut CollectFunction { predicate, __phantom: PhantomData::default() });
|
||||
}
|
||||
|
||||
fn pos(&self) -> I::P {
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
use std::slice::Iter;
|
||||
use std::str::Chars;
|
||||
|
||||
impl<'keyword> crate::KeywordComparatorIterator<'keyword> for Chars<'keyword> {
|
||||
type C = char;
|
||||
|
||||
impl<'keyword> crate::KeywordComparatorIterator<'keyword, char> for Chars<'keyword> {
|
||||
fn consume(&mut self, e: char) -> bool {
|
||||
return self.next().is_some_and(|a| a == e);
|
||||
}
|
||||
@ -15,7 +13,7 @@ impl<'keyword> crate::Keyword<'keyword> for &'keyword str {
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = char>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
@ -30,7 +28,7 @@ impl<'keyword> crate::Keyword<'keyword> for &'keyword String {
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = char>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
@ -38,20 +36,19 @@ impl<'keyword> crate::Keyword<'keyword> for &'keyword String {
|
||||
return Option::Some(self.chars());
|
||||
}
|
||||
}
|
||||
impl<'keyword, T: PartialEq> crate::KeywordComparatorIterator<'keyword> for Iter<'keyword, T> {
|
||||
type C = T;
|
||||
fn consume(&mut self, e: T) -> bool {
|
||||
impl<'keyword, C: PartialEq> crate::KeywordComparatorIterator<'keyword, C> for Iter<'keyword, C> {
|
||||
fn consume(&mut self, e: C) -> bool {
|
||||
return self.next().is_some_and(|a| a.eq(&e));
|
||||
}
|
||||
}
|
||||
|
||||
impl<'keyword, T: PartialEq> crate::Keyword<'keyword> for &'keyword [T] {
|
||||
type C = T;
|
||||
|
||||
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C = T>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
@ -62,11 +59,11 @@ impl<'keyword, T: PartialEq> crate::Keyword<'keyword> for &'keyword [T] {
|
||||
|
||||
impl<'keyword, T: PartialEq, const SZ: usize> crate::Keyword<'keyword> for &'keyword [T; SZ] {
|
||||
type C = T;
|
||||
|
||||
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C=T>> {
|
||||
) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
|
||||
if (self.len() != expectedLen) {
|
||||
return Option::None;
|
||||
}
|
||||
|
||||
16
src/lib.rs
16
src/lib.rs
@ -3,9 +3,8 @@
|
||||
mod _keyword_impls;
|
||||
// mod macros;
|
||||
|
||||
pub trait Predicate {
|
||||
type C;
|
||||
fn check(&mut self, chr: Self::C) -> bool;
|
||||
pub trait Predicate<C> {
|
||||
fn check(&mut self, chr: C) -> bool;
|
||||
}
|
||||
|
||||
pub trait Pos<'pos> {}
|
||||
@ -13,9 +12,8 @@ pub trait Pos<'pos> {}
|
||||
impl Pos<'static> for () {}
|
||||
impl Pos<'static> for usize {}
|
||||
|
||||
pub trait KeywordComparatorIterator<'keyword> {
|
||||
type C;
|
||||
fn consume(&mut self, c: Self::C) -> bool;
|
||||
pub trait KeywordComparatorIterator<'keyword, C> {
|
||||
fn consume(&mut self, c: C) -> bool;
|
||||
}
|
||||
|
||||
pub trait Keyword<'keyword> {
|
||||
@ -23,7 +21,7 @@ pub trait Keyword<'keyword> {
|
||||
fn startComparation(
|
||||
&self,
|
||||
expectedLen: usize,
|
||||
) -> Option<impl KeywordComparatorIterator<'keyword, C=Self::C>>;
|
||||
) -> Option<impl KeywordComparatorIterator<'keyword, Self::C>>;
|
||||
}
|
||||
|
||||
pub trait CollectedSubstring<'source> {
|
||||
@ -45,8 +43,8 @@ pub trait SourceStream<'source, 'pos> {
|
||||
/**
|
||||
* Returns `true` if the end of stream reached.
|
||||
*/
|
||||
fn skip(&mut self, predicate: &mut impl Predicate<C=Self::C>) -> bool;
|
||||
fn collect(&mut self, predicate: &mut impl Predicate<C=Self::C>) -> CollectResult<Self::CS>;
|
||||
fn skip(&mut self, predicate: &mut impl Predicate<Self::C>) -> bool;
|
||||
fn collect(&mut self, predicate: &mut impl Predicate<Self::C>) -> CollectResult<Self::CS>;
|
||||
|
||||
fn pos(&self) -> Self::P;
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use source_stream_0::{CollectResult, CollectedSubstring, Pos, Predicate, SourceStream};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub trait StreamConversions_Char<C> {
|
||||
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>>:
|
||||
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,
|
||||
__phantom: PhantomData<(C, W)>,
|
||||
}
|
||||
|
||||
impl<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<C = W::WC>>
|
||||
PredicateWrapper<'predicate, I>
|
||||
impl<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<W::WC>>
|
||||
PredicateWrapper<'predicate, C, W, I>
|
||||
{
|
||||
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
|
||||
for PredicateWrapper<'predicate, I>
|
||||
impl<'predicate, C, W: StreamConversions_Char<C>, I: Predicate<W::WC>> Predicate<C>
|
||||
for PredicateWrapper<'predicate, C, W, I>
|
||||
{
|
||||
type C = C;
|
||||
|
||||
fn check(&mut self, chr: C) -> bool {
|
||||
return self._orig.check(W::wrapChar(chr));
|
||||
}
|
||||
@ -57,6 +62,7 @@ pub struct SourceStreamWrapper<
|
||||
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
|
||||
> {
|
||||
_src: I,
|
||||
__phantom: PhantomData<(&'source (), &'pos (), W)>,
|
||||
}
|
||||
|
||||
impl<
|
||||
@ -64,7 +70,7 @@ impl<
|
||||
'pos,
|
||||
C,
|
||||
P: Pos<'pos>,
|
||||
CS: CollectedSubstring<'source>,
|
||||
CS: CollectedSubstring<'source, C = C>,
|
||||
W: StreamConversions<'source, 'pos, C, P, CS>,
|
||||
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
|
||||
> SourceStream<'source, 'pos> for SourceStreamWrapper<'source, 'pos, C, P, CS, W, I>
|
||||
@ -73,11 +79,11 @@ impl<
|
||||
type P = W::WP;
|
||||
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))
|
||||
}
|
||||
|
||||
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)) {
|
||||
CollectResult::EOF => return CollectResult::EOF,
|
||||
CollectResult::NotMatches => return CollectResult::NotMatches,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user