| 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 ::core::convert::TryFrom<MixedInts> for (i32) { |
| 99 |
type Error = &'static str; |
| 100 |
fn try_from(value: MixedInts) -> Result<Self, Self::Error> { |
| 101 |
match value { |
| 102 |
MixedInts::SmallInt(__0) => Ok(__0), |
| 103 |
_ => Err("Only SmallInt can be converted to i32"), |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
impl ::core::convert::TryFrom<MixedInts> for (i64) { |
| 108 |
type Error = &'static str; |
| 109 |
fn try_from(value: MixedInts) -> Result<Self, Self::Error> { |
| 110 |
match value { |
| 111 |
MixedInts::BigInt(__0) => Ok(__0), |
| 112 |
_ => Err("Only BigInt can be converted to i64"), |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
impl ::core::convert::TryFrom<MixedInts> for (i32, i32) { |
| 117 |
type Error = &'static str; |
| 118 |
fn try_from(value: MixedInts) -> Result<Self, Self::Error> { |
| 119 |
match value { |
| 120 |
MixedInts::TwoSmallInts(__0, __1) => Ok((__0, __1)), |
| 121 |
_ => Err("Only TwoSmallInts can be converted to (i32, i32)"), |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
impl ::core::convert::TryFrom<MixedInts> for (i64, i64) { |
| 126 |
type Error = &'static str; |
| 127 |
fn try_from(value: MixedInts) -> Result<Self, Self::Error> { |
| 128 |
match value { |
| 129 |
MixedInts::NamedSmallInts { x: __0, y: __1 } => Ok((__0, __1)), |
| 130 |
_ => Err("Only NamedSmallInts can be converted to (i64, i64)"), |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
impl ::core::convert::TryFrom<MixedInts> for (u32) { |
| 135 |
type Error = &'static str; |
| 136 |
fn try_from(value: MixedInts) -> Result<Self, Self::Error> { |
| 137 |
match value { |
| 138 |
MixedInts::UnsignedOne(__0) | MixedInts::UnsignedTwo(__0) => Ok(__0), |
| 139 |
_ => Err("Only UnsignedOne, UnsignedTwo can be converted to 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 ::core::convert::TryFrom<EnumWithUnit> for (i32) { |
| 165 |
type Error = &'static str; |
| 166 |
fn try_from(value: EnumWithUnit) -> Result<Self, Self::Error> { |
| 167 |
match value { |
| 168 |
EnumWithUnit::SmallInt(__0) => Ok(__0), |
| 169 |
_ => Err("Only SmallInt can be converted to i32"), |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
impl ::core::convert::TryFrom<EnumWithUnit> for () { |
| 174 |
type Error = &'static str; |
| 175 |
fn try_from(value: EnumWithUnit) -> Result<Self, Self::Error> { |
| 176 |
match value { |
| 177 |
EnumWithUnit::Unit => Ok(()), |
| 178 |
_ => Err("Only Unit can be converted to ()"), |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
``` |