From b2c1f7b9c952c21ebd7816164a7575b905e0a715 Mon Sep 17 00:00:00 2001 From: Christopher Durham Date: Tue, 24 Aug 2021 19:44:48 -0500 Subject: [PATCH] Clarify C-DEREF The ship has flown the nest; `Deref` is not exclusively for smart pointers anymore. Fixes #249, the direct contradiction of C-DEREF with idiomatic std APIs. --- src/predictability.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/predictability.md b/src/predictability.md index a044f0a..6f4d244 100644 --- a/src/predictability.md +++ b/src/predictability.md @@ -133,12 +133,15 @@ so on for the other traits. -## Only smart pointers implement `Deref` and `DerefMut` (C-DEREF) +## `Deref` and `DerefMut` are unsurprising (C-DEREF) The `Deref` traits are used implicitly by the compiler in many circumstances, and interact with method resolution. The relevant rules are designed specifically to accommodate smart pointers, and so the traits should be used -only for that purpose. +with those rules in mind. + +As a general guideline, consider the "_as-a_" rule. If you implement `Deref`, +wherever `&Target` is acceptable, `&Self` works just as well as a `&Target`. ### Examples from the standard library @@ -148,6 +151,8 @@ only for that purpose. - [`Rc`](https://doc.rust-lang.org/std/rc/struct.Rc.html) - [`Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html) - [`Cow<'a, T>`](https://doc.rust-lang.org/std/borrow/enum.Cow.html) +- [`ManuallyDrop`](https://doc.rust-lang.org/stable/std/mem/struct.ManuallyDrop.html) (not a traditional pointer) +- [`AssertUnwindSafe`](https://doc.rust-lang.org/stable/std/panic/struct.AssertUnwindSafe.html) (not a traditional pointer)