Skip to content

Commit 94fe115

Browse files
committed
Add the as keyword consider into trait RFC
1 parent c892139 commit 94fe115

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
# Reference-level explanation
58+
[reference-level-explanation]: #reference-level-explanation
59+
60+
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.
61+
62+
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.
63+
64+
The internal desing could be a simple syntax sugar to something like that:
65+
66+
```rust
67+
// this actual syntax
68+
let x = Foo(42);
69+
let y = x as Bar;
70+
71+
// is a syntax sugar for
72+
let x = Foo(42);
73+
let y = Into::<Bar>::into(x);
74+
```
75+
76+
# Drawbacks
77+
[drawbacks]: #drawbacks
78+
79+
The `Into` Trait is not in the `std::ops` page and there is a reason why,
80+
it's probably not considered overridable to be used on an operator/keyword like the `as` one.
81+
82+
The `as` keyword differ from the `Into::into` named method, it can be confusing.
83+
84+
# Rationale and alternatives
85+
[alternatives]: #alternatives
86+
87+
With this design we doesn't introduce a new syntax, we just enlarge its possibilities.
88+
89+
The `as` keyword can be confusing with the `Into::into` named method,
90+
we can add a new `into` keyword and/or deprecate the `as` one.
91+
92+
# Unresolved questions
93+
[unresolved]: #unresolved-questions
94+
95+
What about `Copy` on the actual types ? Can it be a problem ?
96+
97+
Is the actual restriction on primitives due to the fact that they implement `Copy` and the conversion is low performance impact ?
98+
99+
How can we handle generic type conversions ?
100+
Do we need to disallow them and only accpet non-generic ones ?
101+
102+
```rust
103+
struct Foo<T>(T);
104+
105+
struct Bar<T>(T);
106+
107+
impl Into<Bar<i64>> for Foo<i32> {
108+
fn into(self) -> Bar<i64> {
109+
Bar(self.0 as i64)
110+
}
111+
}
112+
113+
// this old syntax
114+
let x = Foo(42_i32);
115+
let y: Bar<i64> = x.into();
116+
117+
// is equivalent to the new one
118+
let x = Foo(42);
119+
let y = x as Bar<i64>;
120+
121+
// we can let the type inference guess the generic type
122+
let x = Foo(42_i32);
123+
let y: Bar<_> = x.into();
124+
125+
// equivalent to
126+
let x = Foo(42);
127+
let y = x as Bar<_>;
128+
```

0 commit comments

Comments
 (0)