You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Creating a new issue about const-generic array splitting methods since it's been discussed on the array_chunks PR, where it really doesn't belong.
We probably want
impl[T]{fnsplit_array<constN:usize>(&self) -> Option<(&[T;N],&[T])>{ ...}}// requires more const generics work to be accepted by rustc AFAIKimpl<T,constN:usize>[T;N]{fnsplit_array<constM:usize>(&self) -> (&[T;M],&[T;{N - M}]){ ...}}
The possibility of a variadic array splitting function was also mentioned (at least that's my interpretation) but since there's not even an active RFC about variadic generics, I think that's a bit out of scope. The only thing one could do once the second split_array above becomes possible is add split_array_2, split_array_3 and so on.
Reacted by Gurwinder Singh, Luke, technic93 and Mo Rijndael
@DutchGhost pointed out that this could be implemented today by taking 2 const params, but the check that they subdivide the array evenly would have to be a runtime assertion.
I think something that uses traits to impose relational const constraints makes more sense outside of std right now.
yeah, the error message you get when the assert hits is far from pretty and usefull. I'd say its a tradeoff between wanting some ugly compile error whenever the lenghts are wrong, or taking that assertion and finding out the lenghts are wrong during runtime.
One thing to make the error a little bit nicer, is to write the following:
const __ASSERT:&'staticstr =
["The total length of the splitted arrays does not equal the length of the original!"][(!Self::ASSERT)asusize];
Now, if somehow the values of N, N2 and N3 could be displayed in that message, it would be even nicer
@leonardo-m Aren't you the same leonardo from the internals forum who created a thread about this exact feature a week ago? I responded there with an explanation of the state of things in regard to that feature.
In any event the "correct" API is not to have two parameters and constrain them, but to derive the length of the second array by subtracting the a single parameter from the length of the main array. (-> (&[T; M], &[T; N - M])). But the current implementation of const generics does not support that.
Probably it would be better to hash out what APIs we could implement now, and whether its worth adding them to nightly, in a zulip thread than a GitHub issue.
Aren't you the same leonardo from the internals forum who created a thread about this exact feature a week ago?
There I asked for a "where" clause on const integers, while here I've suggested about a more general const_assert that's usable in more/different cases.
Is it possible to add [T; N]::split_array now but adjust the signature so that its use is always a compile-time error? I'd like to land the slice version ASAP but I don't want to run into the same problem we had with into_iter.
Is it possible to add [T; N]::split_array now but adjust the signature so that its use is always a compile-time error? I'd like to land the slice version ASAP but I don't want to run into the same problem we had with into_iter.
This seems easy: just keep the array version unstable?
This is 100% doable in current nightly rust, in a way that simply results in an immediate compile-time error if an invalid split index is provided, and is also completely const-compatible. Here's a playground link that does it as a const impl of the following trait (which is just a stand-in for the actual [T; N] type, of course):
Activity
leonardo-m commentedon Jul 23, 2020
We also need two or more different use cases.
withoutboats commentedon Jul 23, 2020
@DutchGhost pointed out that this could be implemented today by taking 2 const params, but the check that they subdivide the array evenly would have to be a runtime assertion.
We could add the API on nightly with an awareness that we intend to change the API someday.
DutchGhost commentedon Jul 23, 2020
I made an implementation that errors at compiletime when you specify wrong array lengths!
Here a playground link for a PoC: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=52d0664c958d9bca65935b8df6086c33
jethrogb commentedon Jul 23, 2020
use case: Parsing binary data into structs that contain fixed-length arrays
withoutboats commentedon Jul 23, 2020
I think something that uses traits to impose relational const constraints makes more sense outside of std right now.
DutchGhost commentedon Jul 23, 2020
yeah, the error message you get when the assert hits is far from pretty and usefull. I'd say its a tradeoff between wanting some ugly compile error whenever the lenghts are wrong, or taking that assertion and finding out the lenghts are wrong during runtime.
One thing to make the error a little bit nicer, is to write the following:
Now, if somehow the values of
N,N2andN3could be displayed in that message, it would be even nicerleonardo-m commentedon Jul 23, 2020
Compile-time errors are better. This means we need to add something to Rust to perform similar static casts in a nicer and shorter way.
withoutboats commentedon Jul 23, 2020
@leonardo-m Aren't you the same leonardo from the internals forum who created a thread about this exact feature a week ago? I responded there with an explanation of the state of things in regard to that feature.
In any event the "correct" API is not to have two parameters and constrain them, but to derive the length of the second array by subtracting the a single parameter from the length of the main array. (
-> (&[T; M], &[T; N - M])). But the current implementation of const generics does not support that.Probably it would be better to hash out what APIs we could implement now, and whether its worth adding them to nightly, in a zulip thread than a GitHub issue.
leonardo-m commentedon Jul 23, 2020
There I asked for a "where" clause on const integers, while here I've suggested about a more general const_assert that's usable in more/different cases.
mbartlett21 commentedon Jan 17, 2021
Here is an example implementation:
(EDIT: Updated features)
(playground)
jethrogb commentedon Feb 23, 2021
Is it possible to add
[T; N]::split_arraynow but adjust the signature so that its use is always a compile-time error? I'd like to land the slice version ASAP but I don't want to run into the same problem we had withinto_iter.jethrogb commentedon Mar 17, 2021
Another use case: converting binary data to integers using
{integer}::from_Xe_bytesjethrogb commentedon Mar 17, 2021
This seems easy: just keep the array version unstable?
slightlyoutofphase commentedon Apr 2, 2021
This is 100% doable in current nightly rust, in a way that simply results in an immediate compile-time error if an invalid split index is provided, and is also completely
const-compatible. Here's a playground link that does it as aconstimpl of the following trait (which is just a stand-in for the actual[T; N]type, of course):Rollup merge of rust-lang#83233 - jethrogb:split_array, r=yaahc
Rollup merge of rust-lang#83233 - jethrogb:split_array, r=yaahc
Rollup merge of rust-lang#83233 - jethrogb:split_array, r=yaahc
leonardo-m commentedon Oct 25, 2021
To split an array or slice with this functions I'm using:
I guess there isn't a desire for a array method similar to:
jethrogb commentedon Oct 25, 2021
I think I'd use split_array_ref followed by try_from on the rest.
jethrogb commentedon Oct 25, 2021
Check the tracking issue #90091 for more details on exact-length splitting for arrays.