Skip to content

Commit

Permalink
feat(stream): Allow offsets with checkpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 26, 2023
1 parent 4593ccd commit 8b3a1bc
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,27 +1245,58 @@ impl<'a, T> Offset for &'a [T] {
}
}

impl<'a, T> Offset<<&'a [T] as Stream>::Checkpoint> for &'a [T]
where
T: Clone + crate::lib::std::fmt::Debug,
{
#[inline(always)]
fn offset_from(&self, other: &<&'a [T] as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<'a> Offset for &'a str {
#[inline(always)]
fn offset_from(&self, start: &Self) -> usize {
self.as_bytes().offset_from(&start.as_bytes())
}
}

impl<'a> Offset<<&'a str as Stream>::Checkpoint> for &'a str {
#[inline(always)]
fn offset_from(&self, other: &<&'a str as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<'a> Offset for &'a Bytes {
#[inline(always)]
fn offset_from(&self, start: &Self) -> usize {
self.as_bytes().offset_from(&start.as_bytes())
}
}

impl<'a> Offset<<&'a Bytes as Stream>::Checkpoint> for &'a Bytes {
#[inline(always)]
fn offset_from(&self, other: &<&'a Bytes as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<'a> Offset for &'a BStr {
#[inline(always)]
fn offset_from(&self, start: &Self) -> usize {
self.as_bytes().offset_from(&start.as_bytes())
}
}

impl<'a> Offset<<&'a BStr as Stream>::Checkpoint> for &'a BStr {
#[inline(always)]
fn offset_from(&self, other: &<&'a BStr as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<I> Offset for (I, usize)
where
I: Offset,
Expand All @@ -1276,6 +1307,16 @@ where
}
}

impl<I> Offset<<(I, usize) as Stream>::Checkpoint> for (I, usize)
where
I: Stream<Token = u8> + Offset,
{
#[inline(always)]
fn offset_from(&self, other: &<(I, usize) as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<I> Offset for Located<I>
where
I: Offset,
Expand All @@ -1286,6 +1327,16 @@ where
}
}

impl<I> Offset<<Located<I> as Stream>::Checkpoint> for Located<I>
where
I: Stream + Offset,
{
#[inline(always)]
fn offset_from(&self, other: &<Located<I> as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<I, S> Offset for Stateful<I, S>
where
I: Offset,
Expand All @@ -1296,6 +1347,17 @@ where
}
}

impl<I, S> Offset<<Stateful<I, S> as Stream>::Checkpoint> for Stateful<I, S>
where
I: Stream + Offset,
S: Clone + crate::lib::std::fmt::Debug,
{
#[inline(always)]
fn offset_from(&self, other: &<Stateful<I, S> as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<I> Offset for Partial<I>
where
I: Offset,
Expand All @@ -1306,6 +1368,16 @@ where
}
}

impl<I> Offset<<Partial<I> as Stream>::Checkpoint> for Partial<I>
where
I: Stream + Offset,
{
#[inline(always)]
fn offset_from(&self, other: &<Partial<I> as Stream>::Checkpoint) -> usize {
self.checkpoint().offset_from(other)
}
}

impl<I> Offset for Checkpoint<I>
where
I: Offset,
Expand Down

0 comments on commit 8b3a1bc

Please sign in to comment.