6
6
# Summary
7
7
[ summary ] : #summary
8
8
9
- With specialization on the way, we need to talk about the semantics of
10
- ` <T as Clone>::clone() where T: Copy ` .
9
+ Clarify semantics of ` Clone ` so that users are permitted to assume that
10
+ ` <T as Clone>::clone() where T: Copy ` is implemented by ` memcpy ` .
11
11
12
12
It's generally been an unspoken rule of Rust that a ` clone ` of a ` Copy ` type is
13
13
equivalent to a ` memcpy ` of that type; however, that fact is not documented
@@ -34,15 +34,19 @@ compatible to upgrade to Clone in the future if demand is high enough."
34
34
# Detailed design
35
35
[ design ] : #detailed-design
36
36
37
- Specify that ` <T as Clone>::clone(t) ` shall be equivalent to ` ptr::read(t) `
38
- where ` T: Copy, t: &T ` . An implementation that does not uphold this * shall not*
39
- result in undefined behavior; ` Clone ` is not an ` unsafe trait ` .
37
+ Specify that users may assume ` <T as Clone>::clone(t) ` to be equivalent to
38
+ ` ptr::read(t) ` where ` T: Copy, t: &T ` . An implementation that does not uphold
39
+ this * shall not* result in undefined behavior; ` Clone ` is not an `unsafe
40
+ trait`.
40
41
41
42
Also add something like the following sentence to the documentation for the
42
43
` Clone ` trait:
43
44
44
45
"If ` T: Copy ` , ` x: T ` , and ` y: &T ` , then ` let x = y.clone(); ` is equivalent to
45
- ` let x = *y; ` . Manual implementations must be careful to uphold this."
46
+ ` let x = *y; ` . Manual implementations should be careful to uphold this
47
+ invariant, as users * may* assume that it holds (however the compiler itself
48
+ * will not* make that assumption when compiling calls to ` Clone::clone ` ,
49
+ guaranteeing that the relevant ` clone ` method is always invoked)."
46
50
47
51
# Drawbacks
48
52
[ drawbacks ] : #drawbacks
0 commit comments