Skip to content

Commit 0db3932

Browse files
authored
Rollup merge of rust-lang#129938 - chancancode:patch-1, r=thomcc
Elaborate on deriving vs implementing `Copy` I was reading this documentation and this wasn't immediately clear to me. In my mind, it seemed obvious that a type can only claim to be `Copy` if the bits it is storing can be `Copy`, and in the case of a generic struct that can only be the case if `T: Copy`. So the bound added by the derive seemed necessary at all times, and I thought what the documentation was trying to say is that the custom implementation allows you to add _additional bounds_. Of course what it was actually trying to point out is that just because you have a generic parameter `T`, it doesn't necessarily mean you are storing the bits of `T`. And if you aren't, it may be the case that your own bits can be copied regardless of whether the bits of `T` can be safely copied. Thought it may be worth elaborating to make that a bit more clear. Haven't tested/didn't try to figure out how to render this locally. Mainly not sure if the `PhantomData` back link is going to just work or need some extra stuff, but I figured someone else probably could just tell.
2 parents 9be97ae + e45b53e commit 0db3932

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

library/core/src/marker.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,19 @@ marker_impls! {
288288
/// }
289289
/// ```
290290
///
291-
/// There is a small difference between the two: the `derive` strategy will also place a `Copy`
292-
/// bound on type parameters, which isn't always desired.
291+
/// There is a small difference between the two. The `derive` strategy will also place a `Copy`
292+
/// bound on type parameters:
293+
///
294+
/// ```
295+
/// #[derive(Clone)]
296+
/// struct MyStruct<T>(T);
297+
///
298+
/// impl<T: Copy> Copy for MyStruct<T> { }
299+
/// ```
300+
///
301+
/// This isn't always desired. For example, shared references (`&T`) can be copied regardless of
302+
/// whether `T` is `Copy`. Likewise, a generic struct containing markers such as [`PhantomData`]
303+
/// could potentially be duplicated with a bit-wise copy.
293304
///
294305
/// ## What's the difference between `Copy` and `Clone`?
295306
///

0 commit comments

Comments
 (0)