Skip to content

Commit b2c1f7b

Browse files
authored
Clarify C-DEREF
The ship has flown the nest; `Deref` is not exclusively for smart pointers anymore. Fixes rust-lang#249, the direct contradiction of C-DEREF with idiomatic std APIs.
1 parent b2f62d6 commit b2c1f7b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/predictability.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,15 @@ so on for the other traits.
133133

134134

135135
<a id="c-deref"></a>
136-
## Only smart pointers implement `Deref` and `DerefMut` (C-DEREF)
136+
## `Deref` and `DerefMut` are unsurprising (C-DEREF)
137137

138138
The `Deref` traits are used implicitly by the compiler in many circumstances,
139139
and interact with method resolution. The relevant rules are designed
140140
specifically to accommodate smart pointers, and so the traits should be used
141-
only for that purpose.
141+
with those rules in mind.
142+
143+
As a general guideline, consider the "_as-a_" rule. If you implement `Deref`,
144+
wherever `&Target` is acceptable, `&Self` works just as well as a `&Target`.
142145

143146
### Examples from the standard library
144147

@@ -148,6 +151,8 @@ only for that purpose.
148151
- [`Rc<T>`](https://doc.rust-lang.org/std/rc/struct.Rc.html)
149152
- [`Arc<T>`](https://doc.rust-lang.org/std/sync/struct.Arc.html)
150153
- [`Cow<'a, T>`](https://doc.rust-lang.org/std/borrow/enum.Cow.html)
154+
- [`ManuallyDrop<T>`](https://doc.rust-lang.org/stable/std/mem/struct.ManuallyDrop.html) (not a traditional pointer)
155+
- [`AssertUnwindSafe<T>`](https://doc.rust-lang.org/stable/std/panic/struct.AssertUnwindSafe.html) (not a traditional pointer)
151156

152157

153158
<a id="c-ctor"></a>

0 commit comments

Comments
 (0)