More generic From<[T; N]> and Into<[T; N]> implementations
Closed this issue · 2 comments
Qqwy commented
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 commented
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 commented
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.