Removed lifetimes from traits

This commit is contained in:
Andrew Golovashevich 2025-11-24 22:08:05 +03:00
parent 8388ea0657
commit f6f531cf56
14 changed files with 93 additions and 150 deletions

View File

@ -1,36 +1,33 @@
use std::slice::Iter;
use std::str::Chars;
pub trait KeywordComparatorIterator<'keyword, C> {
pub trait KeywordComparatorIterator<C: Copy> {
fn consume(&mut self, c: C) -> bool;
}
pub trait Keyword<C> {
pub trait Keyword<C: Copy> {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'keyword, C>>;
) -> Option<impl KeywordComparatorIterator<C> + use<'keyword, Self, C>>;
}
pub trait CollectedSubstring<'source> {
type C;
fn compareKeyword<'keyword>(&self, kw: impl Keyword<Self::C>) -> bool;
pub trait CollectedSubstring {
type C: Copy;
fn compareKeyword(&self, kw: impl Keyword<Self::C>) -> bool;
}
impl<'keyword> KeywordComparatorIterator<'keyword, char> for Chars<'keyword> {
impl<'keyword> KeywordComparatorIterator<char> for Chars<'keyword> {
fn consume(&mut self, e: char) -> bool {
return self.next().is_some_and(|a| a == e);
}
}
impl Keyword<char> for &str {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'keyword, char>> {
) -> Option<impl KeywordComparatorIterator<char>> {
if self.len() != expectedLen {
return Option::None;
}
@ -43,7 +40,7 @@ impl Keyword<char> for &String {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'keyword, char>> {
) -> Option<impl KeywordComparatorIterator<char>> {
if self.len() != expectedLen {
return Option::None;
}
@ -51,17 +48,17 @@ impl Keyword<char> for &String {
return Option::Some(self.chars());
}
}
impl<'keyword, C: PartialEq> KeywordComparatorIterator<'keyword, C> for Iter<'keyword, C> {
impl<'keyword, C: Copy + PartialEq> KeywordComparatorIterator<C> for Iter<'keyword, C> {
fn consume(&mut self, e: C) -> bool {
return self.next().is_some_and(|a| a.eq(&e));
}
}
impl<C: PartialEq> Keyword<C> for &[C] {
impl<C: Copy + PartialEq> Keyword<C> for &[C] {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'keyword, C>> {
) -> Option<impl KeywordComparatorIterator<C>> {
if self.len() != expectedLen {
return Option::None;
}
@ -70,11 +67,11 @@ impl<C: PartialEq> Keyword<C> for &[C] {
}
}
impl<C: PartialEq, const SZ: usize> Keyword<C> for &[C; SZ] {
impl<C: Copy + PartialEq, const SZ: usize> Keyword<C> for &[C; SZ] {
fn startComparation<'keyword>(
&'keyword self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'keyword, C>> {
) -> Option<impl KeywordComparatorIterator<C>> {
if self.len() != expectedLen {
return Option::None;
}

View File

@ -6,38 +6,28 @@ pub trait StreamConverter_Char<C: Copy> {
fn convertChar(&self, c: C) -> Self::WC;
}
pub trait StreamConverter_Pos<'pos, P: Pos<'pos>> {
type WP: Pos<'pos>;
pub trait StreamConverter_Pos<P: Pos> {
type WP: Pos;
fn convertPos(&self, p: P) -> Self::WP;
}
pub trait StreamConverter_Substring<'source, C: Copy, CS: CollectedSubstring<'source, C = C>>:
StreamConverter_Char<C>
pub trait StreamConverter_Substring<C: Copy, CS: CollectedSubstring<C = C>>:
StreamConverter_Char<C>
{
type WCS: CollectedSubstring<'source, C = Self::WC>;
type WCS: CollectedSubstring<C = Self::WC>;
fn convertSubstring(&self, wcs: CS) -> Self::WCS;
}
pub trait StreamConverter<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
>:
StreamConverter_Char<C> + StreamConverter_Pos<'pos, P> + StreamConverter_Substring<'source, C, CS>
pub trait StreamConverter<C: Copy, P: Pos, CS: CollectedSubstring<C = C>>:
StreamConverter_Char<C> + StreamConverter_Pos<P> + StreamConverter_Substring<C, CS>
{
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter_Char<C>
+ StreamConverter_Pos<'pos, P>
+ StreamConverter_Substring<'source, C, CS>,
> StreamConverter<'source, 'pos, C, P, CS> for W
P: Pos,
CS: CollectedSubstring<C = C>,
W: StreamConverter_Char<C> + StreamConverter_Pos<P> + StreamConverter_Substring<C, CS>,
> StreamConverter<C, P, CS> for W
{
}
}

View File

@ -3,25 +3,23 @@ use crate::{Keyword, KeywordComparatorIterator};
use std::marker::PhantomData;
struct KeywordComparatorConverter<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
I: KeywordComparatorIterator<W::WC>,
> {
_orig: I,
_converter: &'converter W,
__phantom: PhantomData<(&'keyword (), C)>,
__phantom: PhantomData<C>,
}
impl<
'keyword,
'converter,
C: Copy,
W: StreamConverter_Char<C>,
I: KeywordComparatorIterator<'keyword, W::WC>,
> KeywordComparatorIterator<'keyword, C>
for KeywordComparatorConverter<'keyword, 'converter, C, W, I>
I: KeywordComparatorIterator<W::WC>,
> KeywordComparatorIterator<C>
for KeywordComparatorConverter<'converter, C, W, I>
{
fn consume(&mut self, c: C) -> bool {
self._orig.consume(self._converter.convertChar(c))
@ -58,7 +56,7 @@ impl<'keyword, 'converter, C: Copy, W: StreamConverter_Char<C>, I: Keyword<W::WC
fn startComparation<'self_>(
&'self_ self,
expectedLen: usize,
) -> Option<impl KeywordComparatorIterator<'self_, C>> {
) -> Option<impl KeywordComparatorIterator<C>> {
return self
._orig
.startComparation(expectedLen)

View File

@ -3,7 +3,7 @@ use crate::pos::Pos;
pub trait StreamConverter_Char_Noop<C: Copy> {}
pub trait StreamConverter_Pos_Noop<'pos, P: Pos<'pos>> {}
pub trait StreamConverter_Pos_Noop<P: Pos> {}
/*
pub trait StreamConverter_Substring_Noop<'source, C: Copy, CS: CollectedSubstring<'source, C = C>>:
@ -20,7 +20,7 @@ impl<C: Copy, W: StreamConverter_Char_Noop<C>> StreamConverter_Char<C> for W {
}
}
impl<'pos, P: Pos<'pos>, W: StreamConverter_Pos_Noop<'pos, P>> StreamConverter_Pos<'pos, P> for W {
impl<P: Pos, W: StreamConverter_Pos_Noop<P>> StreamConverter_Pos<P> for W {
type WP = P;
fn convertPos(&self, p: P) -> Self::WP {

View File

@ -1,57 +1,48 @@
use std::marker::PhantomData;
use crate::{CollectedSubstring, Predicate, SourceStream};
use crate::converter::{PredicateConverter, StreamConverter};
use crate::pos::Pos;
use crate::{CollectedSubstring, Predicate, SourceStream};
pub struct ConvertedSourceStream<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
P: Pos,
CS: CollectedSubstring<C = C>,
W: StreamConverter<C, P, CS>,
I: SourceStream<C = C, P = P, CS = CS>,
> {
_src: I,
_converter: W,
__phantom: PhantomData<(&'source (), &'pos (), W)>,
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> ConvertedSourceStream<'source, 'pos, C, P, CS, W, I>
P: Pos,
CS: CollectedSubstring<C = C>,
W: StreamConverter<C, P, CS>,
I: SourceStream<C = C, P = P, CS = CS>,
> ConvertedSourceStream<C, P, CS, W, I>
{
pub fn convert(stream: I, converter: W) -> Self {
return ConvertedSourceStream {
_src: stream,
_converter: converter,
__phantom: PhantomData::default(),
};
}
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C = C>,
W: StreamConverter<'source, 'pos, C, P, CS>,
I: SourceStream<'source, 'pos, C = C, P = P, CS = CS>,
> SourceStream<'source, 'pos> for ConvertedSourceStream<'source, 'pos, C, P, CS, W, I>
P: Pos,
CS: CollectedSubstring<C = C>,
W: StreamConverter<C, P, CS>,
I: SourceStream<C = C, P = P, CS = CS>,
> SourceStream for ConvertedSourceStream<C, P, CS, W, I>
{
type C = W::WC;
type P = W::WP;
type CS = W::WCS;
fn skipNext(&mut self, predicate: &mut impl Predicate<Self::C>) -> Option<Self::C> {
return self
return self
._src
.skipNext(&mut PredicateConverter::wrap(predicate, &self._converter))
.map(|c| self._converter.convertChar(c));
@ -75,4 +66,4 @@ impl<
fn nextChar(&mut self) -> Option<W::WC> {
return self._src.nextChar().map(|c| self._converter.convertChar(c));
}
}
}

View File

@ -1,16 +1,16 @@
use crate::KeywordComparatorIterator;
use std::marker::PhantomData;
use std::slice::Iter;
use crate::{CollectedSubstring, Keyword, Predicate, SourceStream};
use crate::pos::PosCounter;
use crate::{CollectedSubstring, Keyword, Predicate, SourceStream};
use std::slice::Iter;
pub struct ArrayCollectedSubstring<'source, C> {
pub struct ArrayCollectedSubstring<'source, C: Copy> {
pub slice: &'source [C],
}
impl<'source, C: Copy> CollectedSubstring<'source> for ArrayCollectedSubstring<'source, C> {
impl<'source, C: Copy> CollectedSubstring for ArrayCollectedSubstring<'source, C> {
type C = C;
fn compareKeyword<'keyword>(&self, kw: impl Keyword<C>) -> bool {
fn compareKeyword(&self, kw: impl Keyword<C>) -> bool {
let mut kwi;
match kw.startComparation(self.slice.len()) {
None => return false,
@ -27,17 +27,16 @@ impl<'source, C: Copy> CollectedSubstring<'source> for ArrayCollectedSubstring<'
}
}
pub struct ArraySourceStream<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> {
pub struct ArraySourceStream<'source, C: Copy, PC: PosCounter<C>> {
_source: &'source [C],
__iter: Iter<'source, C>,
_current: C,
_posRaw: usize,
_posHighlevel: PC,
_isEnded: bool,
__phantom: PhantomData<&'pos ()>,
}
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> ArraySourceStream<'source, 'pos, C, PC> {
impl<'source, C: Copy, PC: PosCounter<C>> ArraySourceStream<'source, C, PC> {
pub fn start(arr: &'source [C], pos: PC) -> Option<(Self, C)> {
let mut iter = arr.iter();
let first = iter.next().copied();
@ -51,7 +50,6 @@ impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> ArraySourceStream<'source,
_posRaw: 0,
_posHighlevel: pos,
_isEnded: false,
__phantom: PhantomData::default(),
};
return Some((stream, c));
}
@ -59,7 +57,7 @@ impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> ArraySourceStream<'source,
}
}
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> ArraySourceStream<'source, 'pos, C, PC> {
impl<'source, 'pos, C: Copy, PC: PosCounter<C>> ArraySourceStream<'source, C, PC> {
fn _next(&mut self) -> Option<C> {
if !self._isEnded {
self._posHighlevel.update(self._current);
@ -76,9 +74,7 @@ impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> ArraySourceStream<'source,
}
}
impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> SourceStream<'source, 'pos>
for ArraySourceStream<'source, 'pos, C, PC>
{
impl<'source, C: Copy, PC: PosCounter<C>> SourceStream for ArraySourceStream<'source, C, PC> {
type C = C;
type P = PC::P;
type CS = ArrayCollectedSubstring<'source, C>;

View File

@ -1,47 +1,24 @@
use std::marker::PhantomData;
use crate::pos::{PosCounter};
use crate::{CollectedSubstring, Predicate, SourceStream};
use crate::pos::{Pos, PosCounter};
pub struct EmptySourceStream<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C=C>,
PC: PosCounter<'pos, C, P=P>,
> {
pub struct EmptySourceStream<C: Copy, CS: CollectedSubstring<C = C>, PC: PosCounter<C>> {
_pos: PC,
__phantom: PhantomData<(&'source (), &'pos (), P, CS)>,
__phantom: PhantomData<(C, CS)>
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C=C>,
PC: PosCounter<'pos, C, P=P>,
> EmptySourceStream<'source, 'pos, C, P, CS, PC>
{
impl<C: Copy, CS: CollectedSubstring<C = C>, PC: PosCounter<C>> EmptySourceStream<C, CS, PC> {
pub fn start(pos: PC) -> (Self, Option<C>) {
let stream = EmptySourceStream {
_pos: pos,
__phantom: PhantomData::default()
};
let stream = EmptySourceStream { _pos: pos, __phantom: PhantomData::default() };
return (stream, None);
}
}
impl<
'source,
'pos,
C: Copy,
P: Pos<'pos>,
CS: CollectedSubstring<'source, C=C>,
PC: PosCounter<'pos, C, P=P>,
> SourceStream<'source, 'pos> for EmptySourceStream<'source, 'pos, C, P, CS, PC>
impl<C: Copy, CS: CollectedSubstring<C = C>, PC: PosCounter<C>> SourceStream
for EmptySourceStream<C, CS, PC>
{
type C = C;
type P = P;
type P = PC::P;
type CS = CS;
fn skipNext(&mut self, _: &mut impl Predicate<Self::C>) -> Option<Self::C> {

View File

@ -1,18 +1,17 @@
use crate::KeywordComparatorIterator;
use std::marker::PhantomData;
use std::str::CharIndices;
use crate::{CollectedSubstring, Keyword, Predicate, SourceStream};
use crate::pos::PosCounter;
use crate::{CollectedSubstring, Keyword, Predicate, SourceStream};
use std::str::CharIndices;
pub struct StrCollectedSubstring<'source> {
pub slice: &'source str,
pub size: usize,
}
impl<'source> CollectedSubstring<'source> for StrCollectedSubstring<'source> {
impl<'source> CollectedSubstring for StrCollectedSubstring<'source> {
type C = char;
fn compareKeyword<'keyword>(&self, kw: impl Keyword<char>) -> bool {
fn compareKeyword(&self, kw: impl Keyword<char>) -> bool {
let mut kwi;
match kw.startComparation(self.size) {
None => return false,
@ -29,7 +28,7 @@ impl<'source> CollectedSubstring<'source> for StrCollectedSubstring<'source> {
}
}
pub struct StrSourceStream<'source, 'pos, PC: PosCounter<'pos, char>> {
pub struct StrSourceStream<'source, PC: PosCounter<char>> {
_source: &'source str,
__iter: CharIndices<'source>,
_current: char,
@ -37,10 +36,9 @@ pub struct StrSourceStream<'source, 'pos, PC: PosCounter<'pos, char>> {
_posCodePoints: usize,
_posHighlevel: PC,
_isEnded: bool,
__phantom: PhantomData<&'pos ()>,
}
impl<'source, 'pos, PC: PosCounter<'pos, char>> StrSourceStream<'source, 'pos, PC> {
impl<'source, PC: PosCounter<char>> StrSourceStream<'source, PC> {
pub fn start(s: &'source str, pos: PC) -> Option<(Self, char)> {
let mut it = s.char_indices();
let first = it.next();
@ -55,7 +53,6 @@ impl<'source, 'pos, PC: PosCounter<'pos, char>> StrSourceStream<'source, 'pos, P
_posCodePoints: 0,
_posHighlevel: pos,
_isEnded: false,
__phantom: PhantomData::default(),
};
return Some((stream, c));
}
@ -63,7 +60,7 @@ impl<'source, 'pos, PC: PosCounter<'pos, char>> StrSourceStream<'source, 'pos, P
}
}
impl<'source, 'pos, PC: PosCounter<'pos, char>> StrSourceStream<'source, 'pos, PC> {
impl<'source, PC: PosCounter<char>> StrSourceStream<'source, PC> {
fn _next(&mut self) -> Option<char> {
if !self._isEnded {
self._posHighlevel.update(self._current);
@ -81,9 +78,7 @@ impl<'source, 'pos, PC: PosCounter<'pos, char>> StrSourceStream<'source, 'pos, P
}
}
impl<'source, 'pos, PC: PosCounter<'pos, char>> SourceStream<'source, 'pos>
for StrSourceStream<'source, 'pos, PC>
{
impl<'source, PC: PosCounter<char>> SourceStream for StrSourceStream<'source, PC> {
type C = char;
type P = PC::P;
type CS = StrCollectedSubstring<'source>;

View File

@ -10,7 +10,7 @@ impl Default for IndexPosCounter {
}
}
impl<C: Copy> PosCounter<'static, C> for IndexPosCounter {
impl<C: Copy> PosCounter<C> for IndexPosCounter {
type P = usize;
fn update(&mut self, _: C) {

View File

@ -5,7 +5,7 @@ pub struct PosLineCol {
pub col: usize,
}
impl Pos<'static> for PosLineCol {}
impl Pos for PosLineCol {}
pub struct NewLinePosCounter {
row: usize,
@ -13,7 +13,7 @@ pub struct NewLinePosCounter {
}
impl NewLinePosCounter {
pub fn _update<C: PartialEq>(&mut self, actual: C, expected: C) {
pub fn _update<C: Copy + PartialEq>(&mut self, actual: C, expected: C) {
if actual == expected {
self.row += 1;
self.col = 0;
@ -36,7 +36,7 @@ impl Default for NewLinePosCounter {
}
}
impl PosCounter<'static, char> for NewLinePosCounter {
impl PosCounter<char> for NewLinePosCounter {
type P = PosLineCol;
fn update(&mut self, c: char) {
@ -49,7 +49,7 @@ impl PosCounter<'static, char> for NewLinePosCounter {
}
impl PosCounter<'static, u8> for NewLinePosCounter {
impl PosCounter<u8> for NewLinePosCounter {
type P = PosLineCol;
fn update(&mut self, c: u8) {

View File

@ -8,7 +8,7 @@ impl Default for NoopPosCounter {
}
}
impl<C: Copy> PosCounter<'static, C> for NoopPosCounter {
impl<C: Copy> PosCounter<C> for NoopPosCounter {
type P = ();
fn update(&mut self, _: C) {}

View File

@ -1,4 +1,4 @@
pub trait Pos<'pos> {}
pub trait Pos {}
impl Pos<'static> for () {}
impl Pos<'static> for usize {}
impl Pos for () {}
impl Pos for usize {}

View File

@ -1,10 +1,9 @@
use crate::pos::Pos;
pub trait PosCounter<'pos, C: Copy> {
type P: Pos<'pos>;
pub trait PosCounter<C: Copy> {
type P: Pos;
fn update(&mut self, c: C);
fn export(&self) -> Self::P;
}
}

View File

@ -5,10 +5,10 @@ pub trait Predicate<C: Copy> {
fn check(&mut self, chr: C) -> bool;
}
pub trait SourceStream<'source, 'pos> {
pub trait SourceStream {
type C: Copy;
type P: Pos<'pos>;
type CS: CollectedSubstring<'source, C = Self::C>;
type P: Pos;
type CS: CollectedSubstring<C = Self::C>;
fn skipNext(&mut self, predicate: &mut impl Predicate<Self::C>) -> Option<Self::C>;