Skip to content

Commit

Permalink
move Option conversion test to doc test;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jul 26, 2022
1 parent 43ed86e commit 0657831
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/bounded_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@ impl<T, const L: usize, const U: usize> BoundedVec<T, L, U> {
}

/// 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)
Expand Down Expand Up @@ -553,14 +565,4 @@ mod tests {
vec.iter_mut().collect::<Vec<&mut u8>>()
);
}

#[test]
fn try_opt_empty_vec_roundtrip() {
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]);
}
}

0 comments on commit 0657831

Please sign in to comment.