mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-10-01 22:40:31 +00:00
Relax Vec bounds to allow for zero-length Vecs
Use when building generated code where it's possible to have a zero length array/vector (e.g. wire-format). See #252 - Added vec::tests::zero_capacity test - Added sealed::greater_than_eq_0
This commit is contained in:
parent
f3af76d607
commit
bc2cd1be70
@ -52,6 +52,12 @@ pub(crate) const fn smaller_than<const N: usize, const MAX: usize>() {
|
||||
Assert::<N, MAX>::LESS;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[allow(path_statements)]
|
||||
pub(crate) const fn greater_than_eq_0<const N: usize>() {
|
||||
Assert::<N, 0>::GREATER_EQ;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[allow(path_statements)]
|
||||
pub(crate) const fn greater_than_0<const N: usize>() {
|
||||
|
23
src/vec.rs
23
src/vec.rs
@ -56,8 +56,8 @@ impl<T, const N: usize> Vec<T, N> {
|
||||
/// ```
|
||||
/// `Vec` `const` constructor; wrap the returned value in [`Vec`](../struct.Vec.html)
|
||||
pub const fn new() -> Self {
|
||||
// Const assert N > 0
|
||||
crate::sealed::greater_than_0::<N>();
|
||||
// Const assert N >= 0
|
||||
crate::sealed::greater_than_eq_0::<N>();
|
||||
|
||||
Self {
|
||||
buffer: [Self::INIT; N],
|
||||
@ -1233,4 +1233,23 @@ mod tests {
|
||||
assert!(!v.ends_with(b"ba"));
|
||||
assert!(!v.ends_with(b"a"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_capacity() {
|
||||
let mut v: Vec<u8, 0> = Vec::new();
|
||||
// Validate capacity
|
||||
assert_eq!(v.capacity(), 0);
|
||||
|
||||
// Make sure there is no capacity
|
||||
assert!(v.push(1).is_err());
|
||||
|
||||
// Validate length
|
||||
assert_eq!(v.len(), 0);
|
||||
|
||||
// Validate pop
|
||||
assert_eq!(v.pop(), None);
|
||||
|
||||
// Validate slice
|
||||
assert_eq!(v.as_slice(), &[]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user