Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add From<[T; LEN]> #232

Open
khoover opened this issue Dec 11, 2022 · 1 comment
Open

Add From<[T; LEN]> #232

khoover opened this issue Dec 11, 2022 · 1 comment

Comments

@khoover
Copy link

khoover commented Dec 11, 2022

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 khoover changed the title Add From<[T; LEN]> where T: Default Add From<[T; LEN]> Dec 11, 2022
@AngelicosPhosphoros
Copy link

AngelicosPhosphoros commented May 31, 2023

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
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants