-
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
Clarify documentation of traits with behavioral contracts (PartialOrd etc.) #73682
Comments
Related closed RFC here: rust-lang/rfcs#956 |
I was unsure at first if it will be possible to implement all necessary unsafe operations soundly without relying on these behavioral contracts, but after studying data structures in libstd, it seems quite possible. They use proxy objects with transactional methods that will keep the invariant of the underlying data type in all situations. I think these design patterns are worth being noted in Rustonomicon. |
From my comment here:
|
Rollup merge of rust-lang#115607 - RalfJung:safe-traits-unsafe-code, r=dtolnay clarify that unsafe code must not rely on our safe traits This adds a disclaimer to PartialEq, Eq, PartialOrd, Ord, Hash, Deref, DerefMut. We already have a similar disclaimer in ExactSizeIterator (worded a bit differently): ``` /// Note that this trait is a safe trait and as such does *not* and *cannot* /// guarantee that the returned length is correct. This means that `unsafe` /// code **must not** rely on the correctness of [`Iterator::size_hint`]. The /// unstable and unsafe [`TrustedLen`](super::marker::TrustedLen) trait gives /// this additional guarantee. ``` If there are any other traits that should carry such a disclaimer, please let me know. Fixes rust-lang#73682
Re-draft Deref docs Make general advice more explicit and note the difference between generic and specific implementations. Re-draft DerefMut docs in-line with Deref Fix Deref docs typos Fix broken links Clarify advice for specific-over-generic impls Add comment addressing Issue rust-lang#73682 x fmt Copy faillibility warning to DerefMut
@Qwaz points out that
However, all of these are safe traits, so unsafe code must not rely on such properties. If it does, we have a soundness bug.
I don't think Rust has RFC-style standardization of must/should/etc (maybe it should^^), but at least we should clarify these docs here I feel. Also given that safe code can break these promises, I wonder if "must" is appropriate. Maybe a better wording would be something like
The text was updated successfully, but these errors were encountered: