Skip to content

Commit a9756bd

Browse files
committed
Implement TryFrom<&[T]> for Vec<T, N>
There already is the Vec::from_slice method that does the same, but implementing the TryFrom trait makes it easier to write code that works both with heapless::Vec and std::vec::Vec.
1 parent f3175ee commit a9756bd

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/vec.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use core::{cmp::Ordering, fmt, hash, iter::FromIterator, mem::MaybeUninit, ops, ptr, slice};
1+
use core::{
2+
cmp::Ordering, convert::TryFrom, fmt, hash, iter::FromIterator, mem::MaybeUninit, ops, ptr,
3+
slice,
4+
};
25
use hash32;
36

47
/// A fixed capacity [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html)
@@ -569,6 +572,14 @@ impl<T, const N: usize> Drop for Vec<T, N> {
569572
}
570573
}
571574

575+
impl<'a, T: Clone, const N: usize> TryFrom<&'a [T]> for Vec<T, N> {
576+
type Error = ();
577+
578+
fn try_from(slice: &'a [T]) -> Result<Self, Self::Error> {
579+
Vec::from_slice(slice)
580+
}
581+
}
582+
572583
impl<T, const N: usize> Extend<T> for Vec<T, N> {
573584
fn extend<I>(&mut self, iter: I)
574585
where

0 commit comments

Comments
 (0)