-
Notifications
You must be signed in to change notification settings - Fork 78
Feature request: Conversion from &[[T; N]] to &[GenericArray<T, N>] (and &mut) #145
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
Comments
What would be a good name for these methods? About to commit them, just having a difficult time naming them. |
I was thinking "something similar to whatever the current [T; N] -> GenericArray conversion is called", but that's just From/Into. Which I think could actually be implemented here too, but maybe that's too subtle. To me, |
I also wonder if the reverse operation is valuable enough to add at the same time, even if I personally don't have a use for it right now. |
This is the signature for what I have so far: impl<T, N: ArrayLength> GenericArray<T, N> {
pub const fn chunks_from_slice(slice: &[T]) -> (&[GenericArray<T, N>], &[T]) and the mutable version. It's worth noting this will be on the 1.0 branch in #138 I can do the opposite with |
Oh, I was thinking this signature:
except macro-generated cause |
That was true in pre-1.0, but as of 1.0 we can do: pub const fn from_chunks<const U: usize>(chunks: &[[T; U]]) -> &[GenericArray<T, N>]
where
Const<U>: IntoArrayLength<ArrayLength = N>,
{ … } I can include both, and hopefully the full 1.0 releases within a week. |
So then there's a naming question for the opposite operation: pub const fn as_slice_of_arrays<const U: usize>(chunks: &[GenericArray<T, N>]) -> &[[T; U]]
where
Const<U>: IntoArrayLength<ArrayLength = N>,
{ … } Does generic-array have a standard name for the built-in, lang-item fixed-sized arrays? |
Not really, no.
For an inverse of At least Rust will prevent them from being used incorrectly. |
Also hopefully it won't take years for the Rust crypto crates to upgrade to GA 1.0. If you absolutely must have these backported, I can look into it sometime. |
No, it's okay, we're currently just going to use the raw cast like |
APIs like the cipher crate's
BlockEncrypt::encrypt_blocks
take a slice of GenericArrays. However, if you're starting with a flat&[u8]
, it's unergonomic to get to the required type. The unstableslice::as_chunks
gives you&[[u8; N]]
, but going from that to&[GenericArray<u8, _>]
seems impossible without a pointer cast or transmute. However, that pointer cast or transmute is valid because GenericArray isrepr(transparent)
, and it would be nice™ if the generic-array crate provided that operation as a safe wrapper.The text was updated successfully, but these errors were encountered: