Skip to content

Commit d9b6c56

Browse files
committed
vec: add tests for alloc conversion
1 parent bee84f9 commit d9b6c56

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/vec/mod.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ impl<T, S: VecStorage<T> + ?Sized> Drop for VecInner<T, S> {
12801280
#[cfg(feature = "alloc")]
12811281
/// Converts the given `alloc::vec::Vec<T>` into a `Vec<T, N>`.
12821282
impl<T, const N: usize> TryFrom<alloc::vec::Vec<T>> for Vec<T, N> {
1283-
type Error = crate::CapacityError;
1283+
type Error = CapacityError;
12841284

12851285
/// Converts the given `alloc::vec::Vec<T>` into a `Vec<T, N>`.
12861286
///
@@ -1292,7 +1292,7 @@ impl<T, const N: usize> TryFrom<alloc::vec::Vec<T>> for Vec<T, N> {
12921292

12931293
for e in alloc_vec {
12941294
// Push each element individually to allow handling capacity errors.
1295-
vec.push(e)?;
1295+
vec.push(e).map_err(|_| CapacityError {})?;
12961296
}
12971297

12981298
Ok(vec)
@@ -2146,6 +2146,28 @@ mod tests {
21462146
assert!(v.spare_capacity_mut().is_empty());
21472147
}
21482148

2149+
#[test]
2150+
#[cfg(feature = "alloc")]
2151+
fn heapless_to_alloc() {
2152+
let mut hv: Vec<u8, 4> = Vec::new();
2153+
hv.push(0).unwrap();
2154+
hv.push(1).unwrap();
2155+
2156+
let av: alloc::vec::Vec<u8> = hv.clone().try_into().unwrap();
2157+
assert_eq!(av.as_slice(), hv.as_slice());
2158+
}
2159+
2160+
#[test]
2161+
#[cfg(feature = "alloc")]
2162+
fn alloc_to_heapless() {
2163+
let mut av: alloc::vec::Vec<u8> = alloc::vec::Vec::new();
2164+
av.push(0);
2165+
av.push(1);
2166+
2167+
let hv: Vec<u8, 2> = av.clone().try_into().unwrap();
2168+
assert_eq!(hv.as_slice(), av.as_slice());
2169+
}
2170+
21492171
fn _test_variance<'a: 'b, 'b>(x: Vec<&'a (), 42>) -> Vec<&'b (), 42> {
21502172
x
21512173
}

0 commit comments

Comments
 (0)