1 # What `#[derive(From)]` generates
3 The point of deriving this type is that it makes it easy to create a new
4 instance of the type by using the `.into()` method on the value(s) that it
5 should contain. This is done by implementing the `From` trait for the type
6 that is passed to the derive.
13 For structs with a single field you can call `.into()` on the desired content
14 itself after deriving `From`.
17 # use derive_more::From;
19 #[derive(Debug, From, PartialEq)]
22 assert_eq!(Int(2), 2.into());
25 For structs that have multiple fields `.into()` needs to be called on a tuple
26 containing the desired content for each field.
29 # use derive_more::From;
31 #[derive(Debug, From, PartialEq)]
32 struct Point(i32, i32);
34 assert_eq!(Point(1, 2), (1, 2).into());
37 To specify concrete types to derive convert from use `#[from(<types>)]`.
40 # use std::borrow::Cow;
42 # use derive_more::From;
44 #[derive(Debug, From, PartialEq)]
45 #[from(Cow<'static, str>, String, &'static str)]
46 struct Str(Cow<'static, str>);
48 assert_eq!(Str("&str".into()), "&str".into());
49 assert_eq!(Str("String".into()), "String".to_owned().into());
50 assert_eq!(Str("Cow".into()), Cow::Borrowed("Cow").to_owned().into());
52 #[derive(Debug, From, PartialEq)]
53 #[from((i16, i16), (i32, i32))]
59 assert_eq!(Point { x: 1_i32, y: 2_i32 }, (1_i16, 2_i16).into());
60 assert_eq!(Point { x: 3_i32, y: 4_i32 }, (3_i32, 4_i32).into());
63 Also, you can forward implementation to the inner type, which means deriving `From` for any type, that derives `From`
67 # use std::borrow::Cow;
69 # use derive_more::From;
71 #[derive(Debug, From, PartialEq)]
74 inner: Cow<'static, str>,
77 assert_eq!(Str { inner: "&str".into() }, "&str".into());
78 assert_eq!(Str { inner: "String".into() }, "String".to_owned().into());
79 assert_eq!(Str { inner: "Cow".into() }, Cow::Borrowed("Cow").to_owned().into());
87 For enums `.into()` works for each variant as if they were structs. This
88 includes specifying concrete types via `#[from(<types>)]` or forwarding
89 implementation with `#[from(forward)]`.
92 # use derive_more::From;
94 #[derive(Debug, From, PartialEq)]
103 assert_eq!(IntOrPoint::Int(1), 1.into());
104 assert_eq!(IntOrPoint::Point { x: 1, y: 2 }, (1, 2).into());
107 By default, `From` is generated for every enum variant, but you can skip some
108 variants via `#[from(skip)]` or only concrete fields via `#[from]`.
112 # use derive_more::From;
113 #[derive(Debug, From, PartialEq)]
124 # use derive_more::From;
125 #[derive(Debug, From, PartialEq)]
140 # use derive_more::From;
142 // Allow converting from i32
143 #[derive(From, PartialEq)]
146 // Forward from call to the field, so allow converting
147 // from anything that can be converted into an i64 (so most integers)
148 #[derive(From, PartialEq)]
152 // You can ignore a variant
153 #[derive(From, PartialEq)]
156 NamedBigInt { int: i64 },
161 // Or explicitly annotate the ones you need
162 #[derive(From, PartialEq)]
167 NamedBigInt { int: i64 },
171 // And even specify additional conversions for them
172 #[derive(From, PartialEq)]
177 NamedBigInt { int: i64 },
181 assert!(MyInt(2) == 2.into());
182 assert!(MyInt64(6) == 6u8.into());
183 assert!(MyEnum::SmallInt(123) == 123i32.into());
184 assert!(MyEnum::SmallInt(123) != 123i64.into());
185 assert!(MyEnum::NamedBigInt{int: 123} == 123i64.into());
186 assert!(MyEnum3::SmallInt(123) == 123i8.into());
187 assert!(MyEnum3::NamedBigInt{int: 123} == 123i16.into());