Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/buf/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::buf::{IntoIter, UninitSlice};
use crate::{Buf, BufMut};
use crate::{Buf, BufMut, Cursor, CursorMut};

#[cfg(feature = "std")]
use std::io::IoSlice;
Expand Down Expand Up @@ -226,6 +226,33 @@ where
}
}

impl<T: Cursor, U: Cursor> Cursor for Chain<T, U> {
type Cursor<'a>
= Chain<T::Cursor<'a>, U::Cursor<'a>>
where
Self: 'a;

fn cursor(&self, index: usize) -> Self::Cursor<'_> {
let mut chain = self.a.cursor(0).chain(self.b.cursor(0));
chain.advance(index);
chain
}
}

unsafe impl<T: CursorMut, U: CursorMut> CursorMut for Chain<T, U> {
type CursorMut<'a>
= Chain<T::CursorMut<'a>, U::CursorMut<'a>>
where
Self: 'a;

fn cursor_mut(&mut self, index: usize) -> Self::CursorMut<'_> {
let mut chain = self.a.cursor_mut(0).chain_mut(self.b.cursor_mut(0));
// SAFETY: advance_mut must be safe on CursorMut::CursorMut
unsafe { chain.advance_mut(index) };
chain
}
}

impl<T, U> IntoIterator for Chain<T, U>
where
T: Buf,
Expand Down
35 changes: 35 additions & 0 deletions src/buf/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::{buf::Chain, Buf};
use std::collections::VecDeque;

pub trait Cursor: Buf {
type Cursor<'a>: Buf
where
Self: 'a;

fn cursor(&self, index: usize) -> Self::Cursor<'_>;
}

impl Cursor for &[u8] {
type Cursor<'a>
= &'a [u8]
where
Self: 'a;

fn cursor(&self, index: usize) -> Self::Cursor<'_> {
&self[index..]
}
}

impl Cursor for VecDeque<u8> {
type Cursor<'a>
= Chain<&'a [u8], &'a [u8]>
where
Self: 'a;

fn cursor(&self, index: usize) -> Self::Cursor<'_> {
let (s1, s2) = self.as_slices();
let mut chain = s1.chain(s2);
chain.advance(index);
chain
}
}
26 changes: 26 additions & 0 deletions src/buf/cursor_mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::BufMut;

/// # Safety
///
/// It must be safe to call Self::CursorMut::advance_mut. This should be true
/// if the type Self::CursorMut does not allow to access bytes writen to it.
/// For exemple, Vec<u8> is not ok, as the content written to it is accesible.
/// On the other hand &mut [u8] is ok, as the content written cant't be accessed.
pub unsafe trait CursorMut: BufMut {
type CursorMut<'a>: BufMut
where
Self: 'a;

fn cursor_mut(&mut self, index: usize) -> Self::CursorMut<'_>;
}

unsafe impl CursorMut for &mut [u8] {
type CursorMut<'a>
= &'a mut [u8]
where
Self: 'a;

fn cursor_mut(&mut self, index: usize) -> Self::CursorMut<'_> {
&mut self[index..]
}
}
12 changes: 12 additions & 0 deletions src/buf/limit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::buf::UninitSlice;
use crate::BufMut;
use crate::CursorMut;

use core::cmp;

Expand Down Expand Up @@ -73,3 +74,14 @@ unsafe impl<T: BufMut> BufMut for Limit<T> {
self.limit -= cnt;
}
}

unsafe impl<T: CursorMut> CursorMut for Limit<T> {
type CursorMut<'a>
= Limit<T::CursorMut<'a>>
where
Self: 'a;

fn cursor_mut(&mut self, index: usize) -> Self::CursorMut<'_> {
self.inner.cursor_mut(index).limit(self.limit - index)
}
}
4 changes: 4 additions & 0 deletions src/buf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
mod buf_impl;
mod buf_mut;
mod chain;
mod cursor;
mod cursor_mut;
mod iter;
mod limit;
#[cfg(feature = "std")]
Expand All @@ -30,6 +32,8 @@ mod writer;
pub use self::buf_impl::Buf;
pub use self::buf_mut::BufMut;
pub use self::chain::Chain;
pub use self::cursor::Cursor;
pub use self::cursor_mut::CursorMut;
pub use self::iter::IntoIter;
pub use self::limit::Limit;
pub use self::take::Take;
Expand Down
13 changes: 12 additions & 1 deletion src/buf/take.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Buf;
use crate::{Buf, Cursor};

use core::cmp;

Expand Down Expand Up @@ -185,3 +185,14 @@ impl<T: Buf> Buf for Take<T> {
cnt
}
}

impl<T: Cursor> Cursor for Take<T> {
type Cursor<'a>
= Take<T::Cursor<'a>>
where
Self: 'a;

fn cursor(&self, index: usize) -> Self::Cursor<'_> {
self.inner.cursor(index).take(self.limit - index)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extern crate alloc;
extern crate std;

pub mod buf;
pub use crate::buf::{Buf, BufMut};
pub use crate::buf::{Buf, BufMut, Cursor, CursorMut};

mod bytes;
mod bytes_mut;
Expand Down