| 1 | # What `#[derive(TryInto)]` generates |
| 2 | |
| 3 | This derive allows you to convert enum variants into their corresponding |
| 4 | variant types. |
| 5 | One thing to note is that this derive doesn't actually generate an |
| 6 | implementation for the `TryInto` trait. |
| 7 | Instead it derives `TryFrom` for each variant in the enum and thus has an |
| 8 | indirect implementation of `TryInto` as recommended by the |
| 9 | [docs](https://doc.rust-lang.org/core/convert/trait.TryInto.html). |
| 10 | |
| 11 | By using `#[try_into(owned, ref, ref_mut)]` it's possible to derive a `TryInto` |
| 12 | implementation for reference types as well. |
| 13 | You can pick any combination of `owned`, `ref` and `ref_mut`. |
| 14 | If that's not provided the default is `#[try_into(owned)]`. |
| 15 | |
| 16 | With `#[try_into]` or `#[try_into(ignore)]` it's possible to indicate which |
| 17 | variants you want to derive `TryInto` for. |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | ## Example usage |
| 23 | |
| 24 | ```rust |
| 25 | # use derive_more::TryInto; |
| 26 | # |
| 27 | #[derive(TryInto, Clone, Debug)] |
| 28 | #[try_into(owned, ref, ref_mut)] |
| 29 | enum MixedData { |
| 30 | Int(u32), |
| 31 | String(String), |
| 32 | } |
| 33 | |
| 34 | let mixed_string = MixedData::String("foo".to_string()); |
| 35 | let mixed_int1 = MixedData::Int(123); |
| 36 | let mixed_int2 = mixed_int1.clone(); |
| 37 | let mut mixed_int3 = mixed_int1.clone(); |
| 38 | |
| 39 | assert_eq!(123u32, mixed_int1.try_into().unwrap()); |
| 40 | |
| 41 | let int_ref : &u32 = (&mixed_int2).try_into().unwrap(); |
| 42 | assert_eq!(&123u32, int_ref); |
| 43 | |
| 44 | let int_ref_mut : &mut u32 = (&mut mixed_int3).try_into().unwrap(); |
| 45 | assert_eq!(&mut 123u32, int_ref_mut); |
| 46 | |
| 47 | assert_eq!("foo".to_string(), String::try_from(mixed_string.clone()).unwrap()); |
| 48 | |
| 49 | assert!(u32::try_from(mixed_string).is_err()); |
| 50 | ``` |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | ## Structs |
| 56 | |
| 57 | Deriving `TryInto` for structs is not supported because there is no failing |
| 58 | mode. Use `#[derive(Into)]` instead. `TryInto` will automatically get a |
| 59 | blanket implementation through `TryFrom`, automatically derived from `From`, |
| 60 | which `#[derive(Into)]` produces. |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | ## Enums |
| 66 | |
| 67 | When deriving `TryInto` for an enum, each enum variant gets its own |
| 68 | `TryFrom` implementation. |
| 69 | For instance, when deriving `TryInto` for an enum link this: |
| 70 | |
| 71 | ```rust |
| 72 | # use derive_more::TryInto; |
| 73 | # |
| 74 | #[derive(TryInto)] |
| 75 | enum MixedInts { |
| 76 | SmallInt(i32), |
| 77 | BigInt(i64), |
| 78 | TwoSmallInts(i32, i32), |
| 79 | NamedSmallInts { x: i64, y: i64 }, |
| 80 | UnsignedOne(u32), |
| 81 | UnsignedTwo(u32), |
| 82 | #[try_into(ignore)] |
| 83 | NotImportant, |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | Code like this will be generated: |
| 88 | |
| 89 | ```rust |
| 90 | # enum MixedInts { |
| 91 | # SmallInt(i32), |
| 92 | # BigInt(i64), |
| 93 | # TwoSmallInts(i32, i32), |
| 94 | # NamedSmallInts { x: i64, y: i64 }, |
| 95 | # UnsignedOne(u32), |
| 96 | # UnsignedTwo(u32), |
| 97 | # } |
| 98 | impl TryFrom<MixedInts> for (i32) { |
| 99 | type Error = derive_more::TryIntoError<MixedInts>; |
| 100 | fn try_from(value: MixedInts) -> Result<Self, derive_more::TryIntoError<MixedInts>> { |
| 101 | match value { |
| 102 | MixedInts::SmallInt(__0) => Ok(__0), |
| 103 | _ => Err(derive_more::TryIntoError::new(value, "SmallInt", "i32")), |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | impl TryFrom<MixedInts> for (i64) { |
| 108 | type Error = derive_more::TryIntoError<MixedInts>; |
| 109 | fn try_from(value: MixedInts) -> Result<Self, derive_more::TryIntoError<MixedInts>> { |
| 110 | match value { |
| 111 | MixedInts::BigInt(__0) => Ok(__0), |
| 112 | _ => Err(derive_more::TryIntoError::new(value, "BigInt", "i64")), |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | impl TryFrom<MixedInts> for (i32, i32) { |
| 117 | type Error = derive_more::TryIntoError<MixedInts>; |
| 118 | fn try_from(value: MixedInts) -> Result<Self, derive_more::TryIntoError<MixedInts>> { |
| 119 | match value { |
| 120 | MixedInts::TwoSmallInts(__0, __1) => Ok((__0, __1)), |
| 121 | _ => Err(derive_more::TryIntoError::new(value, "TwoSmallInts", "(i32, i32)")), |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | impl TryFrom<MixedInts> for (i64, i64) { |
| 126 | type Error = derive_more::TryIntoError<MixedInts>; |
| 127 | fn try_from(value: MixedInts) -> Result<Self, derive_more::TryIntoError<MixedInts>> { |
| 128 | match value { |
| 129 | MixedInts::NamedSmallInts { x: __0, y: __1 } => Ok((__0, __1)), |
| 130 | _ => Err(derive_more::TryIntoError::new(value, "NamedSmallInts", "(i64, i64)")), |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | impl TryFrom<MixedInts> for (u32) { |
| 135 | type Error = derive_more::TryIntoError<MixedInts>; |
| 136 | fn try_from(value: MixedInts) -> Result<Self, derive_more::TryIntoError<MixedInts>> { |
| 137 | match value { |
| 138 | MixedInts::UnsignedOne(__0) | MixedInts::UnsignedTwo(__0) => Ok(__0), |
| 139 | _ => Err(derive_more::TryIntoError::new(value, "UnsignedOne", "u32")), |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | ``` |
| 144 | |
| 145 | When deriving `TryInto` for an enum with Unit variants like this: |
| 146 | |
| 147 | ```rust |
| 148 | # use derive_more::TryInto; |
| 149 | # |
| 150 | #[derive(TryInto)] |
| 151 | enum EnumWithUnit { |
| 152 | SmallInt(i32), |
| 153 | Unit, |
| 154 | } |
| 155 | ``` |
| 156 | |
| 157 | Code like this will be generated: |
| 158 | |
| 159 | ```rust |
| 160 | # enum EnumWithUnit { |
| 161 | # SmallInt(i32), |
| 162 | # Unit, |
| 163 | # } |
| 164 | impl TryFrom<EnumWithUnit> for (i32) { |
| 165 | type Error = derive_more::TryIntoError<EnumWithUnit>; |
| 166 | fn try_from(value: EnumWithUnit) -> Result<Self, derive_more::TryIntoError<EnumWithUnit>> { |
| 167 | match value { |
| 168 | EnumWithUnit::SmallInt(__0) => Ok(__0), |
| 169 | _ => Err(derive_more::TryIntoError::new(value, "SmallInt", "i32")), |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | impl TryFrom<EnumWithUnit> for () { |
| 174 | type Error = derive_more::TryIntoError<EnumWithUnit>; |
| 175 | fn try_from(value: EnumWithUnit) -> Result<Self, derive_more::TryIntoError<EnumWithUnit>> { |
| 176 | match value { |
| 177 | EnumWithUnit::Unit => Ok(()), |
| 178 | _ => Err(derive_more::TryIntoError::new(value, "Unit", "()")), |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | ``` |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | ## Custom error |
| 188 | |
| 189 | The `#[try_into(error(<ty>[, <conv>]))]` attribute can be used to convert the `TryIntoError` type |
| 190 | into a custom error type. |
| 191 | |
| 192 | If the conversion function is not provided, the custom error type must implement |
| 193 | `From<derive_more::TryIntoError<T>>`. |
| 194 | The conversion function could be provided in the following forms: |
| 195 | - function (like `CustomError::new`); |
| 196 | - closure (like `|e| CustomError::new(e)`); |
| 197 | - function call (like `CustomError::factory()`). |
| 198 | |
| 199 | This, however, doesn't support custom error with generic parameters, such as |
| 200 | `struct CustomError<T>(derive_more::TryIntoError<T>)` (newtype wrapper around `derive_more::TryIntoError`). |
| 201 | |
| 202 | Given the following enum: |
| 203 | ```rust |
| 204 | # use derive_more::TryInto; |
| 205 | # |
| 206 | struct CustomError(String); |
| 207 | |
| 208 | impl<T> From<derive_more::TryIntoError<T>> for CustomError { |
| 209 | fn from(value: derive_more::TryIntoError<T>) -> Self { |
| 210 | Self(value.to_string()) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | #[derive(TryInto, Clone, Debug)] |
| 215 | #[try_into(error(CustomError))] |
| 216 | enum MixedData { |
| 217 | Int(u32), |
| 218 | String(String), |
| 219 | } |
| 220 | ``` |
| 221 | Code like this is generated: |
| 222 | ```rust |
| 223 | # use derive_more::TryIntoError; |
| 224 | # |
| 225 | # enum MixedData { |
| 226 | # Int(u32), |
| 227 | # String(String), |
| 228 | # } |
| 229 | # |
| 230 | # struct CustomError(String); |
| 231 | # |
| 232 | # impl<T> From<TryIntoError<T>> for CustomError { |
| 233 | # fn from(value: TryIntoError<T>) -> Self { |
| 234 | # Self(value.to_string()) |
| 235 | # } |
| 236 | # } |
| 237 | impl derive_more::core::convert::TryFrom<MixedData> for (u32) { |
| 238 | type Error = CustomError; |
| 239 | fn try_from(value: MixedData) -> Result<Self, CustomError> { |
| 240 | match value { |
| 241 | MixedData::Int(v) => Ok(v), |
| 242 | _ => Err(derive_more::TryIntoError::new(value, "Int", "u32").into()), |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | impl derive_more::core::convert::TryFrom<MixedData> for (String) { |
| 247 | type Error = CustomError; |
| 248 | fn try_from(value: MixedData) -> Result<Self, CustomError> { |
| 249 | match value { |
| 250 | MixedData::String(v) => Ok(v), |
| 251 | _ => Err(derive_more::TryIntoError::new(value, "String", "String").into()), |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | ``` |