summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md6
-rw-r--r--bzipper/Cargo.toml4
-rw-r--r--bzipper/src/lib.rs2
-rw-r--r--bzipper/src/sized_iter/mod.rs58
-rw-r--r--bzipper/src/sized_iter/test.rs44
-rw-r--r--bzipper/src/sized_slice/test.rs2
-rw-r--r--bzipper_benchmarks/Cargo.toml4
-rw-r--r--bzipper_macros/Cargo.toml2
8 files changed, 87 insertions, 35 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a85644f..ed93051 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,12 @@
This is the changelog of bzipper.
See `README.md` for more information.
+## 0.10.1
+
+* Clean up and refactor code
+* Add more tests
+* Fix `DoubleEndedIterator` implementation for `SizedIter`
+
## 0.10.0
* Clean up code
diff --git a/bzipper/Cargo.toml b/bzipper/Cargo.toml
index a263a32..909d9c4 100644
--- a/bzipper/Cargo.toml
+++ b/bzipper/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "bzipper"
-version = "0.10.0"
+version = "0.10.1"
edition = "2021"
rust-version = "1.83"
documentation = "https://docs.rs/bzipper/"
@@ -24,7 +24,7 @@ alloc = []
std = []
[dependencies]
-bzipper_macros = { path = "../bzipper_macros", version = "0.10.0" }
+bzipper_macros = { path = "../bzipper_macros", version = "0.10.1" }
[lints]
workspace = true
diff --git a/bzipper/src/lib.rs b/bzipper/src/lib.rs
index 525464c..b57732c 100644
--- a/bzipper/src/lib.rs
+++ b/bzipper/src/lib.rs
@@ -436,4 +436,4 @@ use_mod!(pub sized_slice);
#[cfg(feature = "alloc")]
use_mod!(pub buf);
-pub mod error; \ No newline at end of file
+pub mod error;
diff --git a/bzipper/src/sized_iter/mod.rs b/bzipper/src/sized_iter/mod.rs
index 7612907..ae5efc4 100644
--- a/bzipper/src/sized_iter/mod.rs
+++ b/bzipper/src/sized_iter/mod.rs
@@ -19,6 +19,9 @@
// er General Public License along with bZipper. If
// not, see <https://www.gnu.org/licenses/>.
+#[cfg(test)]
+mod test;
+
use core::iter::{DoubleEndedIterator, ExactSizeIterator, FusedIterator};
use core::mem::MaybeUninit;
use core::slice;
@@ -85,26 +88,25 @@ impl<T, const N: usize> AsRef<[T]> for SizedIter<T, N> {
impl<T: Clone, const N: usize> Clone for SizedIter<T, N> {
#[inline]
fn clone(&self) -> Self {
- unsafe {
- let mut buf: [MaybeUninit<T>; N] = MaybeUninit::uninit().assume_init();
- let Self { pos, len, .. } = *self;
+ let mut buf: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() };
+ let Self { pos, len, .. } = *self;
+
+ let start = pos;
+ let stop = start + len;
- let start = pos;
- let stop = start.unchecked_add(len);
+ for i in start..stop {
+ unsafe {
+ let item = (&raw const *self.buf.get_unchecked(i)).cast();
- for i in start..stop {
- let value = &*self.buf
- .as_ptr()
- .add(i)
- .cast::<T>();
+ let value = Clone::clone(&*item);
buf
.get_unchecked_mut(i)
- .write(value.clone());
+ .write(value);
}
-
- Self { buf, pos, len }
}
+
+ Self { buf, pos, len }
}
}
@@ -113,17 +115,17 @@ impl<T, const N: usize> DoubleEndedIterator for SizedIter<T, N> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.len == 0x0 { return None };
- unsafe {
- let index = self.pos.unchecked_add(self.len);
+ let index = self.pos + self.len - 0x1;
- let item = self.buf
+ let item = unsafe {
+ self.buf
.get_unchecked(index)
- .assume_init_read();
+ .assume_init_read()
+ };
- self.len = self.len.unchecked_sub(0x1);
+ self.len -= 0x1;
- Some(item)
- }
+ Some(item)
}
}
@@ -138,18 +140,18 @@ impl<T, const N: usize> Iterator for SizedIter<T, N> {
fn next(&mut self) -> Option<Self::Item> {
if self.len == 0x0 { return None };
- unsafe {
- let index = self.pos;
+ let index = self.pos;
- let item = self.buf
+ let item = unsafe {
+ self.buf
.get_unchecked(index)
- .assume_init_read();
+ .assume_init_read()
+ };
- self.pos = self.pos.unchecked_add(0x1);
- self.len = self.len.unchecked_sub(0x1);
+ self.len -= 0x1;
+ self.pos += 0x1;
- Some(item)
- }
+ Some(item)
}
#[inline(always)]
diff --git a/bzipper/src/sized_iter/test.rs b/bzipper/src/sized_iter/test.rs
new file mode 100644
index 0000000..d99a508
--- /dev/null
+++ b/bzipper/src/sized_iter/test.rs
@@ -0,0 +1,44 @@
+// Copyright 2024 Gabriel Bjørnager Jensen.
+//
+// This file is part of bZipper.
+//
+// bZipper is free software: you can redistribute
+// it and/or modify it under the terms of the GNU
+// Lesser General Public License as published by
+// the Free Software Foundation, either version 3
+// of the License, or (at your option) any later
+// version.
+//
+// bZipper is distributed in the hope that it will
+// be useful, but WITHOUT ANY WARRANTY; without
+// even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Less-
+// er General Public License along with bZipper. If
+// not, see <https://www.gnu.org/licenses/>.
+
+use bzipper::SizedSlice;
+
+#[test]
+fn test_sized_iter_double_ended() {
+ let data = SizedSlice::from([
+ 'H', 'E', 'L', 'L', 'O', ' ', 'W', 'O',
+ 'R', 'L', 'D',
+ ]);
+
+ let mut data = data.into_iter();
+
+ assert_eq!(data.next(), Some('H'));
+ assert_eq!(data.next_back(), Some('D'));
+ assert_eq!(data.next(), Some('E'));
+ assert_eq!(data.next_back(), Some('L'));
+ assert_eq!(data.next(), Some('L'));
+ assert_eq!(data.next_back(), Some('R'));
+ assert_eq!(data.next(), Some('L'));
+ assert_eq!(data.next_back(), Some('O'));
+ assert_eq!(data.next(), Some('O'));
+ assert_eq!(data.next_back(), Some('W'));
+ assert_eq!(data.next(), Some(' '));
+}
diff --git a/bzipper/src/sized_slice/test.rs b/bzipper/src/sized_slice/test.rs
index 8fb5065..317b1a4 100644
--- a/bzipper/src/sized_slice/test.rs
+++ b/bzipper/src/sized_slice/test.rs
@@ -23,7 +23,7 @@ use alloc::vec::Vec;
use bzipper::SizedSlice;
#[test]
-fn test_fixed_vec_from_iter() {
+fn test_sized_slice_from_iter() {
let f = |x: u32| -> u32 {
let x = f64::from(x);
diff --git a/bzipper_benchmarks/Cargo.toml b/bzipper_benchmarks/Cargo.toml
index 0c01948..b94acb7 100644
--- a/bzipper_benchmarks/Cargo.toml
+++ b/bzipper_benchmarks/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "bzipper_benchmarks"
-version = "0.10.0"
+version = "0.10.1"
edition = "2021"
description = "bZipper benchmarks."
@@ -10,7 +10,7 @@ homepage.workspace = true
repository.workspace = true
[dependencies]
-bzipper = { path = "../bzipper", version = "0.10.0" }
+bzipper = { path = "../bzipper", version = "0.10.1" }
bincode = "1.3.3"
ciborium = "0.2.2"
diff --git a/bzipper_macros/Cargo.toml b/bzipper_macros/Cargo.toml
index 087ab4c..8731193 100644
--- a/bzipper_macros/Cargo.toml
+++ b/bzipper_macros/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "bzipper_macros"
-version = "0.10.0"
+version = "0.10.1"
edition = "2021"
documentation = "https://docs.rs/bzipper_macros/"