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

More generic From<[T; N]> and Into<[T; N]> implementations #140

Closed
Qqwy opened this issue Mar 30, 2023 · 2 comments · Fixed by #138
Closed

More generic From<[T; N]> and Into<[T; N]> implementations #140

Qqwy opened this issue Mar 30, 2023 · 2 comments · Fixed by #138

Comments

@Qqwy
Copy link

Qqwy commented Mar 30, 2023

Currently, these implementations are hard-coded for certain array lengths.

This was necessary on old Rust versions in which

  • const generics were not possible, and
  • array traits were restricted to max lengths of 32.
    But since v1.47 this is no longer the case.

By enabling the consts feature on typenum, we get access to the U<const N: usize> type. And this is what lets us write a single implementation for From and for Into that will work for all lengths!

@Qqwy
Copy link
Author

Qqwy commented Mar 30, 2023

Currently I am working around this in my own code by transmuting directly (as the current hard-coded implementations of From/Into do as well) which works, but is hacky as it makes my code deeply dependent on the internals of this crate.

novacrazy added a commit that referenced this issue Mar 30, 2023
@novacrazy
Copy link
Collaborator

This is now implemented in the 1.0 branch. However, to ease use with const-generics in user libraries, I'm considering something like:

pub unsafe trait IntoArrayLength {
    type ArrayLength: ArrayLength;
}

unsafe impl<const N: usize> IntoArrayLength for Const<N>
where
    Const<N>: ToUInt,
    U<N>: ArrayLength,
{
    type ArrayLength = U<N>;
}

pub type ConstArrayLength<const N: usize> = <Const<N> as IntoArrayLength>::ArrayLength;

impl<T, const N: usize> From<[T; N]> for GenericArray<T, ConstArrayLength<N>>
where
    Const<N>: IntoArrayLength,

Cuts down on one where bound.

@novacrazy novacrazy mentioned this issue Apr 28, 2023
Merged
2 tasks
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

Successfully merging a pull request may close this issue.

2 participants