Skip to content

Commit

Permalink
WIP: FlexVec iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
agerasev committed Jul 24, 2024
1 parent 1473990 commit bed059b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 193 deletions.
48 changes: 40 additions & 8 deletions base/src/utils/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,45 @@ impl<'a> Data<'a> for &'a mut [u8] {
}

#[derive(Clone, Debug)]
pub struct RefData<'a>(&'a [u8]);
impl<'a> RefData<'a> {
pub struct RefData<'a>(pub &'a [u8]);
impl<'a> Data<'a> for RefData<'a> {
fn bytes(&self) -> &'_ [u8] {
self.0.bytes()
}
type Output<T: Flat + ?Sized + 'a> = Result<&'a T, Error>;
fn split(self, pos: usize) -> (Self, Self) {
let (a, b) = Data::split(self.0, pos);
(Self(a), Self(b))
}
fn value<T: Flat + ?Sized + 'a>(self) -> Self::Output<T> {
T::from_bytes(self.0.value::<T>())
}
}

#[derive(Debug)]
pub struct MutData<'a>(pub &'a mut [u8]);
impl<'a> Data<'a> for MutData<'a> {
fn bytes(&self) -> &'_ [u8] {
self.0.bytes()
}
type Output<T: Flat + ?Sized + 'a> = Result<&'a mut T, Error>;
fn split(self, pos: usize) -> (Self, Self) {
let (a, b) = Data::split(self.0, pos);
(Self(a), Self(b))
}
fn value<T: Flat + ?Sized + 'a>(self) -> Self::Output<T> {
T::from_mut_bytes(self.0.value::<T>())
}
}

#[derive(Clone, Debug)]
pub struct UncheckedRefData<'a>(&'a [u8]);
impl<'a> UncheckedRefData<'a> {
pub unsafe fn new(data: &'a [u8]) -> Self {
Self(data)
}
}
impl<'a> Data<'a> for RefData<'a> {
impl<'a> Data<'a> for UncheckedRefData<'a> {
fn bytes(&self) -> &'_ [u8] {
self.0.bytes()
}
Expand All @@ -168,13 +200,13 @@ impl<'a> Data<'a> for RefData<'a> {
}

#[derive(Debug)]
pub struct MutData<'a>(&'a mut [u8]);
impl<'a> MutData<'a> {
pub struct UncheckedMutData<'a>(&'a mut [u8]);
impl<'a> UncheckedMutData<'a> {
pub unsafe fn new(data: &'a mut [u8]) -> Self {
Self(data)
}
}
impl<'a> Data<'a> for MutData<'a> {
impl<'a> Data<'a> for UncheckedMutData<'a> {
fn bytes(&self) -> &'_ [u8] {
self.0.bytes()
}
Expand Down Expand Up @@ -252,8 +284,8 @@ impl<'a, D: Data<'a>, T: Flat + ?Sized> DataIter<'a, D, SingleType<T>> {

pub type BytesIter<'a, I> = DataIter<'a, &'a [u8], I>;
pub type BytesMutIter<'a, I> = DataIter<'a, &'a mut [u8], I>;
pub type RefIter<'a, I> = DataIter<'a, RefData<'a>, I>;
pub type MutIter<'a, I> = DataIter<'a, MutData<'a>, I>;
pub type RefIter<'a, I> = DataIter<'a, UncheckedRefData<'a>, I>;
pub type MutIter<'a, I> = DataIter<'a, UncheckedMutData<'a>, I>;

pub trait ValidateIter {
fn validate_all(self) -> Result<(), Error>;
Expand Down
1 change: 1 addition & 0 deletions containers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ flatty-base.workspace = true
stavec = { path = "../../stavec", version = "0.4.0", default-features = false, features = [
"repr-c",
] }
take_mut = "0.2.2"
Loading

0 comments on commit bed059b

Please sign in to comment.