| 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)]` (or `#[into(ignore)]`) 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)] // or #[into(ignore)] |
| 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 | |
| 112 | ### Fields |
| 113 | |
| 114 | The `#[into]` attribute can also be applied to specific fields of a struct. |
| 115 | |
| 116 | ```rust |
| 117 | # use derive_more::Into; |
| 118 | # |
| 119 | #[derive(Into)] |
| 120 | struct Data { |
| 121 | id: i32, |
| 122 | #[into] |
| 123 | raw: f64 |
| 124 | } |
| 125 | |
| 126 | assert_eq!(42.0, Data { id: 1, raw: 42.0 }.into()); |
| 127 | ``` |
| 128 | |
| 129 | In such cases, no conversion into a tuple of all fields is generated, unless |
| 130 | an explicit struct attribute is present. |
| 131 | |
| 132 | ```rust |
| 133 | # use derive_more::Into; |
| 134 | # |
| 135 | #[derive(Into)] |
| 136 | #[into] |
| 137 | struct Data { |
| 138 | id: i32, |
| 139 | #[into] |
| 140 | raw: f64 |
| 141 | } |
| 142 | |
| 143 | assert_eq!(42.0, Data { id: 1, raw: 42.0 }.into()); |
| 144 | assert_eq!((1, 42.0), Data { id: 1, raw: 42.0 }.into()); |
| 145 | ``` |
| 146 | |
| 147 | The `#[into(<types>)]` syntax can be used on fields as well. |
| 148 | |
| 149 | ```rust |
| 150 | # use std::marker::PhantomData; |
| 151 | # use derive_more::Into; |
| 152 | # struct Whatever; |
| 153 | # |
| 154 | #[derive(Into, Clone)] |
| 155 | #[into(owned, ref((u8, str)), ref_mut)] |
| 156 | struct Foo { |
| 157 | #[into(owned(u64), ref)] |
| 158 | a: u8, |
| 159 | b: String, |
| 160 | #[into(skip)] |
| 161 | _c: PhantomData<Whatever>, |
| 162 | } |
| 163 | |
| 164 | let mut foo = Foo { a: 1, b: "string".to_owned(), _c: PhantomData }; |
| 165 | |
| 166 | assert_eq!((1_u8, "string".to_owned()), foo.clone().into()); |
| 167 | assert_eq!((&1_u8, "string"), <(&u8, &str)>::from(&foo)); |
| 168 | assert_eq!((&mut 1_u8, &mut "string".to_owned()), <(&mut u8, &mut String)>::from(&mut foo)); |
| 169 | assert_eq!(1_u64, foo.clone().into()); |
| 170 | assert_eq!(&1_u8, <&u8>::from(&foo)); |
| 171 | ``` |
| 172 | |
| 173 | Fields, having specific conversions into them, can also be skipped for top-level |
| 174 | tuple conversions. |
| 175 | |
| 176 | ```rust |
| 177 | # use derive_more::Into; |
| 178 | |
| 179 | #[derive(Into)] |
| 180 | #[into(ref((str, f64)))] |
| 181 | struct Foo { |
| 182 | #[into(ref)] |
| 183 | #[into(skip)] |
| 184 | a: u8, |
| 185 | b: String, |
| 186 | c: f64, |
| 187 | } |
| 188 | |
| 189 | let foo = Foo { a: 1, b: "string".to_owned(), c: 3.0 }; |
| 190 | |
| 191 | assert_eq!(("string", &3.0), (&foo).into()); |
| 192 | assert_eq!(&1_u8, <&u8>::from(&foo)); |
| 193 | ``` |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | ## Enums |
| 199 | |
| 200 | Deriving `Into` for enums is not supported as it would not always be successful, |
| 201 | so `TryInto` should be used instead. |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | [1]: https://doc.rust-lang.org/core/convert/trait.Into.html |