Skip to content

Commit 3457ee8

Browse files
committed
Fixed backward compatibility problem
1 parent f67d89b commit 3457ee8

1 file changed

Lines changed: 104 additions & 16 deletions

File tree

src/bounded_vec.rs

Lines changed: 104 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,36 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
138138
self.inner.as_slice()
139139
}
140140

141-
/// Returns the first element of Vec
141+
/// Returns the first element of non-empty Vec
142142
///
143143
/// # Example
144144
/// ```
145145
/// use bounded_vec::BoundedVec;
146146
/// use std::convert::TryInto;
147147
///
148148
/// let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
149-
/// assert_eq!(data.first(), Some(&1));
149+
/// assert_eq!(*data.first(), 1);
150150
/// ```
151-
pub fn first(&self) -> Option<&T> {
152-
self.inner.first()
151+
pub fn first(&self) -> &T {
152+
const { assert!(L != 0) }
153+
#[allow(clippy::unwrap_used)]
154+
self.inner.first().unwrap()
153155
}
154156

155-
/// Returns the last element of Vec
157+
// Returns the last element of Vec
156158
///
157159
/// # Example
158160
/// ```
159161
/// use bounded_vec::BoundedVec;
160162
/// use std::convert::TryInto;
161163
///
162164
/// let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
163-
/// assert_eq!(data.last(), Some(&2));
165+
/// assert_eq!(*data.last(), 2);
164166
/// ```
165-
pub fn last(&self) -> Option<&T> {
166-
self.inner.last()
167+
pub fn last(&self) -> &T {
168+
const { assert!(L != 0) }
169+
#[allow(clippy::unwrap_used)]
170+
self.inner.last().unwrap()
167171
}
168172

169173
/// Create a new `BoundedVec` by consuming `self` and mapping each element.
@@ -305,8 +309,10 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
305309
}
306310

307311
/// Returns the last and all the rest of the elements
308-
pub fn split_last(&self) -> Option<(&T, &[T])> {
309-
self.inner.split_last()
312+
pub fn split_last(&self) -> (&T, &[T]) {
313+
const { assert!(L != 0) }
314+
#[allow(clippy::unwrap_used)]
315+
self.inner.split_last().unwrap()
310316
}
311317

312318
/// Return a new BoundedVec with indices included
@@ -319,6 +325,62 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
319325
.try_into()
320326
.unwrap()
321327
}
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+
}
349+
}
350+
351+
impl<T, const U: usize> BoundedVec<T, 0, U> {
352+
/// Returns the first element of Vec
353+
///
354+
/// # Example
355+
/// ```
356+
/// use bounded_vec::BoundedVec;
357+
/// use std::convert::TryInto;
358+
///
359+
/// let data: BoundedVec<_, 0, 8> = vec![1u8, 2].try_into().unwrap();
360+
/// assert_eq!(data.try_first(), Some(&1));
361+
/// ```
362+
pub fn try_first(&self) -> Option<&T> {
363+
self.inner.first()
364+
}
365+
366+
/// Returns the last element of Vec
367+
///
368+
/// # Example
369+
/// ```
370+
/// use bounded_vec::BoundedVec;
371+
/// use std::convert::TryInto;
372+
///
373+
/// let data: BoundedVec<_, 0, 8> = vec![1u8, 2].try_into().unwrap();
374+
/// assert_eq!(data.try_last(), Some(&2));
375+
/// ```
376+
pub fn try_last(&self) -> Option<&T> {
377+
self.inner.last()
378+
}
379+
380+
/// Returns the last and all the rest of the elements
381+
pub fn try_split_last(&self) -> Option<(&T, &[T])> {
382+
self.inner.split_last()
383+
}
322384
}
323385

324386
/// A non-empty Vec with no effective upper-bound on its length
@@ -359,7 +421,7 @@ impl<'a, T, const L: usize, const U: usize> IntoIterator for &'a BoundedVec<T, L
359421
type IntoIter = core::slice::Iter<'a, T>;
360422

361423
fn into_iter(self) -> Self::IntoIter {
362-
(&self.inner).iter()
424+
self.inner.iter()
363425
}
364426
}
365427

@@ -368,7 +430,7 @@ impl<'a, T, const L: usize, const U: usize> IntoIterator for &'a mut BoundedVec<
368430
type IntoIter = core::slice::IterMut<'a, T>;
369431

370432
fn into_iter(self) -> Self::IntoIter {
371-
(&mut self.inner).iter_mut()
433+
self.inner.iter_mut()
372434
}
373435
}
374436

@@ -475,13 +537,29 @@ mod tests {
475537
#[test]
476538
fn first() {
477539
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
478-
assert_eq!(data.first(), Some(&1u8));
540+
assert_eq!(data.first(), &1u8);
479541
}
480542

481543
#[test]
482544
fn last() {
483545
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
484-
assert_eq!(data.last(), Some(&2u8));
546+
assert_eq!(data.last(), &2u8);
547+
}
548+
549+
#[test]
550+
fn try_first() {
551+
let data: BoundedVec<_, 0, 8> = vec![1u8, 2].try_into().unwrap();
552+
assert_eq!(data.try_first(), Some(&1u8));
553+
let data: BoundedVec<i32, 0, 8> = vec![].try_into().unwrap();
554+
assert_eq!(data.try_first(), None);
555+
}
556+
557+
#[test]
558+
fn try_last() {
559+
let data: BoundedVec<_, 0, 8> = vec![1u8, 2].try_into().unwrap();
560+
assert_eq!(data.try_last(), Some(&2u8));
561+
let data: BoundedVec<i32, 0, 8> = vec![].try_into().unwrap();
562+
assert_eq!(data.try_last(), None);
485563
}
486564

487565
#[test]
@@ -536,9 +614,19 @@ mod tests {
536614
#[test]
537615
fn split_last() {
538616
let data: BoundedVec<_, 2, 8> = vec![1u8, 2].try_into().unwrap();
539-
assert_eq!(data.split_last(), Some((&2u8, [1u8].as_ref())));
617+
assert_eq!(data.split_last(), (&2u8, [1u8].as_ref()));
540618
let data1: BoundedVec<_, 1, 8> = vec![1u8].try_into().unwrap();
541-
assert_eq!(data1.split_last(), Some((&1u8, Vec::new().as_ref())));
619+
assert_eq!(data1.split_last(), (&1u8, Vec::new().as_ref()));
620+
}
621+
622+
#[test]
623+
fn try_split_last() {
624+
let data: BoundedVec<_, 0, 8> = vec![1u8, 2].try_into().unwrap();
625+
assert_eq!(data.try_split_last(), Some((&2u8, [1u8].as_ref())));
626+
let data1: BoundedVec<_, 0, 8> = vec![1u8].try_into().unwrap();
627+
assert_eq!(data1.try_split_last(), Some((&1u8, Vec::new().as_ref())));
628+
let data2: BoundedVec<i32, 0, 8> = vec![].try_into().unwrap();
629+
assert_eq!(data2.try_split_last(), None);
542630
}
543631

544632
#[test]

0 commit comments

Comments
 (0)