Skip to content

Commit 2597a22

Browse files
authored
impl Default for structs where all properties have a default (#725)
1 parent 6afbe73 commit 2597a22

32 files changed

+11678
-9373
lines changed

cargo-typify/tests/outputs/builder.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ impl ::std::ops::Deref for Fruit {
5050
&self.0
5151
}
5252
}
53-
impl From<Fruit> for ::std::collections::HashMap<::std::string::String, ::std::string::String> {
53+
impl ::std::convert::From<Fruit>
54+
for ::std::collections::HashMap<::std::string::String, ::std::string::String>
55+
{
5456
fn from(value: Fruit) -> Self {
5557
value.0
5658
}
5759
}
58-
impl From<&Fruit> for Fruit {
60+
impl ::std::convert::From<&Fruit> for Fruit {
5961
fn from(value: &Fruit) -> Self {
6062
value.clone()
6163
}
6264
}
63-
impl From<::std::collections::HashMap<::std::string::String, ::std::string::String>> for Fruit {
65+
impl ::std::convert::From<::std::collections::HashMap<::std::string::String, ::std::string::String>>
66+
for Fruit
67+
{
6468
fn from(
6569
value: ::std::collections::HashMap<::std::string::String, ::std::string::String>,
6670
) -> Self {
@@ -100,17 +104,17 @@ pub enum FruitOrVeg {
100104
Veg(Veggie),
101105
Fruit(Fruit),
102106
}
103-
impl From<&FruitOrVeg> for FruitOrVeg {
107+
impl ::std::convert::From<&FruitOrVeg> for FruitOrVeg {
104108
fn from(value: &FruitOrVeg) -> Self {
105109
value.clone()
106110
}
107111
}
108-
impl From<Veggie> for FruitOrVeg {
112+
impl ::std::convert::From<Veggie> for FruitOrVeg {
109113
fn from(value: Veggie) -> Self {
110114
Self::Veg(value)
111115
}
112116
}
113-
impl From<Fruit> for FruitOrVeg {
117+
impl ::std::convert::From<Fruit> for FruitOrVeg {
114118
fn from(value: Fruit) -> Self {
115119
Self::Fruit(value)
116120
}
@@ -148,7 +152,7 @@ pub struct Veggie {
148152
#[serde(rename = "veggieName")]
149153
pub veggie_name: ::std::string::String,
150154
}
151-
impl From<&Veggie> for Veggie {
155+
impl ::std::convert::From<&Veggie> for Veggie {
152156
fn from(value: &Veggie) -> Self {
153157
value.clone()
154158
}
@@ -192,11 +196,19 @@ pub struct Veggies {
192196
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
193197
pub vegetables: ::std::vec::Vec<Veggie>,
194198
}
195-
impl From<&Veggies> for Veggies {
199+
impl ::std::convert::From<&Veggies> for Veggies {
196200
fn from(value: &Veggies) -> Self {
197201
value.clone()
198202
}
199203
}
204+
impl ::std::default::Default for Veggies {
205+
fn default() -> Self {
206+
Self {
207+
fruits: Default::default(),
208+
vegetables: Default::default(),
209+
}
210+
}
211+
}
200212
impl Veggies {
201213
pub fn builder() -> builder::Veggies {
202214
Default::default()
@@ -209,7 +221,7 @@ pub mod builder {
209221
veggie_like: ::std::result::Result<bool, ::std::string::String>,
210222
veggie_name: ::std::result::Result<::std::string::String, ::std::string::String>,
211223
}
212-
impl Default for Veggie {
224+
impl ::std::default::Default for Veggie {
213225
fn default() -> Self {
214226
Self {
215227
veggie_like: Err("no value supplied for veggie_like".to_string()),
@@ -220,8 +232,8 @@ pub mod builder {
220232
impl Veggie {
221233
pub fn veggie_like<T>(mut self, value: T) -> Self
222234
where
223-
T: std::convert::TryInto<bool>,
224-
T::Error: std::fmt::Display,
235+
T: ::std::convert::TryInto<bool>,
236+
T::Error: ::std::fmt::Display,
225237
{
226238
self.veggie_like = value
227239
.try_into()
@@ -230,8 +242,8 @@ pub mod builder {
230242
}
231243
pub fn veggie_name<T>(mut self, value: T) -> Self
232244
where
233-
T: std::convert::TryInto<::std::string::String>,
234-
T::Error: std::fmt::Display,
245+
T: ::std::convert::TryInto<::std::string::String>,
246+
T::Error: ::std::fmt::Display,
235247
{
236248
self.veggie_name = value
237249
.try_into()
@@ -248,7 +260,7 @@ pub mod builder {
248260
})
249261
}
250262
}
251-
impl From<super::Veggie> for Veggie {
263+
impl ::std::convert::From<super::Veggie> for Veggie {
252264
fn from(value: super::Veggie) -> Self {
253265
Self {
254266
veggie_like: Ok(value.veggie_like),
@@ -262,7 +274,7 @@ pub mod builder {
262274
::std::result::Result<::std::vec::Vec<::std::string::String>, ::std::string::String>,
263275
vegetables: ::std::result::Result<::std::vec::Vec<super::Veggie>, ::std::string::String>,
264276
}
265-
impl Default for Veggies {
277+
impl ::std::default::Default for Veggies {
266278
fn default() -> Self {
267279
Self {
268280
fruits: Ok(Default::default()),
@@ -273,8 +285,8 @@ pub mod builder {
273285
impl Veggies {
274286
pub fn fruits<T>(mut self, value: T) -> Self
275287
where
276-
T: std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
277-
T::Error: std::fmt::Display,
288+
T: ::std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
289+
T::Error: ::std::fmt::Display,
278290
{
279291
self.fruits = value
280292
.try_into()
@@ -283,8 +295,8 @@ pub mod builder {
283295
}
284296
pub fn vegetables<T>(mut self, value: T) -> Self
285297
where
286-
T: std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
287-
T::Error: std::fmt::Display,
298+
T: ::std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
299+
T::Error: ::std::fmt::Display,
288300
{
289301
self.vegetables = value
290302
.try_into()
@@ -301,7 +313,7 @@ pub mod builder {
301313
})
302314
}
303315
}
304-
impl From<super::Veggies> for Veggies {
316+
impl ::std::convert::From<super::Veggies> for Veggies {
305317
fn from(value: super::Veggies) -> Self {
306318
Self {
307319
fruits: Ok(value.fruits),

cargo-typify/tests/outputs/custom_btree_map.rs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,22 @@ impl ::std::ops::Deref for Fruit {
5050
&self.0
5151
}
5252
}
53-
impl From<Fruit> for ::std::collections::BTreeMap<::std::string::String, ::std::string::String> {
53+
impl ::std::convert::From<Fruit>
54+
for ::std::collections::BTreeMap<::std::string::String, ::std::string::String>
55+
{
5456
fn from(value: Fruit) -> Self {
5557
value.0
5658
}
5759
}
58-
impl From<&Fruit> for Fruit {
60+
impl ::std::convert::From<&Fruit> for Fruit {
5961
fn from(value: &Fruit) -> Self {
6062
value.clone()
6163
}
6264
}
63-
impl From<::std::collections::BTreeMap<::std::string::String, ::std::string::String>> for Fruit {
65+
impl
66+
::std::convert::From<::std::collections::BTreeMap<::std::string::String, ::std::string::String>>
67+
for Fruit
68+
{
6469
fn from(
6570
value: ::std::collections::BTreeMap<::std::string::String, ::std::string::String>,
6671
) -> Self {
@@ -100,17 +105,17 @@ pub enum FruitOrVeg {
100105
Veg(Veggie),
101106
Fruit(Fruit),
102107
}
103-
impl From<&FruitOrVeg> for FruitOrVeg {
108+
impl ::std::convert::From<&FruitOrVeg> for FruitOrVeg {
104109
fn from(value: &FruitOrVeg) -> Self {
105110
value.clone()
106111
}
107112
}
108-
impl From<Veggie> for FruitOrVeg {
113+
impl ::std::convert::From<Veggie> for FruitOrVeg {
109114
fn from(value: Veggie) -> Self {
110115
Self::Veg(value)
111116
}
112117
}
113-
impl From<Fruit> for FruitOrVeg {
118+
impl ::std::convert::From<Fruit> for FruitOrVeg {
114119
fn from(value: Fruit) -> Self {
115120
Self::Fruit(value)
116121
}
@@ -148,7 +153,7 @@ pub struct Veggie {
148153
#[serde(rename = "veggieName")]
149154
pub veggie_name: ::std::string::String,
150155
}
151-
impl From<&Veggie> for Veggie {
156+
impl ::std::convert::From<&Veggie> for Veggie {
152157
fn from(value: &Veggie) -> Self {
153158
value.clone()
154159
}
@@ -192,11 +197,19 @@ pub struct Veggies {
192197
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
193198
pub vegetables: ::std::vec::Vec<Veggie>,
194199
}
195-
impl From<&Veggies> for Veggies {
200+
impl ::std::convert::From<&Veggies> for Veggies {
196201
fn from(value: &Veggies) -> Self {
197202
value.clone()
198203
}
199204
}
205+
impl ::std::default::Default for Veggies {
206+
fn default() -> Self {
207+
Self {
208+
fruits: Default::default(),
209+
vegetables: Default::default(),
210+
}
211+
}
212+
}
200213
impl Veggies {
201214
pub fn builder() -> builder::Veggies {
202215
Default::default()
@@ -209,7 +222,7 @@ pub mod builder {
209222
veggie_like: ::std::result::Result<bool, ::std::string::String>,
210223
veggie_name: ::std::result::Result<::std::string::String, ::std::string::String>,
211224
}
212-
impl Default for Veggie {
225+
impl ::std::default::Default for Veggie {
213226
fn default() -> Self {
214227
Self {
215228
veggie_like: Err("no value supplied for veggie_like".to_string()),
@@ -220,8 +233,8 @@ pub mod builder {
220233
impl Veggie {
221234
pub fn veggie_like<T>(mut self, value: T) -> Self
222235
where
223-
T: std::convert::TryInto<bool>,
224-
T::Error: std::fmt::Display,
236+
T: ::std::convert::TryInto<bool>,
237+
T::Error: ::std::fmt::Display,
225238
{
226239
self.veggie_like = value
227240
.try_into()
@@ -230,8 +243,8 @@ pub mod builder {
230243
}
231244
pub fn veggie_name<T>(mut self, value: T) -> Self
232245
where
233-
T: std::convert::TryInto<::std::string::String>,
234-
T::Error: std::fmt::Display,
246+
T: ::std::convert::TryInto<::std::string::String>,
247+
T::Error: ::std::fmt::Display,
235248
{
236249
self.veggie_name = value
237250
.try_into()
@@ -248,7 +261,7 @@ pub mod builder {
248261
})
249262
}
250263
}
251-
impl From<super::Veggie> for Veggie {
264+
impl ::std::convert::From<super::Veggie> for Veggie {
252265
fn from(value: super::Veggie) -> Self {
253266
Self {
254267
veggie_like: Ok(value.veggie_like),
@@ -262,7 +275,7 @@ pub mod builder {
262275
::std::result::Result<::std::vec::Vec<::std::string::String>, ::std::string::String>,
263276
vegetables: ::std::result::Result<::std::vec::Vec<super::Veggie>, ::std::string::String>,
264277
}
265-
impl Default for Veggies {
278+
impl ::std::default::Default for Veggies {
266279
fn default() -> Self {
267280
Self {
268281
fruits: Ok(Default::default()),
@@ -273,8 +286,8 @@ pub mod builder {
273286
impl Veggies {
274287
pub fn fruits<T>(mut self, value: T) -> Self
275288
where
276-
T: std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
277-
T::Error: std::fmt::Display,
289+
T: ::std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
290+
T::Error: ::std::fmt::Display,
278291
{
279292
self.fruits = value
280293
.try_into()
@@ -283,8 +296,8 @@ pub mod builder {
283296
}
284297
pub fn vegetables<T>(mut self, value: T) -> Self
285298
where
286-
T: std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
287-
T::Error: std::fmt::Display,
299+
T: ::std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
300+
T::Error: ::std::fmt::Display,
288301
{
289302
self.vegetables = value
290303
.try_into()
@@ -301,7 +314,7 @@ pub mod builder {
301314
})
302315
}
303316
}
304-
impl From<super::Veggies> for Veggies {
317+
impl ::std::convert::From<super::Veggies> for Veggies {
305318
fn from(value: super::Veggies) -> Self {
306319
Self {
307320
fruits: Ok(value.fruits),

cargo-typify/tests/outputs/derive.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ impl ::std::ops::Deref for Fruit {
5050
&self.0
5151
}
5252
}
53-
impl From<Fruit> for ::std::collections::HashMap<::std::string::String, ::std::string::String> {
53+
impl ::std::convert::From<Fruit>
54+
for ::std::collections::HashMap<::std::string::String, ::std::string::String>
55+
{
5456
fn from(value: Fruit) -> Self {
5557
value.0
5658
}
5759
}
58-
impl From<&Fruit> for Fruit {
60+
impl ::std::convert::From<&Fruit> for Fruit {
5961
fn from(value: &Fruit) -> Self {
6062
value.clone()
6163
}
6264
}
63-
impl From<::std::collections::HashMap<::std::string::String, ::std::string::String>> for Fruit {
65+
impl ::std::convert::From<::std::collections::HashMap<::std::string::String, ::std::string::String>>
66+
for Fruit
67+
{
6468
fn from(
6569
value: ::std::collections::HashMap<::std::string::String, ::std::string::String>,
6670
) -> Self {
@@ -100,17 +104,17 @@ pub enum FruitOrVeg {
100104
Veg(Veggie),
101105
Fruit(Fruit),
102106
}
103-
impl From<&FruitOrVeg> for FruitOrVeg {
107+
impl ::std::convert::From<&FruitOrVeg> for FruitOrVeg {
104108
fn from(value: &FruitOrVeg) -> Self {
105109
value.clone()
106110
}
107111
}
108-
impl From<Veggie> for FruitOrVeg {
112+
impl ::std::convert::From<Veggie> for FruitOrVeg {
109113
fn from(value: Veggie) -> Self {
110114
Self::Veg(value)
111115
}
112116
}
113-
impl From<Fruit> for FruitOrVeg {
117+
impl ::std::convert::From<Fruit> for FruitOrVeg {
114118
fn from(value: Fruit) -> Self {
115119
Self::Fruit(value)
116120
}
@@ -148,7 +152,7 @@ pub struct Veggie {
148152
#[serde(rename = "veggieName")]
149153
pub veggie_name: ::std::string::String,
150154
}
151-
impl From<&Veggie> for Veggie {
155+
impl ::std::convert::From<&Veggie> for Veggie {
152156
fn from(value: &Veggie) -> Self {
153157
value.clone()
154158
}
@@ -187,8 +191,16 @@ pub struct Veggies {
187191
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
188192
pub vegetables: ::std::vec::Vec<Veggie>,
189193
}
190-
impl From<&Veggies> for Veggies {
194+
impl ::std::convert::From<&Veggies> for Veggies {
191195
fn from(value: &Veggies) -> Self {
192196
value.clone()
193197
}
194198
}
199+
impl ::std::default::Default for Veggies {
200+
fn default() -> Self {
201+
Self {
202+
fruits: Default::default(),
203+
vegetables: Default::default(),
204+
}
205+
}
206+
}

0 commit comments

Comments
 (0)