Skip to content

Commit b62140a

Browse files
Autoref/autoderef
1 parent 7645e56 commit b62140a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
- [Proposing a new project](./proposing_a_project.md)
1010
- [Design notes](./design_notes.md)
1111
- [Allowing integer literals like `1` to be inferred to floating point](./design_notes/int_literal_as_float.md)
12+
- [Autoderef and autoref in operators](./design_notes/autoref_ops.md)

src/design_notes/autoref_ops.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Autoref/Autoderef in operators
2+
3+
Rust permits overriding most of the operators (e.g., `+`, `-`, `+=`). In part
4+
due to `Copy` types not auto-dereferencing, it is common to have `T + &T` or `&T
5+
+ &T`, with potentially many levels of indirection on either side of the
6+
operator.
7+
8+
There is desire in general to avoid needing to both add impls for referenced
9+
versions of types because they:
10+
11+
- bloat documentation
12+
- are never quite sufficient (always more references are possible)
13+
- can cause inference regressions, as the compiler cannot in general know that
14+
`&T + &T` is equivalent to `u64 + u64`.
15+
16+
## History
17+
18+
The standard library initially had just the basic impls of the operator traits
19+
(e.g., `impl Add<u64> for u64`) but has since gained `&u64 + &u64`, `u64 + &u64`
20+
and `&u64 + u64`. These impls usually cause some amount of inference breakage in
21+
practice.
22+
23+
Especially with non-Copy types (for example bigints), forcing users to add references can be
24+
increasingly verbose: `&u * &(&(&u.square() + &(&a * &u)) + &one)`, for example.
25+
26+
There have also been a number of discussions on RFCs and issues, including:
27+
- [Rust tracking issue #44762](https://github.com/rust-lang/rust/issues/44762)
28+
- This includes some implementation/mentoring notes.
29+
- [RFC 2147](https://github.com/rust-lang/rfcs/pull/2147)

0 commit comments

Comments
 (0)