Skip to content

Commit 1285b70

Browse files
committed
Add the as keyword consider into trait RFC
1 parent c892139 commit 1285b70

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
- Feature Name: `as_keyword_consider_into_trait`
2+
- Start Date: 2018-01-21
3+
- RFC PR:
4+
- Rust Issue:
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Permit to use the common `as` keyword with any type that implement the `Into` Trait,
10+
allowing explicit conversions whose primitives already benefit, more visible than simple function calls.
11+
12+
# Motivation
13+
[motivation]: #motivation
14+
15+
Many operators are allowed to be implemented permitting a much better programming flow:
16+
- the `+`, `-`, `*`, `\` operators are the result of the `Add`, `Sub`, `Mul`, `Div` Traits implementations.
17+
- the `+=`, `-=`, `*=`, `\=` operators are the result of the `AddAssign`, `SubAssign`, `MulAssign`, `DivAssign` Traits implementations.
18+
- the `container[index]` operator is the result of the `Index` and `IndexMut` Traits implementations.
19+
- and many others can be found on the [`std::ops`](https://doc.rust-lang.org/std/ops/) documentation page.
20+
21+
The `as` operator is actually reserved to primitives only but these given primitives already implement `Into` Traits for all possible conversions:
22+
- the `u32` implement `Into<u64>` in the form of an [`impl From<u32> for u64`](https://doc.rust-lang.org/std/primitive.u64.html#impl-From%3Cu32%3E).
23+
- the `u8` implement `Into<u16>` in the form of an [`impl From<u8> for u16`](https://doc.rust-lang.org/std/primitive.u16.html#impl-From%3Cu8%3E).
24+
25+
All of these conversions can be found in the [`libcore/num/mod.rs` file](https://github.com/rust-lang/rust/blob/d9d5c667d819ce400fc7adb09dcd6482b0aa519e/src/libcore/num/mod.rs#L3343-L3400).
26+
27+
Some special primitives like `usize` and `isize` doesn't implement many `From` Traits because these are [the representation of the address register](https://users.rust-lang.org/t/cant-convert-usize-to-u64/6243) and it can break compilation on some architectures.
28+
29+
Adding this feature to the compiler will probably unify the [actual conversion error detections](https://users.rust-lang.org/t/cant-convert-usize-to-u64/6243/8) of the `as` keyword and the `into` method.
30+
31+
# Guide-level explanation
32+
[guide-level-explanation]: #guide-level-explanation
33+
34+
The actual design is not far from what we already know of the `as` keyword.
35+
It doesn't add new grammar to the language, it add more freedom to the actual syntax:
36+
37+
```rust
38+
struct Foo(i32);
39+
40+
struct Bar(i32);
41+
42+
impl Into<Bar> for Foo {
43+
fn into(self) -> Bar {
44+
Bar(self.0)
45+
}
46+
}
47+
48+
// this old syntax
49+
let x = Foo(42);
50+
let y: Bar = x.into();
51+
52+
// is equivalent to the new one
53+
let x = Foo(42);
54+
let y = x as Bar;
55+
```
56+
57+
The actual syntax **is not deprecated in any way**, it has to mimic the `Add::add` or the `Deref::deref` methods and permit the user to directly call these methods or use the operators as he likes.
58+
59+
# Reference-level explanation
60+
[reference-level-explanation]: #reference-level-explanation
61+
62+
The `as` keyword is use to reflect the actual `Into::into` method call, if the `Into` Trait is implemented for the type to be converted, the compiler needs to emit an error informing the user that the conversion is not possible.
63+
64+
The actual `as` keyword is reserved for primitives to convert into other primitives but it shouldn't be a breaking change to change the behavior and to work with any type implementing `Into` because the primitives already implement these Traits.
65+
66+
The internal desing could be a simple syntax sugar to something like that:
67+
68+
```rust
69+
// this actual syntax
70+
let x = Foo(42);
71+
let y = x as Bar;
72+
73+
// is a syntax sugar for
74+
let x = Foo(42);
75+
let y = Into::<Bar>::into(x);
76+
```
77+
78+
# Drawbacks
79+
[drawbacks]: #drawbacks
80+
81+
The `Into` Trait is not in the `std::ops` page and there is a reason why,
82+
it's probably not considered overridable to be used on an operator/keyword like the `as` one.
83+
84+
The `as` keyword differ from the `Into::into` named method, it can be confusing.
85+
86+
# Rationale and alternatives
87+
[alternatives]: #alternatives
88+
89+
With this design we doesn't introduce a new syntax, we just enlarge its possibilities.
90+
91+
The `as` keyword can be confusing with the `Into::into` named method,
92+
we can add a new `into` keyword and/or deprecate the `as` one.
93+
94+
# Unresolved questions
95+
[unresolved]: #unresolved-questions
96+
97+
What about `Copy` on the actual types ? Can it be a problem ?
98+
99+
Is the actual restriction on primitives due to the fact that they implement `Copy` and the conversion is low performance impact ?
100+
101+
How can we handle generic type conversions ?
102+
Do we need to disallow them and only accpet non-generic ones ?
103+
104+
```rust
105+
struct Foo<T>(T);
106+
107+
struct Bar<T>(T);
108+
109+
impl Into<Bar<i64>> for Foo<i32> {
110+
fn into(self) -> Bar<i64> {
111+
Bar(self.0 as i64)
112+
}
113+
}
114+
115+
// this old syntax
116+
let x = Foo(42_i32);
117+
let y: Bar<i64> = x.into();
118+
119+
// is equivalent to the new one
120+
let x = Foo(42);
121+
let y = x as Bar<i64>;
122+
123+
// we can let the type inference guess the generic type
124+
let x = Foo(42_i32);
125+
let y: Bar<_> = x.into();
126+
127+
// equivalent to
128+
let x = Foo(42);
129+
let y = x as Bar<_>;
130+
```

0 commit comments

Comments
 (0)