-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Slice with size known at compile time should be coerced to array #1772
Comments
I've been thinking about a more general solution to this.
That's a little surprising in my opinion. |
For I call it "value refinement", that is, you've refined a type to a single value (any pattern can technically be a refinement and you can even have full subtyping with "pattern-refined types" if you wanted to). But it's one of those typesystem extensions that are "less mainstream" than, say, HKT, or even GADTs. |
@est31 No, those are nowhere near allowing |
I'm happy to learn about the arrayref crate because lacking this gets annoying. |
I think should go read about |
@burdges I've wondered about it as well and talked with @eddyb on IRC. In fact, not the Index trait is used here, but the Range struct. The problem is that the Range struct only has fields for the borders, whose value is only known at runtime. @eddyb is proposing that the issue is left until there is a generic way to work even with these values. However, he confirmed that something like |
I see. It's an issue of there being too many things like this, so they should be dealt with using something more general like #1657, as opposed to many little syntax hacks. |
I'd love to have this feature, especially with firmware development where compile-time checks like this are crucial |
So uh, |
It's more like a specialization for indexing a slice by
|
this would be really useful for reading known size ints from a byte slice/vector. |
We'd ideally want I think refinement types sound pretty handy for many things, and we should explore them, but not sure this feature drives that well. Imho, we should simply push for arrayref-like tooling to be considered idiomatic rust, including convenience methods like droundy/arrayref#10 I've noticed many projects avoid arrayref for silly reasons like it's internal usage of unsafe, or that it panics exactly like slicing does, so maybe an RFC adding arrayref and friends to core makes sense. I think const generics change how arrayref should look, which gives good reason for inclusion in core. I think you're two fundamental methods become
There are also mut reference to slices variants that come in pretty handy, like they partially replace the arrayref multi-slicing macros.
I've written these with |
In fact, there is a fairly simple approach by adding 4 or 5 new range types that express the constness.
I believe this already addresses the current issue because We can abstract the split and take machinery as well.
We implement
We now implement
|
I'll clarify, there is currently no way for rustc to infer It sounds hard to infer In all cases however, there is a convoluted aspect to this inference that really does not feel like rust, so.. We should maybe just specify these array slicing types directly, either by writing them our, or with funky notation? |
It feels a bit like the issue is related to staging of computations. It's about how much knowledge can be gleaned at compile time, and from that, what type specialisations or materialisations can be made. Perhaps if the I'm not a fan of adding extra syntax to bake in placeholders for statically and dynamically known limits. We don't need yet more magic characters cluttering up our source. Not knowing much about the compiler guts, this feels like a const eval + type refinement problem. |
There is TryInto now that converts slices to references of arrays, as well as owned arrays if the type impl's Copy: https://doc.rust-lang.org/1.47.0/std/primitive.array.html#impl-TryFrom%3C%26%27a%20%5BT%5D%3E I'll close this, even though the precise feature I requested isn't implemented yet, because the core of the request of easy conversions is covered now. Thanks everyone. |
You should be able to write stuff like:
If subslice notation (
[]
with a range inside) is used on a slice or array with the borders known at compile time, it should be coerced to an array.There is a stackoverflow thread with a list of workarounds, and someone has even done a crate for the problem, but it should be solved inside the language, as it doesn't really feel natural.
Also, if one end is unspecified, it should allow for values only known at runtime for the specified end, and infer the unspecified end from the type of the function. Example code:
The text was updated successfully, but these errors were encountered: