From 732c5a12fc7ab41b77a53f7efdf92fc9aced7a4d Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 16 Nov 2025 21:55:46 +0300 Subject: [PATCH] Rewrote generics to associated types --- default-streams/src/_sandbox.rs | 25 +++--- default-streams/src/iterator.rs | 73 ++++++++++++---- default-streams/src/iterators/array.rs | 43 +++++----- default-streams/src/iterators/str.rs | 46 +++++----- default-streams/src/lib.rs | 2 +- default-streams/src/pos/index.rs | 11 ++- default-streams/src/pos/newline.rs | 20 +++-- default-streams/src/pos/nop.rs | 15 +++- default-streams/src/stream.rs | 114 +++++++++++-------------- src/_keyword_impls.rs | 31 ++++--- src/lib.rs | 42 +++++---- 11 files changed, 243 insertions(+), 179 deletions(-) diff --git a/default-streams/src/_sandbox.rs b/default-streams/src/_sandbox.rs index 51b098c..d3a6886 100644 --- a/default-streams/src/_sandbox.rs +++ b/default-streams/src/_sandbox.rs @@ -1,32 +1,33 @@ +/*use source_stream_0::{CollectedSubstring, Pos, SourceStream}; use crate::iterators::StrSourceIterator; use crate::pos::{IndexPosCounter, NewLinePosCounter, PosLineCol}; use crate::{_CollectScopeContext, SourceIterator}; #[test] fn sandbox() { - let mut z: StrSourceIterator<'_, '_, usize, IndexPosCounter> = - StrSourceIterator::start("12\n34"); + let mut z = StrSourceIterator::start("12\n34"); println!("{}", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); println!("{}", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); println!("{} ", z.pos()); - z.next(); + z.next(); return; } +*/ \ No newline at end of file diff --git a/default-streams/src/iterator.rs b/default-streams/src/iterator.rs index 6578ca4..507f0eb 100644 --- a/default-streams/src/iterator.rs +++ b/default-streams/src/iterator.rs @@ -1,26 +1,69 @@ use source_stream_0::{CollectResult, CollectedSubstring, Pos}; +use std::marker::PhantomData; -pub trait PosCounter<'pos, C, P: Pos<'pos>> { - fn update(&mut self, c: C); +pub trait PosCounter<'pos> { + type C; + type P: Pos<'pos>; - fn export(&self) -> P; + fn update(&mut self, c: Self::C); + + fn export(&self) -> Self::P; fn init() -> Self; } -pub trait _CollectScopeContext<'source, C> { - fn next(&mut self) -> Option; - fn current(&self) -> Option; + +pub trait SourceIterator<'source, 'pos> { + type C; + type P: Pos<'pos>; + type CS: CollectedSubstring<'source, C = Self::C>; + + fn next(&mut self) -> Option; + fn current(&self) -> Option; + fn pos(&self) -> Self::P; + + fn collect(&mut self, scope: &mut impl _SourceIteratorCollect::Lambda) -> CollectResult; } -pub trait SourceIterator<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C>>: - _CollectScopeContext<'source, C> -{ - fn pos(&self) -> P; +pub mod _SourceIteratorCollect { + use crate::SourceIterator; + use std::marker::PhantomData; - type CollectScopeContext: _CollectScopeContext<'source, C>; + pub trait Context<'source> { + type C; + fn next(&mut self) -> Option; + fn current(&self) -> Option; + } - fn collect( - &mut self, - scope: impl FnOnce(&mut Self::CollectScopeContext) -> bool, - ) -> CollectResult; + pub trait Lambda { + type C; + fn collect<'source>(&mut self, src: &mut impl Context<'source, C=Self::C>) -> bool; + } + + pub struct DefaultImpl<'source, 'pos, 'iter, I: SourceIterator<'source, 'pos>> { + _full: &'iter mut I, + __phantom: PhantomData<(&'source (), &'pos ())>, + } + + impl<'source, 'pos, 'iter, I: SourceIterator<'source, 'pos>> DefaultImpl<'source, 'pos, 'iter, I> { + pub fn wrap(s: &'iter mut I) -> Self { + return DefaultImpl { + _full: s, + __phantom: PhantomData::default(), + }; + } + } + + impl<'source, 'pos, 'iter, I: SourceIterator<'source, 'pos>> Context<'source> + for DefaultImpl<'source, 'pos, 'iter, I> + { + type C = I::C; + + fn next(&mut self) -> Option { + return self._full.next(); + } + + fn current(&self) -> Option { + return self._full.current(); + } + } } diff --git a/default-streams/src/iterators/array.rs b/default-streams/src/iterators/array.rs index 30eea48..6f30fba 100644 --- a/default-streams/src/iterators/array.rs +++ b/default-streams/src/iterators/array.rs @@ -1,4 +1,5 @@ -use crate::iterator::{_CollectScopeContext, PosCounter, SourceIterator}; +use crate::_SourceIteratorCollect; +use crate::iterator::{PosCounter, SourceIterator}; use source_stream_0::{CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator, Pos}; use std::marker::PhantomData; use std::slice::Iter; @@ -7,8 +8,9 @@ pub struct ArrayCollectedSubstring<'source, C> { pub slice: &'source [C], } -impl<'source, C: Copy> CollectedSubstring<'source, C> for ArrayCollectedSubstring<'source, C> { - fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C>) -> bool { +impl<'source, C: Copy> CollectedSubstring<'source> for ArrayCollectedSubstring<'source, C> { + type C = C; + fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C = C>) -> bool { let mut kwi; match kw.startComparation(self.slice.len()) { None => return false, @@ -25,20 +27,17 @@ impl<'source, C: Copy> CollectedSubstring<'source, C> for ArrayCollectedSubstrin } } -pub struct ArraySourceIterator<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> { +pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos>> { _source: &'source [C], _iter: Iter<'source, C>, _current: Option<&'source C>, _posRaw: usize, _posHighlevel: PC, - __phantom1: PhantomData<&'pos ()>, - __phantom2: PhantomData

, + __phantom: PhantomData<&'pos ()>, } -impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> - ArraySourceIterator<'source, 'pos, C, P, PC> -{ - pub fn start(arr: &'source [C]) -> ArraySourceIterator<'source, 'pos, C, P, PC> { +impl<'source, 'pos, C: Copy, PC: PosCounter<'pos>> ArraySourceIterator<'source, 'pos, C, PC> { + pub fn start(arr: &'source [C]) -> ArraySourceIterator<'source, 'pos, C, PC> { let mut it = arr.iter(); let first = it.next(); return ArraySourceIterator { @@ -47,15 +46,18 @@ impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> _current: first, _posRaw: 0, _posHighlevel: PC::init(), - __phantom1: PhantomData::default(), - __phantom2: PhantomData::default(), + __phantom: PhantomData::default(), }; } } -impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> - _CollectScopeContext<'source, C> for ArraySourceIterator<'source, 'pos, C, P, PC> +impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C = C>> SourceIterator<'source, 'pos> + for ArraySourceIterator<'source, 'pos, C, PC> { + type C = C; + type P = PC::P; + type CS = ArrayCollectedSubstring<'source, C>; + fn next(&mut self) -> Option { if let Some(c) = self._current { self._posHighlevel.update(*c); @@ -68,21 +70,14 @@ impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> fn current(&self) -> Option { return self._current.map(|c| *c); } -} -impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> - SourceIterator<'source, 'pos, C, P, ArrayCollectedSubstring<'source, C>> - for ArraySourceIterator<'source, 'pos, C, P, PC> -{ - fn pos(&self) -> P { + fn pos(&self) -> Self::P { return self._posHighlevel.export(); } - type CollectScopeContext = ArraySourceIterator<'source, 'pos, C, P, PC>; - fn collect( &mut self, - scope: impl FnOnce(&mut Self::CollectScopeContext) -> bool, + scope: &mut impl _SourceIteratorCollect::Lambda, ) -> CollectResult> { if self._current.is_none() { return CollectResult::EOF; @@ -90,7 +85,7 @@ impl<'source, 'pos, C: Copy, P: Pos<'pos>, PC: PosCounter<'pos, C, P>> let startPosRaw = self._posRaw; - match scope(self) { + match scope.collect(&mut _SourceIteratorCollect::DefaultImpl::wrap(self)) { false => return CollectResult::NotMatches, true => { let slice: &'source [C]; diff --git a/default-streams/src/iterators/str.rs b/default-streams/src/iterators/str.rs index 9f4316e..a443952 100644 --- a/default-streams/src/iterators/str.rs +++ b/default-streams/src/iterators/str.rs @@ -1,5 +1,6 @@ -use crate::iterator::{_CollectScopeContext, PosCounter, SourceIterator}; -use source_stream_0::{CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator, Pos}; +use crate::_SourceIteratorCollect; +use crate::iterator::{PosCounter, SourceIterator}; +use source_stream_0::{CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator}; use std::marker::PhantomData; use std::str::CharIndices; @@ -8,8 +9,10 @@ pub struct StrCollectedSubstring<'source> { pub size: usize, } -impl<'source> CollectedSubstring<'source, char> for StrCollectedSubstring<'source> { - fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, char>) -> bool { +impl<'source> CollectedSubstring<'source> for StrCollectedSubstring<'source> { + type C = char; + + fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C = char>) -> bool { let mut kwi; match kw.startComparation(self.size) { None => return false, @@ -26,21 +29,18 @@ impl<'source> CollectedSubstring<'source, char> for StrCollectedSubstring<'sourc } } -pub struct StrSourceIterator<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> { +pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, C = char>> { _source: &'source str, _iter: CharIndices<'source>, _current: Option, _posRaw: usize, _posCodePoints: usize, _posHighlevel: PC, - __phantom1: PhantomData<&'pos ()>, - __phantom2: PhantomData

, + __phantom: PhantomData<(&'pos ())>, } -impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> - StrSourceIterator<'source, 'pos, P, PC> -{ - pub fn start(s: &'source str) -> StrSourceIterator<'source, 'pos, P, PC> { +impl<'source, 'pos, PC: PosCounter<'pos, C = char>> StrSourceIterator<'source, 'pos, PC> { + pub fn start(s: &'source str) -> Self { let mut it = s.char_indices(); let first = it.next(); return StrSourceIterator { @@ -50,15 +50,18 @@ impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> _posRaw: first.map_or(s.len(), |(p, _)| p), _posCodePoints: 0, _posHighlevel: PC::init(), - __phantom1: PhantomData::default(), - __phantom2: PhantomData::default(), + __phantom: PhantomData::default(), }; } } -impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> _CollectScopeContext<'source, char> - for StrSourceIterator<'source, 'pos, P, PC> +impl<'source, 'pos, PC: PosCounter<'pos, C = char>> SourceIterator<'source, 'pos> + for StrSourceIterator<'source, 'pos, PC> { + type C = char; + type P = PC::P; + type CS = StrCollectedSubstring<'source>; + fn next(&mut self) -> Option { match self._current { None => return None, @@ -85,21 +88,14 @@ impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> _CollectScopeCo fn current(&self) -> Option { return self._current; } -} -impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> - SourceIterator<'source, 'pos, char, P, StrCollectedSubstring<'source>> - for StrSourceIterator<'source, 'pos, P, PC> -{ - fn pos(&self) -> P { + fn pos(&self) -> PC::P { return self._posHighlevel.export(); } - type CollectScopeContext = StrSourceIterator<'source, 'pos, P, PC>; - fn collect( &mut self, - scope: impl FnOnce(&mut Self::CollectScopeContext) -> bool, + scope: &mut impl _SourceIteratorCollect::Lambda, ) -> CollectResult> { if self._current.is_none() { return CollectResult::EOF; @@ -108,7 +104,7 @@ impl<'source, 'pos, P: Pos<'pos>, PC: PosCounter<'pos, char, P>> let startPosRaw = self._posRaw; let startPosCodePoints = self._posCodePoints; - match scope(self) { + match scope.collect(&mut _SourceIteratorCollect::DefaultImpl::wrap(self)) { false => return CollectResult::NotMatches, true => { let slice: &'source str; diff --git a/default-streams/src/lib.rs b/default-streams/src/lib.rs index 1362dc9..75412c8 100644 --- a/default-streams/src/lib.rs +++ b/default-streams/src/lib.rs @@ -2,7 +2,7 @@ mod iterator; mod stream; pub use iterator::PosCounter; pub use iterator::SourceIterator; -pub use iterator::_CollectScopeContext; +pub use iterator::_SourceIteratorCollect; pub use stream::SourceStreamOverIterator; pub mod pos; diff --git a/default-streams/src/pos/index.rs b/default-streams/src/pos/index.rs index 691f133..55dd232 100644 --- a/default-streams/src/pos/index.rs +++ b/default-streams/src/pos/index.rs @@ -1,10 +1,15 @@ +use std::marker::PhantomData; use crate::iterator::PosCounter; -pub struct IndexPosCounter { +pub struct IndexPosCounter { index: usize, + __phantom: PhantomData } -impl PosCounter<'static, C, usize> for IndexPosCounter { +impl PosCounter<'static> for IndexPosCounter { + type C = C; + type P = usize; + fn update(&mut self, c: C) { self.index += 1; } @@ -14,6 +19,6 @@ impl PosCounter<'static, C, usize> for IndexPosCounter { } fn init() -> Self { - return IndexPosCounter { index: 0 }; + return IndexPosCounter { index: 0, __phantom: PhantomData::default() }; } } diff --git a/default-streams/src/pos/newline.rs b/default-streams/src/pos/newline.rs index af28c4e..f78a722 100644 --- a/default-streams/src/pos/newline.rs +++ b/default-streams/src/pos/newline.rs @@ -1,3 +1,4 @@ +use std::marker::PhantomData; use crate::iterator::PosCounter; use source_stream_0::Pos; @@ -8,13 +9,14 @@ pub struct PosLineCol { impl Pos<'static> for PosLineCol {} -pub struct NewLinePosCounter { +pub struct NewLinePosCounter { row: usize, col: usize, + __phantom: PhantomData } -impl NewLinePosCounter { - pub fn _update(&mut self, actual: C, expected: C) { +impl NewLinePosCounter { + pub fn _update(&mut self, actual: C, expected: C) { if actual == expected { self.row += 1; self.col = 0; @@ -31,11 +33,14 @@ impl NewLinePosCounter { } pub fn _init() -> Self { - return NewLinePosCounter { row: 0, col: 0 }; + return NewLinePosCounter { row: 0, col: 0, __phantom: Default::default() }; } } -impl PosCounter<'static, char, PosLineCol> for NewLinePosCounter { +impl PosCounter<'static> for NewLinePosCounter { + type C = char; + type P = PosLineCol; + fn update(&mut self, c: char) { self._update(c, '\n') } @@ -49,7 +54,10 @@ impl PosCounter<'static, char, PosLineCol> for NewLinePosCounter { } } -impl PosCounter<'static, u8, PosLineCol> for NewLinePosCounter { +impl PosCounter<'static> for NewLinePosCounter { + type C = u8; + type P = PosLineCol; + fn update(&mut self, c: u8) { self._update(c, b'\n') } diff --git a/default-streams/src/pos/nop.rs b/default-streams/src/pos/nop.rs index d8be3c8..365c383 100644 --- a/default-streams/src/pos/nop.rs +++ b/default-streams/src/pos/nop.rs @@ -1,12 +1,21 @@ use crate::iterator::PosCounter; -pub struct NopPosCounter {} +use std::marker::PhantomData; + +pub struct NopPosCounter { + __phantom: PhantomData, +} + +impl PosCounter<'static> for NopPosCounter { + type C = C; + type P = (); -impl PosCounter<'static, C, ()> for NopPosCounter { fn update(&mut self, c: C) {} fn export(&self) {} fn init() -> Self { - return NopPosCounter {}; + return NopPosCounter { + __phantom: PhantomData::default(), + }; } } diff --git a/default-streams/src/stream.rs b/default-streams/src/stream.rs index aac41f8..c93eeeb 100644 --- a/default-streams/src/stream.rs +++ b/default-streams/src/stream.rs @@ -1,54 +1,60 @@ -use crate::iterator::{_CollectScopeContext, SourceIterator}; -use source_stream_0::{CollectResult, CollectedSubstring, Pos, Predicate, SourceStream}; +use crate::_SourceIteratorCollect; +use crate::iterator::SourceIterator; +use source_stream_0::{CollectResult, CollectedSubstring, Predicate, SourceStream}; use std::marker::PhantomData; -pub struct SourceStreamOverIterator< - 'source, - 'pos, - C, - P: Pos<'pos>, - CS: CollectedSubstring<'source, C>, - I: SourceIterator<'source, 'pos, C, P, CS>, -> { +pub struct SourceStreamOverIterator<'source, 'pos, I: SourceIterator<'source, 'pos>> { _iter: I, - __phantom1: PhantomData<&'source ()>, - __phantom2: PhantomData<&'pos ()>, - __phantom3: PhantomData, - __phantom4: PhantomData

, - __phantom5: PhantomData, + __phantom: PhantomData<(&'source (), &'pos ())>, } -impl< - 'source, - 'pos, - C, - P: Pos<'pos>, - CS: CollectedSubstring<'source, C>, - I: SourceIterator<'source, 'pos, C, P, CS>, -> SourceStreamOverIterator<'source, 'pos, C, P, CS, I> -{ - fn wrap(iter: I) -> SourceStreamOverIterator<'source, 'pos, C, P, CS, I> { +impl<'source, 'pos, I: SourceIterator<'source, 'pos>> SourceStreamOverIterator<'source, 'pos, I> { + fn wrap(iter: I) -> SourceStreamOverIterator<'source, 'pos, I> { return SourceStreamOverIterator { _iter: iter, - __phantom1: PhantomData::default(), - __phantom2: PhantomData::default(), - __phantom3: PhantomData::default(), - __phantom4: PhantomData::default(), - __phantom5: PhantomData::default(), + __phantom: PhantomData::default(), }; } } -impl< - 'source, - 'pos, - C, - P: Pos<'pos>, - CS: CollectedSubstring<'source, C>, - I: SourceIterator<'source, 'pos, C, P, CS>, -> SourceStream<'source, 'pos, C, P, CS> for SourceStreamOverIterator<'source, 'pos, C, P, CS, I> +struct CollectFunction<'predicate, C, I: Predicate> { + predicate: &'predicate mut I, +} + +impl<'predicate, C, I: Predicate> _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 { + match src.current() { + Some(c) => match self.predicate.check(c) { + false => return false, + true => {} + }, + None => return false, + } + + loop { + match src.next() { + None => { + return true; + } + Some(c) => match self.predicate.check(c) { + true => continue, + false => return true, + }, + } + } + } +} + +impl<'source, 'pos, I: SourceIterator<'source, 'pos>> SourceStream<'source, 'pos> + for SourceStreamOverIterator<'source, 'pos, I> { - fn skip(&mut self, predicate: &mut impl Predicate) -> bool { + type C = I::C; + type P = I::P; + type CS = I::CS; + + fn skip(&mut self, predicate: &mut impl Predicate) -> bool { match self._iter.current() { Some(c) => match predicate.check(c) { false => return false, @@ -68,39 +74,19 @@ impl< } } - fn collect(&mut self, predicate: &mut impl Predicate) -> CollectResult { - return self._iter.collect(|iter| { - match iter.current() { - Some(c) => match predicate.check(c) { - false => return false, - true => {} - }, - None => return false, - } - - loop { - match iter.next() { - None => { - return true; - } - Some(c) => match predicate.check(c) { - true => continue, - false => return true, - }, - } - } - }); + fn collect(&mut self, predicate: &mut impl Predicate) -> CollectResult { + return self._iter.collect(&mut CollectFunction { predicate }); } - fn pos(&self) -> P { + fn pos(&self) -> I::P { return self._iter.pos(); } - fn currentChar(&self) -> Option { + fn currentChar(&self) -> Option { return self._iter.current(); } - fn nextChar(&mut self) -> Option { + fn nextChar(&mut self) -> Option { return self._iter.next(); } } diff --git a/src/_keyword_impls.rs b/src/_keyword_impls.rs index 5945a15..07dc8fb 100644 --- a/src/_keyword_impls.rs +++ b/src/_keyword_impls.rs @@ -1,17 +1,21 @@ use std::slice::Iter; use std::str::Chars; -impl <'keyword> crate::KeywordComparatorIterator<'keyword, char> for Chars<'keyword> { +impl<'keyword> crate::KeywordComparatorIterator<'keyword> for Chars<'keyword> { + type C = char; + fn consume(&mut self, e: char) -> bool { return self.next().is_some_and(|a| a == e); } } -impl <'keyword> crate::Keyword<'keyword, char> for &'keyword str { +impl<'keyword> crate::Keyword<'keyword> for &'keyword str { + type C = char; + fn startComparation( &self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if (self.len() != expectedLen) { return Option::None; } @@ -20,11 +24,13 @@ impl <'keyword> crate::Keyword<'keyword, char> for &'keyword str { } } -impl <'keyword> crate::Keyword<'keyword, char> for &'keyword String { +impl<'keyword> crate::Keyword<'keyword> for &'keyword String { + type C = char; + fn startComparation( &self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if (self.len() != expectedLen) { return Option::None; } @@ -32,17 +38,20 @@ impl <'keyword> crate::Keyword<'keyword, char> for &'keyword String { return Option::Some(self.chars()); } } -impl<'keyword, T: PartialEq> crate::KeywordComparatorIterator<'keyword, T> for Iter<'keyword, T> { +impl<'keyword, T: PartialEq> crate::KeywordComparatorIterator<'keyword> for Iter<'keyword, T> { + type C = T; fn consume(&mut self, e: T) -> bool { return self.next().is_some_and(|a| a.eq(&e)); } } -impl<'keyword, T: PartialEq> crate::Keyword<'keyword, T> for &'keyword [T] { +impl<'keyword, T: PartialEq> crate::Keyword<'keyword> for &'keyword [T] { + type C = T; + fn startComparation( &self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if (self.len() != expectedLen) { return Option::None; } @@ -51,11 +60,13 @@ impl<'keyword, T: PartialEq> crate::Keyword<'keyword, T> for &'keyword [T] { } } -impl<'keyword, T: PartialEq, const SZ: usize> crate::Keyword<'keyword, T> for &'keyword [T; SZ] { +impl<'keyword, T: PartialEq, const SZ: usize> crate::Keyword<'keyword> for &'keyword [T; SZ] { + type C = T; + fn startComparation( &self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if (self.len() != expectedLen) { return Option::None; } diff --git a/src/lib.rs b/src/lib.rs index e748eae..dd62ad2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,11 @@ -mod _keyword_impls; -mod macros; +#![allow(non_snake_case)] -pub trait Predicate { - fn check(&mut self, chr: C) -> bool; +mod _keyword_impls; +// mod macros; + +pub trait Predicate { + type C; + fn check(&mut self, chr: Self::C) -> bool; } pub trait Pos<'pos> {} @@ -10,19 +13,22 @@ pub trait Pos<'pos> {} impl Pos<'static> for () {} impl Pos<'static> for usize {} -pub trait KeywordComparatorIterator<'keyword, C> { - fn consume(&mut self, c: C) -> bool; +pub trait KeywordComparatorIterator<'keyword> { + type C; + fn consume(&mut self, c: Self::C) -> bool; } -pub trait Keyword<'keyword, C> { +pub trait Keyword<'keyword> { + type C; fn startComparation( &self, expectedLen: usize, - ) -> Option>; + ) -> Option>; } -pub trait CollectedSubstring<'source, C> { - fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C>) -> bool; +pub trait CollectedSubstring<'source> { + type C; + fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C=Self::C>) -> bool; } pub enum CollectResult { @@ -31,17 +37,21 @@ pub enum CollectResult { Matches(T), } -pub trait SourceStream<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring<'source, C>> { +pub trait SourceStream<'source, 'pos> { + type C; + type P: Pos<'pos>; + type CS: CollectedSubstring<'source, C=Self::C>; + /** * Returns `true` if the end of stream reached. */ - fn skip(&mut self, predicate: &mut impl Predicate) -> bool; - fn collect(&mut self, predicate: &mut impl Predicate) -> CollectResult; + fn skip(&mut self, predicate: &mut impl Predicate) -> bool; + fn collect(&mut self, predicate: &mut impl Predicate) -> CollectResult; - fn pos(&self) -> P; + fn pos(&self) -> Self::P; - fn currentChar(&self) -> Option; - fn nextChar(&mut self) -> Option; + fn currentChar(&self) -> Option; + fn nextChar(&mut self) -> Option; fn isEnded(&mut self) -> bool { return self.currentChar().is_none();