Skip to content
Open
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
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ serde_core = { version = "1.0", optional = true, default-features = false }
arbitrary = { version = "1", optional = true }
# Provides `BorshSerialize` and `BorshDeserialize implementations
borsh = { version = "1.2.0", optional = true, default-features = false }
# Implements the trait `Array` for `GenericArray` struct.
generic-array = { version = "1.1.1", optional = true, default-features = false }
# Implements the trait `tinyvec::Array` for `GenericArray` struct.
generic-array = { version = "1.4.1", optional = true, default-features = false }
# Implements the trait `tinyvec::Array` for `hybrid_array::Array` struct.
hybrid-array = { version = "0.4.12", optional = true, default-features = false }
# Provides derived `BitEncode` and `BitDecode` implementations
bin-proto = { version = "0.12.5", optional = true, default-features = false }
# Provides `Format` implementations
Expand All @@ -30,13 +32,13 @@ defmt = { version = "1.0", optional = true }
default = []

# Provide things that utilize the `alloc` crate, namely `TinyVec`.
alloc = ["tinyvec_macros"]
alloc = ["tinyvec_macros", "hybrid-array?/alloc", "generic-array?/alloc"]

# Provide things that require Rust's `std` module
std = ["alloc"]

# Provides `Serialize` and `Deserialize` implementations
serde = ["dep:serde_core"]
serde = ["dep:serde_core", "hybrid-array?/serde", "generic-array?/serde"]

# (not part of Vec!) Extra methods to let you grab the slice of memory after the
# "active" portion of an `ArrayVec` or `SliceVec`.
Expand Down Expand Up @@ -82,6 +84,9 @@ experimental_write_impl = []
# and thus a nightly compiler, but is only used in benchmarks.
real_blackbox = ["criterion/real_blackbox"]

# enable third-part support
arbitrary = ["dep:arbitrary", "hybrid-array?/arbitrary", "generic-array?/arbitrary"]

[package.metadata.docs.rs]
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt", "bin-proto"]
rustdoc-args = ["--cfg","docs_rs"]
Expand Down
3 changes: 3 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ mod const_generic_impl;

#[cfg(feature = "generic-array")]
mod generic_array_impl;

#[cfg(feature = "hybrid-array")]
mod hybrid_array_impl;
78 changes: 75 additions & 3 deletions src/array/generic_array_impl.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,96 @@
use core::default;

use super::Array;
use generic_array::{ArrayLength, GenericArray};
use generic_array::{ArrayLength, GenericArray, IntoArrayLength};

impl<T: Default, N: ArrayLength> Array for GenericArray<T, N> {
type Item = T;
const CAPACITY: usize = N::USIZE;

#[inline(always)]
fn as_slice(&self) -> &[T] {
&*self
GenericArray::as_slice(self)
}

#[inline(always)]
fn as_slice_mut(&mut self) -> &mut [T] {
&mut *self
GenericArray::as_mut_slice(self)
}

#[inline(always)]
fn default() -> Self {
<Self as Default>::default()
}
}

impl <T> IntoArrayLength for crate::ArrayVec<T> where T: IntoArrayLength {
type ArrayLength = <T as IntoArrayLength>::ArrayLength;
}

#[cfg(test)]
mod test {
use super::*;
use crate::{ArrayVec, array_vec};
use generic_array::ConstGenericArray;
type TinyGenericVec<T, const N: usize> = ArrayVec<ConstGenericArray<T, N>>;

#[test]
fn retain_mut_empty_vec() {
let mut av: TinyGenericVec<i32, 4> = TinyGenericVec::<i32, 4>::new();
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 0);
}

#[test]
fn retain_mut_all_elements() {
let mut av: TinyGenericVec<i32, 4> = array_vec!(ConstGenericArray<i32, 4> => 2, 4, 6, 8);
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 4);
assert_eq!(av.as_slice(), &[2, 4, 6, 8]);
}

#[test]
fn retain_mut_some_elements() {
let mut av: TinyGenericVec<i32, 4> = array_vec!(ConstGenericArray<i32, 4> => 1, 2, 3, 4);
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 2);
assert_eq!(av.as_slice(), &[2, 4]);
}

