Skip to content

Commit 0502703

Browse files
Add copy ergonomics design note
1 parent e2baded commit 0502703

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@
2929
- [Auto traits](./design_notes/auto_traits.md)
3030
- [Eager drop](./design_notes/eager_drop.md)
3131
- [Autoderef and autoref in operators](./design_notes/autoref_ops.md)
32+
- [Copy type ergonomics](./design_notes/copy_ergonomics.md)

src/design_notes/copy_ergonomics.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Copy type ergonomics
2+
3+
## Background
4+
5+
There are a number of pain points with `Copy` types that the lang team is
6+
interested in exploring, though active experimentation is not currently ongoing.
7+
8+
Some key problems are:
9+
10+
## `Copy` cannot be implemented with non-`Copy` members
11+
12+
There are standard library types where the lack of a `Copy` impl is an
13+
active pain point, e.g., [`MaybeUninit`](https://github.com/rust-lang/rust/issues/62835)
14+
and [`UnsafeCell`](https://github.com/rust-lang/rust/issues/25053), when the
15+
contained type is actually `Copy`.
16+
17+
### History
18+
19+
* `unsafe impl Copy for T` which avoids the requirement that T is recursively
20+
Copy, but is obviously unsafe.
21+
* https://github.com/rust-lang/rust/issues/25053#issuecomment-218610508
22+
* `Copy` is dangerous on types like `UnsafeCell` where `&UnsafeCell<T>`
23+
otherwise would not permit access to `T` in [safe
24+
code](https://github.com/rust-lang/rust/issues/25053#issuecomment-98447164).
25+
26+
## `Copy` types can be (unintentionally) copied
27+
28+
Even if a type is Copy (e.g., `[u8; 1024]`) it may not be a good idea to make
29+
use of that in practice, since copying large amounts of data is slow. This is
30+
primarily a performance concern, so the problem is usually that these copies are
31+
easy to miss. However, depending on the size of the buffer, it can also be a
32+
correctness concern as it may cause an unintended stack overflow with too many
33+
accidental copies.
34+
35+
Should we want to lint on this code, deciding on a size threshold may be
36+
difficult. It's not generally possible for the compiler to know whether a
37+
particular copy operation is likely to lead to stack overflow or undesirable
38+
performance. We don't have examples yet of cases where there's desirable large
39+
copies (that should not be linted against) or concrete cases where the copies
40+
are accidental; collecting this information would be worthwhile.
41+
42+
Implementations of `Copy` on closures and arrays are the prime example of Rust
43+
currently being overeager with the defaults in some contexts.
44+
45+
This also comes up with `Copy` impls on `Range`, which would generally be
46+
desirable but is error-prone given the `Iterator/IntoIterator` impls on ranges.
47+
48+
The example here does not compile today (since Range is not Copy), but would be
49+
unintuitive if it did.
50+
51+
```rust,compile_fail
52+
let mut x = 0..10;
53+
let mut c = move || x.next();
54+
println!("{:?}", x.next()); // prints 0
55+
println!("{:?}", c()); // prints 0, because the captured x is implicitly copied.
56+
```
57+
58+
This example illustrates the range being copied into the closure, while the user
59+
may have expected the name "x" to refer to the same range in both cases.
60+
61+
The move keyword here likely disambiguates this particular case for users, but
62+
in closures with more captures it may be not as obvious that the range type in
63+
particular was copied in.
64+
65+
A lint has been [proposed](https://github.com/rust-lang/rust/issues/45683) to
66+
permit Copy impls on types where Copy is likely not desirable with particular
67+
conditions (e.g., Copy of IntoIterator-implementing types after iteration).
68+
69+
Note that "large copies" comes up with moves as well (which are copies, just
70+
taking ownership as well), so a size-based lint is plausibly desirable for both.
71+
72+
### History
73+
74+
* Proposed lint: [#45683](https://github.com/rust-lang/rust/issues/45683)
75+
76+
## References to `Copy` types
77+
78+
Frequently when dealing with code generic over T you end up needing things like
79+
`[u8]::contains(&5)` which is ugly and annoying. Iterators of copy types also
80+
produce `&&u64` and similar constructs which can produce unexpected type errors.
81+
82+
```rust
83+
for x in &vec![1, 2, 3, 4, 5, 6, 7] {
84+
process(*x); // <-- annoying that we need `*x`
85+
}
86+
87+
fn process(x: i32) { }
88+
```
89+
90+
```rust
91+
fn sum_even(v: &[u32]) -> u32 {
92+
// **v is annoying
93+
v.iter().filter(|v| **v % 2 == 0).sum()
94+
}
95+
```
96+
97+
Note that this means that you in most cases want to "boil down" to the inner
98+
type when dealing with references, i.e., `&&u32` you actually want `u32`, not
99+
`&u32`. Notably, though, this may *not* be true if the Copy type is something
100+
more complex (e.g., a future Copy Cell), since then `&Cell` is quite different
101+
from a `Cell`, the latter being likely useless for modification at least.
102+
103+
There is also plausibly performance left on the table with types like `&&u64`.
104+
105+
Note that this interacts with the unintentional copies (especially of large
106+
structures).
107+
108+
This could plausibly be done with moved values as well, so long as the
109+
semantics match the syntax (e.g. `wants_ref(foo)` acts like `wants_ref(&{foo})`)
110+
similar to how one can pass `&mut` to something that only wants `&`.
111+
This would be a tradeoff: in some cases people may want the type-checker to flag such cases and require explicitly taking a reference, while in other cases people may want the compiler to automatically make such code work. We would want to consider and evaluate this tradeoff, and whether we can usefully separate such cases.
112+
113+
### History
114+
115+
* [RFC 2111 (not merged)](https://github.com/rust-lang/rfcs/pull/2111)
116+
* [Rust tracking issue (closed)](https://github.com/rust-lang/rust/issues/44763)
117+
* "Allow owned values where references are expected" in [rust-roadmap-2017#17](https://github.com/rust-lang/rust-roadmap-2017/issues/17)

0 commit comments

Comments
 (0)