fizyk20 / generic-array

Generic array types in Rust
MIT License
404 stars 77 forks source link

Simplify ArrayLength<T> trait to ArrayLength (make the ArrayLength::ArrayType GAT generic instead) #137

Closed Qqwy closed 1 year ago

Qqwy commented 1 year ago

GenericArray<T, N> requires that N implements ArrayLength<T> which is currently implemented like this:

pub unsafe trait ArrayLength<T>: Unsigned {
    /// Associated type representing the array type for the number
    type ArrayType;
}

However, , this is overly restrictive. If you're working on any kind of trait which requires changing the GenericArray's element types, you end up with bounds like N: ArrayLength<T> + ArrayLength<S> + ArrayLength<R> + ....

But T is only used at the place where the ArrayType associated type is actually inspected, and otherwise completely unconstrainted.

As such, in modern Rust versions (I'm not sure what the MSRV is), we could change the trait to:

pub unsafe trait ArrayLength: Unsigned {
    /// Associated type representing the array type for the number
    type ArrayType<T>;
}

which captures the intent more clearly, and keeps bounds on N much simpler.

novacrazy commented 1 year ago

This is a great idea, and seems to work well based on some early testing. Might be enough motivation for a 1.0 release.