-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add IndexRef and IndexMut #11977
Add IndexRef and IndexMut #11977
Conversation
This patch adds two new traits that implement aspects of index overloading. It retains the existing Index trait, which takes an &Element and returns a Result. This is useful for objects whose indexing behavior is to return simple values like booleans or numbers. It adds a new IndexRef trait, which takes an &Element and returns a &Result. This is useful for objects whose indexing behavior is to return larger values where cloning would be awkward, inappropriate or impossible. It also adds a new IndexMut trait, which takes self as &mut self, takes an &Element and returns a &mut Result. This is useful for objects that want to allow mutations to their indexed results. The Index and IndexRef trait both use the `index` method name, so that only one of the two can be implemented by a given struct. The IndexMut trait uses the `index_mut` method name, so it can be implemented alongside one of Index or IndexRef.
I'm not indicating that this PR closes #6515, because I am waiting on feedback to this PR before I continue on to |
Woo! Glad to see this making progress. 🚂 |
@pcwalton yes, I plan to review today / tonight. It seems the work we land here will also pave the way for overloadable deref, since many of the complications are the same. |
Huh. This is ... surprisingly short! I have to digest it again. I think it's clear this is not quite how I see things working longer term but it might be a useful short step, since having a functional |
@nikomatsakis Still hoping to hear your opinion. |
I did talk to @wycats on IRC at one point. I am trying to write up a |
Closing due to inactivity, but I'd love to see this get done! |
…hearth don't visit nested bodies in `is_const_evaluatable` Fixes rust-lang#11939 This ICE happened in `if_let_some_else_none`, but the root problem is in one of the utils that it uses. It is (was) possible for `is_const_evalutable` to visit nested bodies which would lead to it trying to get the type of one of the expressions with the wrong typeck table, which won't have the type stored. Notably, for the expression `Bytes::from_static(&[0; 256 * 1024]);` in the linked issue, the array length is an anonymous const in which type checking happens on its own, so we can't use the typeck table of the enclosing function in there. Visiting nested bodies is also not needed for checking whether an expression can be const, so I think it's safe to ignore just ignore them altogether. changelog: Fix ICE when checking for constness in nested bodies
This patch adds two new traits that implement aspects of index
overloading.
It retains the existing Index trait, which takes an &Element and returns
a Result. This is useful for objects whose indexing behavior is to
return simple values like booleans or numbers.
It adds a new IndexRef trait, which takes an &Element and returns a
&Result. This is useful for objects whose indexing behavior is to return
larger values where cloning would be awkward, inappropriate or
impossible.
It also adds a new IndexMut trait, which takes self as &mut self, takes
an &Element and returns a &mut Result. This is useful for objects that
want to allow mutations to their indexed results.
The Index and IndexRef trait both use the
index
method name, so thatonly one of the two can be implemented by a given struct. The IndexMut
trait uses the
index_mut
method name, so it can be implementedalongside one of Index or IndexRef.