Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed non-empty restriction #26

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![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)

## bounded-vec
`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).
`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).

## Example

Expand Down
59 changes: 16 additions & 43 deletions src/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::slice::{Iter, IterMut};
use std::vec;
use thiserror::Error;

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

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

/// Returns the first element of non-empty Vec
/// Returns the first element of Vec
///
/// # Example
/// ```
/// use bounded_vec::BoundedVec;
/// use std::convert::TryInto;
///
/// let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
/// assert_eq!(*data.first(), 1);
/// assert_eq!(data.first(), Some(&1));
/// ```
pub fn first(&self) -> &T {
#[allow(clippy::unwrap_used)]
self.inner.first().unwrap()
pub fn first(&self) -> Option<&T> {
self.inner.first()
}

/// Returns the last element of non-empty Vec
/// Returns the last element of Vec
///
/// # Example
/// ```
/// use bounded_vec::BoundedVec;
/// use std::convert::TryInto;
///
/// let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
/// assert_eq!(*data.last(), 2);
/// assert_eq!(data.last(), Some(&2));
/// ```
pub fn last(&self) -> &T {
#[allow(clippy::unwrap_used)]
self.inner.last().unwrap()
pub fn last(&self) -> Option<&T> {
self.inner.last()
}

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

/// Returns the last and all the rest of the elements
pub fn split_last(&self) -> (&T, &[T]) {
#[allow(clippy::unwrap_used)]
self.inner.split_last().unwrap()
pub fn split_last(&self) -> Option<(&T, &[T])> {
self.inner.split_last()
}

/// Return a new BoundedVec with indices included
Expand All @@ -325,27 +319,6 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
.try_into()
.unwrap()
}

/// Return a Some(BoundedVec) or None if `v` is empty
/// # Example
/// ```
/// use bounded_vec::BoundedVec;
/// use bounded_vec::OptBoundedVecToVec;
///
/// let opt_bv_none = BoundedVec::<u8, 2, 8>::opt_empty_vec(vec![]).unwrap();
/// assert!(opt_bv_none.is_none());
/// assert_eq!(opt_bv_none.to_vec(), vec![]);
/// let opt_bv_some = BoundedVec::<u8, 2, 8>::opt_empty_vec(vec![0u8, 2]).unwrap();
/// assert!(opt_bv_some.is_some());
/// assert_eq!(opt_bv_some.to_vec(), vec![0u8, 2]);
/// ```
pub fn opt_empty_vec(v: Vec<T>) -> Result<Option<BoundedVec<T, L, U>>, BoundedVecOutOfBounds> {
if v.is_empty() {
Ok(None)
} else {
Ok(Some(BoundedVec::from_vec(v)?))
}
}
}

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

#[test]
fn last() {
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.last(), &2u8);
assert_eq!(data.last(), Some(&2u8));
}

#[test]
Expand Down Expand Up @@ -563,9 +536,9 @@ mod tests {
#[test]
fn split_last() {
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
assert_eq!(data.split_last(), (&2u8, [1u8].as_ref()));
assert_eq!(data.split_last(), Some((&2u8, [1u8].as_ref())));
let data1: BoundedVec<_, 1, 8> = vec![1u8].try_into().unwrap();
assert_eq!(data1.split_last(), (&1u8, Vec::new().as_ref()));
assert_eq!(data1.split_last(), Some((&1u8, Vec::new().as_ref())));
}

#[test]
Expand Down