Skip to content

Commit 04857a8

Browse files
committed
Clarify advice for specific-over-generic impls
1 parent 30cea86 commit 04857a8

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

library/core/src/ops/deref.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,19 @@
5656
/// types.
5757
///
5858
/// Generic implementations, such as for [`Box<T>`][box] (which is generic over
59-
/// every type) should be careful to few methods, since the target type
60-
/// is unknown and therefore every method could collide with one on the target
61-
/// type, causing confusion for users. `Box<T>` has few methods (though
62-
/// several associated functions), partly for this reason.
63-
///
64-
/// Specific implementations, such as for [`String`][string] (which only
65-
/// implements for `Target = str`) can have many methods, since avoiding
59+
/// every type and dereferences to `T`) should be careful to provide few or no
60+
/// methods, since the target type is unknown and therefore every method could
61+
/// collide with one on the target type, causing confusion for users.
62+
/// `impl<T> Box<T>` has no methods (though several associated functions),
63+
/// partly for this reason.
64+
///
65+
/// Specific implementations, such as for [`String`][string] (whose `Deref`
66+
/// implementation has `Target = str`) can have many methods, since avoiding
6667
/// collision is much easier. `String` and `str` both have many methods, and
6768
/// `String` additionally behaves as if it has every method of `str` because of
68-
/// deref coercion.
69+
/// deref coercion. The implementing type may also be generic while the
70+
/// implementation is still specific in this sense; for example, [`Vec<T>`][vec]
71+
/// dereferences to `[T]`, so methods of `T` are not applicable.
6972
///
7073
/// Consider also that deref coericion means that deref traits are a much larger
7174
/// part of a type's public API than any other trait as it is implicitly called
@@ -94,6 +97,7 @@
9497
/// [type coercions]: ../../reference/type-coercions.html
9598
/// [box]: ../../alloc/boxed/struct.Box.html
9699
/// [string]: ../../alloc/string/struct.String.html
100+
/// [vec]: ../../alloc/vec/struct.Vec.html
97101
/// [rc]: ../../alloc/rc/struct.Rc.html
98102
/// [cow]: ../../alloc/borrow/enum.Cow.html
99103
///

0 commit comments

Comments
 (0)