-
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
Introduce non-panicking borrow methods on RefCell<T>
#1660
Conversation
I’ve commented before in favor of this to replace |
cc rust-lang/rust#27733 where a bit of discussion about this has already taken place, especially with respect to breaking the convention of not having panicking and non-panicking methods on a type (in my mind the major drawback here). |
Seems like with respect to that convention, we should have only had the optional ones from day one? [Indexing, brought up in the linked thread, additionally had to be panic because otherwise So +1 this, and maybe even deprecate the old API. |
We (Servo) has only one use case for such a feature in ipc-channel, I wouldn't like to see its panicking counterpart be deprecated. |
Personally I think "don't have both panicking and non-panicking methods" is a fine convention. But it's still just a convention. A guideline, a rule of thumb. I don't think it makes sense to tie ourselves in knots around it if it's clearly not ideal for a particular case. |
Whenever something is built from user input, for example a graph in which nodes | ||
are `RefCell<T>` values, it is primordial to avoid panicking on bad input. The | ||
only way to avoid panics on cyclic input in this case is a way to | ||
conditionally-borrow the cell contents.D |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@alexcrichton There is precedent in slices that have both That said, the |
It's worth mentioning here as well, but the major drawback of |
Also, if one can work around an already-borrowed RefCell, the |
I still prefer
|
🔔 This RFC is now entering its week-long final comment period 🔔 The libs team discussed this and we feel pretty comfortable adding these methods at this point, so we're thinking of merging. |
I'm 👍 on this change, and agree with @glaebhoerl that conventions need to be flexible. We should think carefully about the name here, because we're also establishing a kind of convention for non-panicking alternatives. In particular, That said, no better name is immediately leaping to mind, and I'm happy to accept the RFC and see whether we can think of anything better prior to stabilization. If we end up with overlapping conventions, it's not the end of the world. |
I like this as-is. The |
Yeah, I forgot to mention we're effectively doing the same thing with Anyway, I sort of doubt we'll be able to find a better prefix. |
|
But |
@arielb1 The only reason Add and RefCell are different is because turning off RefCell checking in release mode would allow memory safety violation. The current Add behavior is also not guaranteed in perpetuity; it can switch to panic in release or overflow in debug in future versions. |
The libs team got a chance to discuss this RFC yesterday, and the decision was to merge. We've long felt that this function would likely come into existence so it seems reasonable to merge as unstable at least. We're still a little iffy on the naming (e.g. That being said, these APIs will remain unstable in the standard library before actually stabilizing, and the libs team is fine having a point of stabilization be "finalize the conventions around these sorts of fallible functions", so that's something that we can postpone and doesn't need to block this RFC. @nox one point brought up though is that we probably want these functions to return a |
@alexcrichton Done. |
Thanks again for the RFC @nox! |
No description provided.