From 41dfb7f4321974607bfe6fe83870205051266d80 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Sun, 16 Nov 2025 22:29:00 +0300 Subject: [PATCH] Improved pos counter --- default-streams/src/_sandbox.rs | 9 ++++----- default-streams/src/iterator.rs | 9 +++------ default-streams/src/iterators/array.rs | 10 +++++----- default-streams/src/iterators/str.rs | 10 +++++----- default-streams/src/pos/index.rs | 17 ++++++++--------- default-streams/src/pos/newline.rs | 26 ++++++++++---------------- default-streams/src/pos/nop.rs | 18 +++++++----------- 7 files changed, 42 insertions(+), 57 deletions(-) diff --git a/default-streams/src/_sandbox.rs b/default-streams/src/_sandbox.rs index d3a6886..3bede8a 100644 --- a/default-streams/src/_sandbox.rs +++ b/default-streams/src/_sandbox.rs @@ -1,11 +1,11 @@ -/*use source_stream_0::{CollectedSubstring, Pos, SourceStream}; -use crate::iterators::StrSourceIterator; +use source_stream_0::{CollectedSubstring, Pos, SourceStream}; +use crate::iterators::{ArraySourceIterator, StrSourceIterator}; use crate::pos::{IndexPosCounter, NewLinePosCounter, PosLineCol}; -use crate::{_CollectScopeContext, SourceIterator}; +use crate::SourceIterator; #[test] fn sandbox() { - let mut z = StrSourceIterator::start("12\n34"); + let mut z = ArraySourceIterator::start(b"12\n34", IndexPosCounter::default()); println!("{}", z.pos()); z.next(); println!("{} ", z.pos()); @@ -30,4 +30,3 @@ fn sandbox() { return; } -*/ \ No newline at end of file diff --git a/default-streams/src/iterator.rs b/default-streams/src/iterator.rs index 507f0eb..820e9e5 100644 --- a/default-streams/src/iterator.rs +++ b/default-streams/src/iterator.rs @@ -1,15 +1,12 @@ use source_stream_0::{CollectResult, CollectedSubstring, Pos}; use std::marker::PhantomData; -pub trait PosCounter<'pos> { - type C; +pub trait PosCounter<'pos, C> { type P: Pos<'pos>; - fn update(&mut self, c: Self::C); - + fn update(&mut self, c: C); + fn export(&self) -> Self::P; - - fn init() -> Self; } pub trait SourceIterator<'source, 'pos> { diff --git a/default-streams/src/iterators/array.rs b/default-streams/src/iterators/array.rs index 6f30fba..e1bff06 100644 --- a/default-streams/src/iterators/array.rs +++ b/default-streams/src/iterators/array.rs @@ -27,7 +27,7 @@ impl<'source, C: Copy> CollectedSubstring<'source> for ArrayCollectedSubstring<' } } -pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos>> { +pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> { _source: &'source [C], _iter: Iter<'source, C>, _current: Option<&'source C>, @@ -36,8 +36,8 @@ pub struct ArraySourceIterator<'source, 'pos, C: Copy, PC: PosCounter<'pos>> { __phantom: PhantomData<&'pos ()>, } -impl<'source, 'pos, C: Copy, PC: PosCounter<'pos>> ArraySourceIterator<'source, 'pos, C, PC> { - pub fn start(arr: &'source [C]) -> ArraySourceIterator<'source, 'pos, C, PC> { +impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> ArraySourceIterator<'source, 'pos, C, PC> { + pub fn start(arr: &'source [C], pos: PC) -> ArraySourceIterator<'source, 'pos, C, PC> { let mut it = arr.iter(); let first = it.next(); return ArraySourceIterator { @@ -45,13 +45,13 @@ impl<'source, 'pos, C: Copy, PC: PosCounter<'pos>> ArraySourceIterator<'source, _iter: it, _current: first, _posRaw: 0, - _posHighlevel: PC::init(), + _posHighlevel: pos, __phantom: PhantomData::default(), }; } } -impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C = C>> SourceIterator<'source, 'pos> +impl<'source, 'pos, C: Copy, PC: PosCounter<'pos, C>> SourceIterator<'source, 'pos> for ArraySourceIterator<'source, 'pos, C, PC> { type C = C; diff --git a/default-streams/src/iterators/str.rs b/default-streams/src/iterators/str.rs index a443952..c3ae85e 100644 --- a/default-streams/src/iterators/str.rs +++ b/default-streams/src/iterators/str.rs @@ -29,7 +29,7 @@ impl<'source> CollectedSubstring<'source> for StrCollectedSubstring<'source> { } } -pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, C = char>> { +pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, char>> { _source: &'source str, _iter: CharIndices<'source>, _current: Option, @@ -39,8 +39,8 @@ pub struct StrSourceIterator<'source, 'pos, PC: PosCounter<'pos, C = char>> { __phantom: PhantomData<(&'pos ())>, } -impl<'source, 'pos, PC: PosCounter<'pos, C = char>> StrSourceIterator<'source, 'pos, PC> { - pub fn start(s: &'source str) -> Self { +impl<'source, 'pos, PC: PosCounter<'pos, char>> StrSourceIterator<'source, 'pos, PC> { + pub fn start(s: &'source str, pos: PC) -> Self { let mut it = s.char_indices(); let first = it.next(); return StrSourceIterator { @@ -49,13 +49,13 @@ impl<'source, 'pos, PC: PosCounter<'pos, C = char>> StrSourceIterator<'source, ' _current: first.map(|(_, c)| c), _posRaw: first.map_or(s.len(), |(p, _)| p), _posCodePoints: 0, - _posHighlevel: PC::init(), + _posHighlevel: pos, __phantom: PhantomData::default(), }; } } -impl<'source, 'pos, PC: PosCounter<'pos, C = char>> SourceIterator<'source, 'pos> +impl<'source, 'pos, PC: PosCounter<'pos, char>> SourceIterator<'source, 'pos> for StrSourceIterator<'source, 'pos, PC> { type C = char; diff --git a/default-streams/src/pos/index.rs b/default-streams/src/pos/index.rs index 55dd232..4c7bc5b 100644 --- a/default-streams/src/pos/index.rs +++ b/default-streams/src/pos/index.rs @@ -1,13 +1,16 @@ -use std::marker::PhantomData; use crate::iterator::PosCounter; -pub struct IndexPosCounter { +pub struct IndexPosCounter { index: usize, - __phantom: PhantomData } -impl PosCounter<'static> for IndexPosCounter { - type C = C; +impl Default for IndexPosCounter { + fn default() -> Self { + return IndexPosCounter { index: 0 }; + } +} + +impl PosCounter<'static, C> for IndexPosCounter { type P = usize; fn update(&mut self, c: C) { @@ -17,8 +20,4 @@ impl PosCounter<'static> for IndexPosCounter { fn export(&self) -> usize { return self.index; } - - fn init() -> Self { - return IndexPosCounter { index: 0, __phantom: PhantomData::default() }; - } } diff --git a/default-streams/src/pos/newline.rs b/default-streams/src/pos/newline.rs index f78a722..6dc3cd7 100644 --- a/default-streams/src/pos/newline.rs +++ b/default-streams/src/pos/newline.rs @@ -1,6 +1,7 @@ use std::marker::PhantomData; use crate::iterator::PosCounter; use source_stream_0::Pos; +use crate::pos::IndexPosCounter; pub struct PosLineCol { pub row: usize, @@ -9,14 +10,13 @@ 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,14 +31,15 @@ impl NewLinePosCounter { col: self.col, }; } +} - pub fn _init() -> Self { - return NewLinePosCounter { row: 0, col: 0, __phantom: Default::default() }; +impl Default for NewLinePosCounter { + fn default() -> Self { + return NewLinePosCounter { row: 0, col: 0 }; } } -impl PosCounter<'static> for NewLinePosCounter { - type C = char; +impl PosCounter<'static, char> for NewLinePosCounter { type P = PosLineCol; fn update(&mut self, c: char) { @@ -49,13 +50,9 @@ impl PosCounter<'static> for NewLinePosCounter { self._export() } - fn init() -> Self { - return NewLinePosCounter::_init(); - } } -impl PosCounter<'static> for NewLinePosCounter { - type C = u8; +impl PosCounter<'static, u8> for NewLinePosCounter { type P = PosLineCol; fn update(&mut self, c: u8) { @@ -66,7 +63,4 @@ impl PosCounter<'static> for NewLinePosCounter { self._export() } - fn init() -> Self { - return NewLinePosCounter::_init(); - } } diff --git a/default-streams/src/pos/nop.rs b/default-streams/src/pos/nop.rs index 365c383..6aa58be 100644 --- a/default-streams/src/pos/nop.rs +++ b/default-streams/src/pos/nop.rs @@ -1,21 +1,17 @@ use crate::iterator::PosCounter; -use std::marker::PhantomData; +pub struct NopPosCounter {} -pub struct NopPosCounter { - __phantom: PhantomData, +impl Default for NopPosCounter { + fn default() -> Self { + return NopPosCounter {}; + } } -impl PosCounter<'static> for NopPosCounter { - type C = C; +impl PosCounter<'static, C> for NopPosCounter { type P = (); fn update(&mut self, c: C) {} fn export(&self) {} - - fn init() -> Self { - return NopPosCounter { - __phantom: PhantomData::default(), - }; - } } +