You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In theory, someone could be confused if this code:
225
+
```rust
226
+
#[derive(From)]
227
+
structNewtype(Inner);
228
+
```
229
+
generates this impl:
230
+
```rust
231
+
implFrom<Inner> forNewtype { ... }
232
+
```
233
+
or this impl:
234
+
```rust
235
+
implFrom<Newtype> forInner { ... }
236
+
```
237
+
However, `impl From<Inner> for Newtype` is consistent with all other standard traits that can currently be derived, as they all generate code in the form of `impl Trait for Type`. It should thus not be very surprising that `#[derive(From)]` provides the impl for the outer type, not the inner type. This will also be clearly documented.
238
+
239
+
Generating the other direction of the impl is best left as a separate feature, which is briefly discussed in [Future possibilities][future-possibilities].
240
+
223
241
# Prior art
224
242
[prior-art]: #prior-art
225
243
@@ -275,24 +293,6 @@ The `Default` trait actually shares a similarity with `From`, in that they are b
275
293
# Unresolvedquestions
276
294
[unresolved-questions]: #unresolved-questions
277
295
278
-
## Directionofthe `From` impl
279
-
Intheory, someonecouldbeconfusedifthiscode:
280
-
```rust
281
-
#[derive(From)]
282
-
structNewtype(Inner);
283
-
```
284
-
generates this impl:
285
-
```rust
286
-
implFrom<Inner> forNewtype { ... }
287
-
```
288
-
or this impl:
289
-
```rust
290
-
implFrom<Newtype> forInner { ... }
291
-
```
292
-
However, `impl From<Inner> for Newtype` is consistent with all other standard traits that can currently be derived, as they all generate code in the form of `impl Trait for Type`. It should thus not be very surprising that `#[derive(From)]` provides the impl for the outer type, not the inner type.
293
-
294
-
This concern was presented [here](https://internals.rust-lang.org/t/pre-rfc-derive-from-for-newtypes/22567/2), with a suggestion to properly document this behavior. The [Guide-level explanation](#guide-level-explanation) discusses this topic.
This is similar to how [RFC#3107](https://rust-lang.github.io/rfcs/3107-derive-default-enum.html) extended the deriving of the `Default` trait using the `#[default]` attribute.
362
362
363
+
## Deriving From in the other direction
364
+
365
+
It is also quite useful to generate `From<InnerType> for Struct`, i.e. generating `From` in the other direction. This could be done in the future using e.g. `#[derive(Into)]`.
366
+
363
367
## Enum support
364
368
We could add support for enums in a similar way, where users could mark the variant that should be constructed using `#[from]`.
0 commit comments