Skip to content

Commit 30033b4

Browse files
document assumptions about Clone and Eq traits
1 parent e05ab47 commit 30033b4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

library/core/src/clone.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,30 @@ mod uninit;
139139
/// // Note: With the manual implementations the above line will compile.
140140
/// ```
141141
///
142+
/// ## `Clone` and `PartialEq`/`Eq`
143+
/// `Clone` is intended for the duplication of objects. Consequently, when implementing
144+
/// both `Clone` and [`PartialEq`], the following property is expected to hold:
145+
/// ```text
146+
/// x == x -> x.clone() == x
147+
/// ```
148+
/// In other words, if an object compares equal to itself,
149+
/// its clone must also compare equal to the original.
150+
///
151+
/// For types that also implement [`Eq`] – for which `x == x` always holds –
152+
/// this implies that `x.clone() == x` must always be true.
153+
/// Standard library collections such as
154+
/// [`HashMap`], [`HashSet`], [`BTreeMap`], [`BTreeSet`] and [`BinaryHeap`]
155+
/// rely on their keys respecting this property for correct behavior.
156+
///
157+
/// This property is automatically satisfied when deriving both `Clone` and [`PartialEq`]
158+
/// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`]
159+
/// using `#[derive(Clone, PartialEq, Eq)]`.
160+
///
161+
/// Violating this property is a logic error. The behavior resulting from a logic error is not
162+
/// specified, but users of the trait must ensure that such logic errors do *not* result in
163+
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
164+
/// methods.
165+
///
142166
/// ## Additional implementors
143167
///
144168
/// In addition to the [implementors listed below][impls],

0 commit comments

Comments
 (0)