File tree 2 files changed +10
-4
lines changed
2 files changed +10
-4
lines changed Original file line number Diff line number Diff line change 32
32
/// on the identical behavior of these additional trait implementations.
33
33
/// These traits will likely appear as additional trait bounds.
34
34
///
35
+ /// In particular `Eq`, `Ord` and `Hash` must be equivalent for
36
+ /// borrowed and owned values: `x.borrow() == y.borrow()` should give the
37
+ /// same result as `x == y`.
38
+ ///
35
39
/// If generic code merely needs to work for all types that can
36
40
/// provide a reference to related type `T`, it is often better to use
37
41
/// [`AsRef<T>`] as more types can safely implement it.
Original file line number Diff line number Diff line change @@ -105,11 +105,13 @@ pub const fn identity<T>(x: T) -> T { x }
105
105
/// `&T` or write a custom function.
106
106
///
107
107
///
108
- /// `AsRef` is very similar to , but serves a slightly different purpose than [`Borrow`] :
108
+ /// `AsRef` has the same signature as [`Borrow`] , but `Borrow` is different in few aspects :
109
109
///
110
- /// - Use `AsRef` when the goal is to simply convert into a reference
111
- /// - Use `Borrow` when the goal is related to writing code that is agnostic to
112
- /// the type of borrow and whether it is a reference or value
110
+ /// - Unlike `AsRef`, `Borrow` has a blanket impl for any `T`, and can be used to accept either
111
+ /// a reference or a value.
112
+ /// - `Borrow` also requires that `Hash`, `Eq` and `Ord` for borrowed value are
113
+ /// equivalent to those of the owned value. For this reason, if you want to
114
+ /// borrow only a single field of a struct you can implement `AsRef`, but not `Borrow`.
113
115
///
114
116
/// [`Borrow`]: ../../std/borrow/trait.Borrow.html
115
117
///
You can’t perform that action at this time.
0 commit comments