arrayvec icon indicating copy to clipboard operation
arrayvec copied to clipboard

Add `From<[T; LEN]>`

Open khoover opened this issue 2 years ago • 1 comments

We can use compile-time asserts to check that LEN <= CAP, and then only initialize the first LEN elements. So something like

impl<T, const LEN: usize, const CAP: usize> From<[T; LEN]> for ArrayVec<T, CAP> {
  fn from(arr: [T; LEN]) -> Self {
    assert!(LEN <= CAP);
    // duplicate the rest of From<[T; LEN]> for ArrayVec<T, LEN> here
  }
}

khoover avatar Dec 11 '22 19:12 khoover

I think, we should wait with this until trait bounds would be available so we can write something like this:

impl<T, const LEN: usize, const CAP: usize> From<[T; LEN]> for ArrayVec<T, CAP>
    where LEN <= CAP
{
  fn from(arr: [T; LEN]) -> Self {
    // duplicate the rest of From<[T; LEN]> for ArrayVec<T, LEN> here
  }
}

AngelicosPhosphoros avatar May 31 '23 23:05 AngelicosPhosphoros