Skip to content
Merged
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
193 changes: 132 additions & 61 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion toad-array/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "toad-array"
version = "0.8.0"
edition = "2021"
rust-version = "1.70"
description = "Array trait used by the toad ecosystem to abstract over heap or heapless collections"
authors = ["Orion Kindel <cakekindel@gmail.com>"]
license = "MIT OR Apache-2.0"
Expand All @@ -22,5 +23,5 @@ test = []
docs = []

[dependencies]
tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]}
tinyvec = {version = "1.5", default_features = false, features = ["alloc", "rustc_1_55"]}
toad-len = {version = "^0.1.2", default_features = false}
60 changes: 20 additions & 40 deletions toad-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ use toad_len::Len;

/// Operations on ordered indexed collections
pub trait Indexed<T>
where
Self: Len + Deref<Target = [T]>,
where Self: Len + Deref<Target = [T]>
{
/// Insert a new element at `ix`, shifting all other elements to the right.
///
Expand Down Expand Up @@ -182,8 +181,7 @@ where
/// assert_eq!(v, vec![5]);
/// ```
fn drop_while<F>(&mut self, f: F)
where
F: for<'a> Fn(&'a T) -> bool,
where F: for<'a> Fn(&'a T) -> bool
{
match self.get(0) {
| Some(t) if !f(&t) => return,
Expand All @@ -202,8 +200,7 @@ where
/// - `Vec` is `Reserve`, and invokes `Vec::with_capacity`
/// - `tinyvec::ArrayVec` is `Reserve` and invokes `Default::default()` because creating an `ArrayVec` automatically allocates the required space on the stack.
pub trait Reserve
where
Self: Default,
where Self: Default
{
/// Create an instance of the collection with a given capacity.
///
Expand All @@ -221,8 +218,7 @@ where
///
/// If self was longer, drops elements up to `len`
pub trait Trunc
where
Self: Sized,
where Self: Sized
{
#[allow(missing_docs)]
fn trunc(&mut self, len: usize) -> ();
Expand All @@ -240,9 +236,7 @@ impl<T> Trunc for Vec<T> {
}
}

impl<T, const N: usize> Trunc for tinyvec::ArrayVec<[T; N]>
where
T: Default,
impl<T, const N: usize> Trunc for tinyvec::ArrayVec<[T; N]> where T: Default
{
fn trunc(&mut self, len: usize) -> () {
self.truncate(len)
Expand All @@ -257,24 +251,21 @@ where
pub trait Filled<T>: Sized {
#[allow(missing_docs)]
fn filled(t: T) -> Option<Self>
where
T: Copy,
where T: Copy
{
Self::filled_using(|| t)
}

#[allow(missing_docs)]
fn filled_default() -> Option<Self>
where
T: Default,
where T: Default
{
Self::filled_using(|| Default::default())
}

#[allow(missing_docs)]
fn filled_using<F>(f: F) -> Option<Self>
where
F: Fn() -> T;
where F: Fn() -> T;
}

#[cfg(feature = "alloc")]
Expand All @@ -287,29 +278,24 @@ impl<T> Reserve for Vec<T> {
#[cfg(feature = "alloc")]
impl<T> Filled<T> for Vec<T> {
fn filled_using<F>(_: F) -> Option<Self>
where
F: Fn() -> T,
where F: Fn() -> T
{
None
}
}

impl<A: tinyvec::Array> Reserve for tinyvec::ArrayVec<A> {}

impl<T, const N: usize> Filled<T> for tinyvec::ArrayVec<[T; N]>
where
T: Default,
impl<T, const N: usize> Filled<T> for tinyvec::ArrayVec<[T; N]> where T: Default
{
fn filled_using<F>(f: F) -> Option<Self>
where
F: Fn() -> T,
where F: Fn() -> T
{
Some(core::iter::repeat(()).take(N).map(|_| f()).collect())
}

fn filled(t: T) -> Option<Self>
where
T: Copy,
where T: Copy
{
Some(Self::from([t; N]))
}
Expand Down Expand Up @@ -361,8 +347,7 @@ pub trait Array:

/// Collections that support extending themselves mutably from copyable slices
pub trait AppendCopy<T>
where
T: Copy,
where T: Copy
{
/// Extend self mutably, copying from a slice.
///
Expand All @@ -375,19 +360,16 @@ where
}

#[cfg(feature = "alloc")]
impl<T> AppendCopy<T> for Vec<T>
where
T: Copy,
impl<T> AppendCopy<T> for Vec<T> where T: Copy
{
fn append_copy(&mut self, i: &[T]) {
self.extend(i);
}
}

impl<T, A> AppendCopy<T> for tinyvec::ArrayVec<A>
where
T: Copy,
A: tinyvec::Array<Item = T>,
where T: Copy,
A: tinyvec::Array<Item = T>
{
fn append_copy(&mut self, i: &[T]) {
self.extend_from_slice(i);
Expand Down Expand Up @@ -415,17 +397,15 @@ impl<T> Indexed<T> for Vec<T> {
}

impl<A, T> Array for tinyvec::ArrayVec<A>
where
Self: Filled<T> + Trunc,
A: tinyvec::Array<Item = T>,
where Self: Filled<T> + Trunc,
A: tinyvec::Array<Item = T>
{
type Item = T;
}

impl<A> Indexed<A::Item> for tinyvec::ArrayVec<A>
where
Self: Filled<A::Item> + Trunc,
A: tinyvec::Array,
where Self: Filled<A::Item> + Trunc,
A: tinyvec::Array
{
fn insert(&mut self, ix: usize, t: A::Item) {
tinyvec::ArrayVec::insert(self, ix, t)
Expand Down
3 changes: 2 additions & 1 deletion toad-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "toad-common"
version = "0.15.0"
edition = "2021"
rust-version = "1.70"
description = "Common structs and abstractions used by toad"
authors = ["Orion Kindel <cakekindel@gmail.com>"]
license = "MIT OR Apache-2.0"
Expand All @@ -22,5 +23,5 @@ test = []
docs = []

[dependencies]
tinyvec = {version = "1.5", default_features = false, features = ["rustc_1_55"]}
tinyvec = {version = "1.5", default_features = false, features = ["alloc", "rustc_1_55"]}
blake2 = "0.10"
33 changes: 10 additions & 23 deletions toad-common/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ pub trait Reserve: Default {
///
/// If self was longer, drops elements up to `len`
pub trait Trunc
where
Self: Sized,
where Self: Sized
{
#[allow(missing_docs)]
fn trunc(&mut self, len: usize) -> ();
Expand All @@ -122,9 +121,7 @@ impl<T> Trunc for Vec<T> {
}
}

impl<T, const N: usize> Trunc for tinyvec::ArrayVec<[T; N]>
where
T: Default,
impl<T, const N: usize> Trunc for tinyvec::ArrayVec<[T; N]> where T: Default
{
fn trunc(&mut self, len: usize) -> () {
self.truncate(len)
Expand All @@ -139,24 +136,21 @@ where
pub trait Filled<T>: Sized {
#[allow(missing_docs)]
fn filled(t: T) -> Option<Self>
where
T: Copy,
where T: Copy
{
Self::filled_using(|| t)
}

#[allow(missing_docs)]
fn filled_default() -> Option<Self>
where
T: Default,
where T: Default
{
Self::filled_using(|| Default::default())
}

#[allow(missing_docs)]
fn filled_using<F>(f: F) -> Option<Self>
where
F: Fn() -> T;
where F: Fn() -> T;
}

#[cfg(feature = "alloc")]
Expand All @@ -169,29 +163,24 @@ impl<T> Reserve for Vec<T> {
#[cfg(feature = "alloc")]
impl<T> Filled<T> for Vec<T> {
fn filled_using<F>(_: F) -> Option<Self>
where
F: Fn() -> T,
where F: Fn() -> T
{
None
}
}

impl<A: tinyvec::Array> Reserve for tinyvec::ArrayVec<A> {}

impl<T, const N: usize> Filled<T> for tinyvec::ArrayVec<[T; N]>
where
T: Default,
impl<T, const N: usize> Filled<T> for tinyvec::ArrayVec<[T; N]> where T: Default
{
fn filled_using<F>(f: F) -> Option<Self>
where
F: Fn() -> T,
where F: Fn() -> T
{
Some(core::iter::repeat(()).take(N).map(|_| f()).collect())
}

fn filled(t: T) -> Option<Self>
where
T: Copy,
where T: Copy
{
Some(Self::from([t; N]))
}
Expand Down Expand Up @@ -293,9 +282,7 @@ impl<T> Array for Vec<T> {
}
}

impl<A: tinyvec::Array<Item = T>, T> Array for tinyvec::ArrayVec<A>
where
Self: Filled<T> + Trunc,
impl<A: tinyvec::Array<Item = T>, T> Array for tinyvec::ArrayVec<A> where Self: Filled<T> + Trunc
{
type Item = T;

Expand Down
51 changes: 26 additions & 25 deletions toad-common/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,25 @@ impl<T: AsRef<[u8]>> Cursor<T> {
#[allow(clippy::should_implement_trait)]
pub fn next(&mut self) -> Option<u8> {
self.take_exact(1).and_then(|a| match a {
| &[a] => Some(a),
| _ => None,
})
| &[a] => Some(a),
| _ => None,
})
}

/// Take `n` bytes from the cursor, stopping early if
/// the end of the buffer is encountered.
///
/// Runs in O(1) time.
pub fn take(&mut self, n: usize) -> &[u8] {
Self::peek_(self.len, self.cursor, &self.t, n)
.map(|a| {
Self::skip_(&mut self.cursor, self.len, n);
a
})
.unwrap_or_else(|| Self::take_until_end_(&mut self.cursor, self.len, &self.t))
Self::peek_(self.len, self.cursor, &self.t, n).map(|a| {
Self::skip_(&mut self.cursor, self.len, n);
a
})
.unwrap_or_else(|| {
Self::take_until_end_(&mut self.cursor,
self.len,
&self.t)
})
}

/// Take `n` bytes from the cursor, returning None if
Expand All @@ -97,9 +100,9 @@ impl<T: AsRef<[u8]>> Cursor<T> {
/// Runs in O(1) time.
pub fn take_exact(&mut self, n: usize) -> Option<&[u8]> {
Self::peek_(self.len, self.cursor, &self.t, n).map(|a| {
Self::skip_(&mut self.cursor, self.len, n);
a
})
Self::skip_(&mut self.cursor, self.len, n);
a
})
}

/// Without advancing the position, look at the next
Expand Down Expand Up @@ -140,16 +143,15 @@ impl<T: AsRef<[u8]>> Cursor<T> {
return &[];
}

(self.cursor..self.len)
.into_iter()
.take_while(|ix| f(self.t.as_ref()[*ix]))
.last()
.map(|end_ix| {
let out = &self.t.as_ref()[self.cursor..=end_ix];
self.cursor = end_ix + 1;
out
})
.unwrap_or(&[])
(self.cursor..self.len).into_iter()
.take_while(|ix| f(self.t.as_ref()[*ix]))
.last()
.map(|end_ix| {
let out = &self.t.as_ref()[self.cursor..=end_ix];
self.cursor = end_ix + 1;
out
})
.unwrap_or(&[])
}

/// Whether the cursor has reached the end
Expand Down Expand Up @@ -267,9 +269,8 @@ mod tests {
#[test]
pub fn take_while() {
let til_slash = |c: &mut Cursor<&str>| {
core::str::from_utf8(c.take_while(|b| (b as char) != '/'))
.unwrap()
.to_string()
core::str::from_utf8(c.take_while(|b| (b as char) != '/')).unwrap()
.to_string()
};

let mut cur = Cursor::new("abc/def");
Expand Down
4 changes: 2 additions & 2 deletions toad-common/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl Blake2Hasher {
impl Debug for Blake2Hasher {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("Blake2Hasher")
.field(&"<Blake2bCore<U8>>")
.finish()
.field(&"<Blake2bCore<U8>>")
.finish()
}
}

Expand Down
Loading
Loading