#[test]
fn retain_mut_no_elements() {
let mut av: TinyGenericVec<i32, 4> = array_vec!(ConstGenericArray<i32, 4> => 1, 3, 5, 7);
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 0);
}

#[test]
fn retain_mut_zero_capacity() {
let mut av: TinyGenericVec<i32, 0> = ArrayVec::new();
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 0);
}

#[cfg(feature = "alloc")]
#[test]
fn array_like_debug() {
#[derive(Debug, Default, Copy, Clone)]
struct S {
x: u8,
y: u8,
}

use core::fmt::Write;
use alloc::string::String;

let mut ar: ConstGenericArray<S, 2> = ConstGenericArray::<S, 2>::from_array([S { x: 1, y: 2 }, S { x: 3, y: 4 }]);
let mut buf_ar = String::new();
write!(&mut buf_ar, "{:#?}", ar.as_slice()).unwrap();

let av: TinyGenericVec<S, 2> = TinyGenericVec::<S, 2>::from_array_len(ar, 2);
let mut buf_av = String::new();
write!(&mut buf_av, "{av:#?}").unwrap();

assert_eq!(buf_av, buf_ar)
}
}
96 changes: 96 additions & 0 deletions src/array/hybrid_array_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use core::default;

use super::Array;
use hybrid_array::{Array as HybridArray, AssocArraySize, ArraySize, typenum::Unsigned};

impl<T: Default, N: ArraySize> Array for HybridArray<T, N> {
type Item = T;
const CAPACITY: usize = <N as Unsigned>::USIZE;

#[inline(always)]
fn as_slice(&self) -> &[T] {
HybridArray::as_slice(self)
}

#[inline(always)]
fn as_slice_mut(&mut self) -> &mut [T] {
HybridArray::as_mut_slice(self)
}

#[inline(always)]
fn default() -> Self {
<Self as Default>::default()
}
}

impl <T> AssocArraySize for crate::ArrayVec<T> where T: AssocArraySize {
type Size = <T as AssocArraySize>::Size;
}

#[cfg(test)]
mod test {
use super::*;
use crate::{ArrayVec, array_vec};
use hybrid_array::ArrayN;
type TinyHybridVec<T, const N: usize> = ArrayVec<ArrayN<T, N>>;

#[test]
fn retain_mut_empty_vec() {
let mut av: TinyHybridVec<i32, 4> = TinyHybridVec::<i32, 4>::new();
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 0);
}

#[test]
fn retain_mut_all_elements() {
let mut av: TinyHybridVec<i32, 4> = array_vec!(ArrayN<i32, 4> => 2, 4, 6, 8);
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 4);
assert_eq!(av.as_slice(), &[2, 4, 6, 8]);
}

#[test]
fn retain_mut_some_elements() {
let mut av: TinyHybridVec<i32, 4> = array_vec!(ArrayN<i32, 4> => 1, 2, 3, 4);
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 2);
assert_eq!(av.as_slice(), &[2, 4]);
}

#[test]
fn retain_mut_no_elements() {
let mut av: TinyHybridVec<i32, 4> = array_vec!(ArrayN<i32, 4> => 1, 3, 5, 7);
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 0);
}

#[test]
fn retain_mut_zero_capacity() {
let mut av: TinyHybridVec<i32, 0> = ArrayVec::new();
av.retain_mut(|&mut x| x % 2 == 0);
assert_eq!(av.len(), 0);
}

#[cfg(feature = "alloc")]
#[test]
fn array_like_debug() {
#[derive(Debug, Default, Copy, Clone)]
struct S {
x: u8,
y: u8,
}

use core::fmt::Write;
use alloc::string::String;
use hybrid_array::{Array, sizes::U2};

let mut ar: Array<S, U2> = Array::<S, U2>([S { x: 1, y: 2 }, S { x: 3, y: 4 }]);
let mut buf_ar = String::new();
write!(&mut buf_ar, "{:#?}", ar.as_slice()).unwrap();

let av: TinyHybridVec<S, 2> = TinyHybridVec::<S, 2>::from(ar);
let mut buf_av = String::new();
write!(&mut buf_av, "{av:#?}").unwrap();
assert_eq!(buf_av, buf_ar)
}
}
Loading