Char type in keyword moved from associated type to generic and removed lifetime parameter

This commit is contained in:
Andrew Golovashevich 2025-11-18 01:35:09 +03:00
parent 267e8fb82d
commit e1a5aa8562
5 changed files with 107 additions and 34 deletions

View File

@ -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<C>) -> bool {
let mut kwi;
match kw.startComparation(self.slice.len()) {
None => return false,

View File

@ -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<char>) -> bool {
let mut kwi;
match kw.startComparation(self.size) {
None => return false,

View File

@ -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<char> for &str {
fn startComparation(
&self,
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
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<char> for &String {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, char>> {
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<C: PartialEq> crate::Keyword<C> for &[C] {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C>> {
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<C: PartialEq, const SZ: usize> crate::Keyword<C> for &[C; SZ] {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl crate::KeywordComparatorIterator<'keyword, T>> {
) -> Option<impl crate::KeywordComparatorIterator<'keyword, C>> {
if (self.len() != expectedLen) {
return Option::None;
}

View File

@ -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<C> {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'keyword, Self::C>>;
) -> Option<impl KeywordComparatorIterator<'keyword, C>>;
}
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<Self::C>) -> bool;
}
pub enum CollectResult<T> {

View File

@ -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<C> {
@ -28,10 +31,16 @@ pub trait StreamConverter<'source, 'pos, C, P: Pos<'pos>, CS: CollectedSubstring
{
}
struct PredicateWrapper<'predicate, 'converter, C, W: SourceStreamConverter_Char<C>, I: Predicate<W::WC>> {
struct PredicateWrapper<
'predicate,
'converter,
C,
W: SourceStreamConverter_Char<C>,
I: Predicate<W::WC>,
> {
_orig: &'predicate mut I,
_converter: &'converter W,
__phantom: PhantomData<C>
__phantom: PhantomData<C>,
}
impl<'predicate, 'converter, C, W: SourceStreamConverter_Char<C>, I: Predicate<W::WC>>
@ -83,11 +92,15 @@ impl<
type CS = W::WCS;
fn skip(&mut self, predicate: &mut impl Predicate<W::WC>) -> 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<W::WC>) -> CollectResult<W::WCS> {
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<C>,
I: Keyword<W::WC>,
> {
_orig: &'keyword I,
_converter: &'converter W,
__phantom: PhantomData<C>,
}
impl<'keyword, 'converter, C, W: SourceStreamConverter_Char<C>, I: Keyword<W::WC>>
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<C>, I: Keyword<W::WC>> Keyword<C>
for KeywordUnwrapper<'keyword, 'converter, C, W, I>
{
fn startComparation<'self_>(
&'self_ self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'self_, C>> {
return self
._orig
.startComparation(expectedLen)
.map(|it| KeywordComparatorWrapper {
_orig: it,
_converter: self._converter,
__phantom: PhantomData::default(),
});
}
}
struct KeywordComparatorWrapper<
'keyword,
'converter,
C,
W: SourceStreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
> {
_orig: I,
_converter: &'converter W,
__phantom: PhantomData<(&'keyword(), C)>,
}
impl<
'keyword,
'converter,
C,
W: SourceStreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
> KeywordComparatorIterator<'keyword, C>
for KeywordComparatorWrapper<'keyword, 'converter, C, W, I>
{
fn consume(&mut self, c: C) -> bool {
todo!()
}
}