Skip to content

Commit f67d89b

Browse files
committed
Removed non-empty restriction
1 parent 8265180 commit f67d89b

File tree

2 files changed

+17
-44
lines changed

2 files changed

+17
-44
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Latest Version](https://img.shields.io/crates/v/bounded-vec.svg)](https://crates.io/crates/bounded-vec) [![Documentation](https://docs.rs/bounded-vec/badge.svg)](https://docs.rs/crate/bounded-vec)
33

44
## bounded-vec
5-
`BoundedVec<T, L, U>` - Non-empty rust `std::vec::Vec` wrapper with type guarantees on lower(`L`) and upper(`U`) bounds for items quantity. Inspired by [vec1](https://github.com/rustonaut/vec1).
5+
`BoundedVec<T, L, U>` - rust `std::vec::Vec` wrapper with type guarantees on lower(`L`) and upper(`U`) bounds for items quantity. Inspired by [vec1](https://github.com/rustonaut/vec1).
66

77
## Example
88

src/bounded_vec.rs

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::slice::{Iter, IterMut};
55
use std::vec;
66
use thiserror::Error;
77

8-
/// Non-empty Vec bounded with minimal (L - lower bound) and maximal (U - upper bound) items quantity
8+
/// Bounded Vec with minimal (L - lower bound) and maximal (U - upper bound) items quantity
99
#[derive(PartialEq, Eq, Debug, Clone, Hash, PartialOrd, Ord)]
1010
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
1111
pub struct BoundedVec<T, const L: usize, const U: usize>
@@ -52,9 +52,6 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
5252
/// let data: BoundedVec<_, 2, 8> = BoundedVec::from_vec(vec![1u8, 2]).unwrap();
5353
/// ```
5454
pub fn from_vec(items: Vec<T>) -> Result<Self, BoundedVecOutOfBounds> {
55-
// remove when feature(const_evaluatable_checked) is stable
56-
// and this requirement is encoded in type sig
57-
assert!(L > 0);
5855
let len = items.len();
5956
if len < L {
6057
Err(BoundedVecOutOfBounds::LowerBoundError {
@@ -124,7 +121,7 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
124121
/// assert_eq!(data.is_empty(), false);
125122
/// ```
126123
pub fn is_empty(&self) -> bool {
127-
false
124+
self.inner.is_empty()
128125
}
129126

130127
/// Extracts a slice containing the entire vector.
@@ -141,34 +138,32 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
141138
self.inner.as_slice()
142139
}
143140

144-
/// Returns the first element of non-empty Vec
141+
/// Returns the first element of Vec
145142
///
146143
/// # Example
147144
/// ```
148145
/// use bounded_vec::BoundedVec;
149146
/// use std::convert::TryInto;
150147
///
151148
/// let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
152-
/// assert_eq!(*data.first(), 1);
149+
/// assert_eq!(data.first(), Some(&1));
153150
/// ```
154-
pub fn first(&self) -> &T {
155-
#[allow(clippy::unwrap_used)]
156-
self.inner.first().unwrap()
151+
pub fn first(&self) -> Option<&T> {
152+
self.inner.first()
157153
}
158154

159-
/// Returns the last element of non-empty Vec
155+
/// Returns the last element of Vec
160156
///
161157
/// # Example
162158
/// ```
163159
/// use bounded_vec::BoundedVec;
164160
/// use std::convert::TryInto;
165161
///
166162
/// let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
167-
/// assert_eq!(*data.last(), 2);
163+
/// assert_eq!(data.last(), Some(&2));
168164
/// ```
169-
pub fn last(&self) -> &T {
170-
#[allow(clippy::unwrap_used)]
171-
self.inner.last().unwrap()
165+
pub fn last(&self) -> Option<&T> {
166+
self.inner.last()
172167
}
173168

174169
/// Create a new `BoundedVec` by consuming `self` and mapping each element.
@@ -310,9 +305,8 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
310305
}
311306

312307
/// Returns the last and all the rest of the elements
313-
pub fn split_last(&self) -> (&T, &[T]) {
314-
#[allow(clippy::unwrap_used)]
315-
self.inner.split_last().unwrap()
308+
pub fn split_last(&self) -> Option<(&T, &[T])> {
309+
self.inner.split_last()
316310
}
317311

318312
/// Return a new BoundedVec with indices included
@@ -325,27 +319,6 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
325319
.try_into()
326320
.unwrap()
327321
}
328-
329-
/// Return a Some(BoundedVec) or None if `v` is empty
330-
/// # Example
331-
/// ```
332-
/// use bounded_vec::BoundedVec;
333-
/// use bounded_vec::OptBoundedVecToVec;
334-
///
335-
/// let opt_bv_none = BoundedVec::<u8, 2, 8>::opt_empty_vec(vec![]).unwrap();
336-
/// assert!(opt_bv_none.is_none());
337-
/// assert_eq!(opt_bv_none.to_vec(), vec![]);
338-
/// let opt_bv_some = BoundedVec::<u8, 2, 8>::opt_empty_vec(vec![0u8, 2]).unwrap();
339-
/// assert!(opt_bv_some.is_some());
340-
/// assert_eq!(opt_bv_some.to_vec(), vec![0u8, 2]);
341-
/// ```
342-
pub fn opt_empty_vec(v: Vec<T>) -> Result<Option<BoundedVec<T, L, U>>, BoundedVecOutOfBounds> {
343-
if v.is_empty() {
344-
Ok(None)
345-
} else {
346-
Ok(Some(BoundedVec::from_vec(v)?))
347-
}
348-
}
349322
}
350323

351324
/// A non-empty Vec with no effective upper-bound on its length
@@ -502,13 +475,13 @@ mod tests {
502475
#[test]
503476
fn first() {
504477
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
505-
assert_eq!(data.first(), &1u8);
478+
assert_eq!(data.first(), Some(&1u8));
506479
}
507480

508481
#[test]
509482
fn last() {
510483
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
511-
assert_eq!(data.last(), &2u8);
484+
assert_eq!(data.last(), Some(&2u8));
512485
}
513486

514487
#[test]
@@ -563,9 +536,9 @@ mod tests {
563536
#[test]
564537
fn split_last() {
565538
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
566-
assert_eq!(data.split_last(), (&2u8, [1u8].as_ref()));
539+
assert_eq!(data.split_last(), Some((&2u8, [1u8].as_ref())));
567540
let data1: BoundedVec<_, 1, 8> = vec![1u8].try_into().unwrap();
568-
assert_eq!(data1.split_last(), (&1u8, Vec::new().as_ref()));
541+
assert_eq!(data1.split_last(), Some((&1u8, Vec::new().as_ref())));
569542
}
570543

571544
#[test]

0 commit comments

Comments
 (0)