| 1 |
# What `#[derive(Into)]` generates |
| 2 |
|
| 3 |
This derive creates the exact opposite of `#[derive(From)]`. |
| 4 |
Instead of allowing you to create a new instance of the struct from the values |
| 5 |
it should contain, it allows you to extract the values from the struct. One |
| 6 |
thing to note is that this derive doesn't actually generate an implementation |
| 7 |
for the `Into` trait. Instead, it derives `From` for the values contained in |
| 8 |
the struct and thus has an indirect implementation of `Into` as |
| 9 |
[recommended by the docs][1]. |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
## Structs |
| 15 |
|
| 16 |
For structs with a single field you can call `.into()` to extract the inner type. |
| 17 |
|
| 18 |
```rust |
| 19 |
# use derive_more::Into; |
| 20 |
# |
| 21 |
#[derive(Debug, Into, PartialEq)] |
| 22 |
struct Int(i32); |
| 23 |
|
| 24 |
assert_eq!(2, Int(2).into()); |
| 25 |
``` |
| 26 |
|
| 27 |
For structs having multiple fields, `.into()` extracts a tuple containing the |
| 28 |
desired content for each field. |
| 29 |
|
| 30 |
```rust |
| 31 |
# use derive_more::Into; |
| 32 |
# |
| 33 |
#[derive(Debug, Into, PartialEq)] |
| 34 |
struct Point(i32, i32); |
| 35 |
|
| 36 |
assert_eq!((1, 2), Point(1, 2).into()); |
| 37 |
``` |
| 38 |
|
| 39 |
To specify concrete types for deriving conversions into, use `#[into(<types>)]`. |
| 40 |
|
| 41 |
```rust |
| 42 |
# use std::borrow::Cow; |
| 43 |
# |
| 44 |
# use derive_more::Into; |
| 45 |
# |
| 46 |
#[derive(Debug, Into, PartialEq)] |
| 47 |
#[into(Cow<'static, str>, String)] |
| 48 |
struct Str(Cow<'static, str>); |
| 49 |
|
| 50 |
assert_eq!("String".to_owned(), String::from(Str("String".into()))); |
| 51 |
assert_eq!(Cow::Borrowed("Cow"), <Cow<_>>::from(Str("Cow".into()))); |
| 52 |
|
| 53 |
#[derive(Debug, Into, PartialEq)] |
| 54 |
#[into((i64, i64), (i32, i32))] |
| 55 |
struct Point { |
| 56 |
x: i32, |
| 57 |
y: i32, |
| 58 |
} |
| 59 |
|
| 60 |
assert_eq!((1_i64, 2_i64), Point { x: 1_i32, y: 2_i32 }.into()); |
| 61 |
assert_eq!((3_i32, 4_i32), Point { x: 3_i32, y: 4_i32 }.into()); |
| 62 |
``` |
| 63 |
|
| 64 |
In addition to converting to owned types, this macro supports deriving into |
| 65 |
reference (mutable or not) via `#[into(ref(...))]`/`#[into(ref_mut(...))]`. |
| 66 |
|
| 67 |
```rust |
| 68 |
# use derive_more::Into; |
| 69 |
# |
| 70 |
#[derive(Debug, Into, PartialEq)] |
| 71 |
#[into(owned, ref(i32), ref_mut)] |
| 72 |
struct Int(i32); |
| 73 |
|
| 74 |
assert_eq!(2, Int(2).into()); |
| 75 |
assert_eq!(&2, <&i32>::from(&Int(2))); |
| 76 |
assert_eq!(&mut 2, <&mut i32>::from(&mut Int(2))); |
| 77 |
``` |
| 78 |
|
| 79 |
In case there are fields, that shouldn't be included in the conversion, use the |
| 80 |
`#[into(skip)]` attribute. |
| 81 |
|
| 82 |
```rust |
| 83 |
# use std::marker::PhantomData; |
| 84 |
# |
| 85 |
# use derive_more::Into; |
| 86 |
# |
| 87 |
# struct Gram; |
| 88 |
# |
| 89 |
#[derive(Debug, Into, PartialEq)] |
| 90 |
#[into(i32, i64, i128)] |
| 91 |
struct Mass<Unit> { |
| 92 |
value: i32, |
| 93 |
#[into(skip)] |
| 94 |
_unit: PhantomData<Unit>, |
| 95 |
} |
| 96 |
|
| 97 |
assert_eq!(5, Mass::<Gram>::new(5).into()); |
| 98 |
assert_eq!(5_i64, Mass::<Gram>::new(5).into()); |
| 99 |
assert_eq!(5_i128, Mass::<Gram>::new(5).into()); |
| 100 |
# |
| 101 |
# impl<Unit> Mass<Unit> { |
| 102 |
# fn new(value: i32) -> Self { |
| 103 |
# Self { |
| 104 |
# value, |
| 105 |
# _unit: PhantomData, |
| 106 |
# } |
| 107 |
# } |
| 108 |
# } |
| 109 |
``` |
| 110 |
|
| 111 |
## Enums |
| 112 |
|
| 113 |
Deriving `Into` for enums is not supported as it would not always be successful, |
| 114 |
so `TryInto` should be used instead. |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
[1]: https://doc.rust-lang.org/core/convert/trait.Into.html |