From e1a5aa856241ce2c07e29fe51c46cf9fa306857b Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Tue, 18 Nov 2025 01:35:09 +0300 Subject: [PATCH] Char type in keyword moved from associated type to generic and removed lifetime parameter --- default-streams/src/iterators/array.rs | 2 +- default-streams/src/iterators/str.rs | 2 +- src/_keyword_impls.rs | 35 ++++------ src/lib.rs | 11 ++-- wrapper/src/lib.rs | 91 ++++++++++++++++++++++++-- 5 files changed, 107 insertions(+), 34 deletions(-) diff --git a/default-streams/src/iterators/array.rs b/default-streams/src/iterators/array.rs index e1bff06..7250cf7 100644 --- a/default-streams/src/iterators/array.rs +++ b/default-streams/src/iterators/array.rs @@ -10,7 +10,7 @@ pub struct ArrayCollectedSubstring<'source, C> { impl<'source, C: Copy> CollectedSubstring<'source> for ArrayCollectedSubstring<'source, C> { type C = C; - fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C = C>) -> bool { + fn compareKeyword<'keyword>(&self, kw: impl Keyword) -> bool { let mut kwi; match kw.startComparation(self.slice.len()) { None => return false, diff --git a/default-streams/src/iterators/str.rs b/default-streams/src/iterators/str.rs index c3ae85e..4e9ea78 100644 --- a/default-streams/src/iterators/str.rs +++ b/default-streams/src/iterators/str.rs @@ -12,7 +12,7 @@ pub struct StrCollectedSubstring<'source> { impl<'source> CollectedSubstring<'source> for StrCollectedSubstring<'source> { type C = char; - fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C = char>) -> bool { + fn compareKeyword<'keyword>(&self, kw: impl Keyword) -> bool { let mut kwi; match kw.startComparation(self.size) { None => return false, diff --git a/src/_keyword_impls.rs b/src/_keyword_impls.rs index b0bad77..368ba67 100644 --- a/src/_keyword_impls.rs +++ b/src/_keyword_impls.rs @@ -7,11 +7,10 @@ impl<'keyword> crate::KeywordComparatorIterator<'keyword, char> for Chars<'keywo } } -impl<'keyword> crate::Keyword<'keyword> for &'keyword str { - type C = char; +impl crate::Keyword for &str { - fn startComparation( - &self, + fn startComparation<'keyword>( + &'keyword self, expectedLen: usize, ) -> Option> { if (self.len() != expectedLen) { @@ -22,11 +21,9 @@ impl<'keyword> crate::Keyword<'keyword> for &'keyword str { } } -impl<'keyword> crate::Keyword<'keyword> for &'keyword String { - type C = char; - - fn startComparation( - &self, +impl crate::Keyword for &String { + fn startComparation<'keyword>( + &'keyword self, expectedLen: usize, ) -> Option> { if (self.len() != expectedLen) { @@ -42,13 +39,11 @@ impl<'keyword, C: PartialEq> crate::KeywordComparatorIterator<'keyword, C> for I } } -impl<'keyword, T: PartialEq> crate::Keyword<'keyword> for &'keyword [T] { - type C = T; - - fn startComparation( - &self, +impl crate::Keyword for &[C] { + fn startComparation<'keyword>( + &'keyword self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if (self.len() != expectedLen) { return Option::None; } @@ -57,13 +52,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, +impl crate::Keyword for &[C; SZ] { + fn startComparation<'keyword>( + &'keyword self, expectedLen: usize, - ) -> Option> { + ) -> Option> { if (self.len() != expectedLen) { return Option::None; } diff --git a/src/lib.rs b/src/lib.rs index f3737e6..c6b32d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,17 +16,16 @@ pub trait KeywordComparatorIterator<'keyword, C> { fn consume(&mut self, c: C) -> bool; } -pub trait Keyword<'keyword> { - type C; - fn startComparation( - &self, +pub trait Keyword { + fn startComparation<'keyword>( + &'keyword self, expectedLen: usize, - ) -> Option>; + ) -> Option>; } pub trait CollectedSubstring<'source> { type C; - fn compareKeyword<'keyword>(&self, kw: impl Keyword<'keyword, C=Self::C>) -> bool; + fn compareKeyword<'keyword>(&self, kw: impl Keyword) -> bool; } pub enum CollectResult { diff --git a/wrapper/src/lib.rs b/wrapper/src/lib.rs index 26b26aa..f0c969d 100644 --- a/wrapper/src/lib.rs +++ b/wrapper/src/lib.rs @@ -1,7 +1,10 @@ #![allow(non_camel_case_types)] #![allow(non_snake_case)] -use source_stream_0::{CollectResult, CollectedSubstring, Keyword, Pos, Predicate, SourceStream}; +use source_stream_0::{ + CollectResult, CollectedSubstring, Keyword, KeywordComparatorIterator, Pos, Predicate, + SourceStream, +}; use std::marker::PhantomData; pub trait SourceStreamConverter_Char { @@ -28,10 +31,16 @@ pub trait StreamConverter<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring { } -struct PredicateWrapper<'predicate, 'converter, C, W: SourceStreamConverter_Char, I: Predicate> { +struct PredicateWrapper< + 'predicate, + 'converter, + C, + W: SourceStreamConverter_Char, + I: Predicate, +> { _orig: &'predicate mut I, _converter: &'converter W, - __phantom: PhantomData + __phantom: PhantomData, } impl<'predicate, 'converter, C, W: SourceStreamConverter_Char, I: Predicate> @@ -83,11 +92,15 @@ impl< type CS = W::WCS; fn skip(&mut self, predicate: &mut impl Predicate) -> bool { - self._src.skip(&mut PredicateWrapper::wrap(predicate, &self._converter)) + self._src + .skip(&mut PredicateWrapper::wrap(predicate, &self._converter)) } fn collect(&mut self, predicate: &mut impl Predicate) -> CollectResult { - match self._src.collect(&mut PredicateWrapper::wrap(predicate, &self._converter)) { + match self + ._src + .collect(&mut PredicateWrapper::wrap(predicate, &self._converter)) + { CollectResult::EOF => return CollectResult::EOF, CollectResult::NotMatches => return CollectResult::NotMatches, CollectResult::Matches(cs) => return CollectResult::Matches(W::wrapSubstring(cs)), @@ -106,3 +119,71 @@ impl< return self._src.nextChar().map(W::wrapChar); } } + +pub struct KeywordUnwrapper< + 'keyword, + 'converter, + C, + W: SourceStreamConverter_Char, + I: Keyword, +> { + _orig: &'keyword I, + _converter: &'converter W, + __phantom: PhantomData, +} + +impl<'keyword, 'converter, C, W: SourceStreamConverter_Char, I: Keyword> + KeywordUnwrapper<'keyword, 'converter, C, W, I> +{ + fn unwrap(kw: &'keyword I, converter: &'converter W) -> Self { + return KeywordUnwrapper { + _orig: kw, + _converter: converter, + __phantom: PhantomData::default(), + }; + } +} + +impl<'keyword, 'converter, C, W: SourceStreamConverter_Char, I: Keyword> Keyword + for KeywordUnwrapper<'keyword, 'converter, C, W, I> +{ + fn startComparation<'self_>( + &'self_ self, + expectedLen: usize, + ) -> Option> { + return self + ._orig + .startComparation(expectedLen) + .map(|it| KeywordComparatorWrapper { + _orig: it, + _converter: self._converter, + __phantom: PhantomData::default(), + }); + } +} + +struct KeywordComparatorWrapper< + 'keyword, + 'converter, + C, + W: SourceStreamConverter_Char, + I: KeywordComparatorIterator<'keyword, W::WC>, +> { + _orig: I, + _converter: &'converter W, + __phantom: PhantomData<(&'keyword(), C)>, +} + +impl< + 'keyword, + 'converter, + C, + W: SourceStreamConverter_Char, + I: KeywordComparatorIterator<'keyword, W::WC>, +> KeywordComparatorIterator<'keyword, C> + for KeywordComparatorWrapper<'keyword, 'converter, C, W, I> +{ + fn consume(&mut self, c: C) -> bool { + todo!() + } +}