Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Default for structs where all properties have a default #725

Merged
merged 2 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions cargo-typify/tests/outputs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ impl ::std::ops::Deref for Fruit {
&self.0
}
}
impl From<Fruit> for ::std::collections::HashMap<::std::string::String, ::std::string::String> {
impl ::std::convert::From<Fruit>
for ::std::collections::HashMap<::std::string::String, ::std::string::String>
{
fn from(value: Fruit) -> Self {
value.0
}
}
impl From<&Fruit> for Fruit {
impl ::std::convert::From<&Fruit> for Fruit {
fn from(value: &Fruit) -> Self {
value.clone()
}
}
impl From<::std::collections::HashMap<::std::string::String, ::std::string::String>> for Fruit {
impl ::std::convert::From<::std::collections::HashMap<::std::string::String, ::std::string::String>>
for Fruit
{
fn from(
value: ::std::collections::HashMap<::std::string::String, ::std::string::String>,
) -> Self {
Expand Down Expand Up @@ -100,17 +104,17 @@ pub enum FruitOrVeg {
Veg(Veggie),
Fruit(Fruit),
}
impl From<&FruitOrVeg> for FruitOrVeg {
impl ::std::convert::From<&FruitOrVeg> for FruitOrVeg {
fn from(value: &FruitOrVeg) -> Self {
value.clone()
}
}
impl From<Veggie> for FruitOrVeg {
impl ::std::convert::From<Veggie> for FruitOrVeg {
fn from(value: Veggie) -> Self {
Self::Veg(value)
}
}
impl From<Fruit> for FruitOrVeg {
impl ::std::convert::From<Fruit> for FruitOrVeg {
fn from(value: Fruit) -> Self {
Self::Fruit(value)
}
Expand Down Expand Up @@ -148,7 +152,7 @@ pub struct Veggie {
#[serde(rename = "veggieName")]
pub veggie_name: ::std::string::String,
}
impl From<&Veggie> for Veggie {
impl ::std::convert::From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
value.clone()
}
Expand Down Expand Up @@ -192,11 +196,19 @@ pub struct Veggies {
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub vegetables: ::std::vec::Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
impl ::std::convert::From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
value.clone()
}
}
impl ::std::default::Default for Veggies {
fn default() -> Self {
Self {
fruits: Default::default(),
vegetables: Default::default(),
}
}
}
impl Veggies {
pub fn builder() -> builder::Veggies {
Default::default()
Expand All @@ -209,7 +221,7 @@ pub mod builder {
veggie_like: ::std::result::Result<bool, ::std::string::String>,
veggie_name: ::std::result::Result<::std::string::String, ::std::string::String>,
}
impl Default for Veggie {
impl ::std::default::Default for Veggie {
fn default() -> Self {
Self {
veggie_like: Err("no value supplied for veggie_like".to_string()),
Expand All @@ -220,8 +232,8 @@ pub mod builder {
impl Veggie {
pub fn veggie_like<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<bool>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<bool>,
T::Error: ::std::fmt::Display,
{
self.veggie_like = value
.try_into()
Expand All @@ -230,8 +242,8 @@ pub mod builder {
}
pub fn veggie_name<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<::std::string::String>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<::std::string::String>,
T::Error: ::std::fmt::Display,
{
self.veggie_name = value
.try_into()
Expand All @@ -248,7 +260,7 @@ pub mod builder {
})
}
}
impl From<super::Veggie> for Veggie {
impl ::std::convert::From<super::Veggie> for Veggie {
fn from(value: super::Veggie) -> Self {
Self {
veggie_like: Ok(value.veggie_like),
Expand All @@ -262,7 +274,7 @@ pub mod builder {
::std::result::Result<::std::vec::Vec<::std::string::String>, ::std::string::String>,
vegetables: ::std::result::Result<::std::vec::Vec<super::Veggie>, ::std::string::String>,
}
impl Default for Veggies {
impl ::std::default::Default for Veggies {
fn default() -> Self {
Self {
fruits: Ok(Default::default()),
Expand All @@ -273,8 +285,8 @@ pub mod builder {
impl Veggies {
pub fn fruits<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
T::Error: ::std::fmt::Display,
{
self.fruits = value
.try_into()
Expand All @@ -283,8 +295,8 @@ pub mod builder {
}
pub fn vegetables<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
T::Error: ::std::fmt::Display,
{
self.vegetables = value
.try_into()
Expand All @@ -301,7 +313,7 @@ pub mod builder {
})
}
}
impl From<super::Veggies> for Veggies {
impl ::std::convert::From<super::Veggies> for Veggies {
fn from(value: super::Veggies) -> Self {
Self {
fruits: Ok(value.fruits),
Expand Down
53 changes: 33 additions & 20 deletions cargo-typify/tests/outputs/custom_btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,22 @@ impl ::std::ops::Deref for Fruit {
&self.0
}
}
impl From<Fruit> for ::std::collections::BTreeMap<::std::string::String, ::std::string::String> {
impl ::std::convert::From<Fruit>
for ::std::collections::BTreeMap<::std::string::String, ::std::string::String>
{
fn from(value: Fruit) -> Self {
value.0
}
}
impl From<&Fruit> for Fruit {
impl ::std::convert::From<&Fruit> for Fruit {
fn from(value: &Fruit) -> Self {
value.clone()
}
}
impl From<::std::collections::BTreeMap<::std::string::String, ::std::string::String>> for Fruit {
impl
::std::convert::From<::std::collections::BTreeMap<::std::string::String, ::std::string::String>>
for Fruit
{
fn from(
value: ::std::collections::BTreeMap<::std::string::String, ::std::string::String>,
) -> Self {
Expand Down Expand Up @@ -100,17 +105,17 @@ pub enum FruitOrVeg {
Veg(Veggie),
Fruit(Fruit),
}
impl From<&FruitOrVeg> for FruitOrVeg {
impl ::std::convert::From<&FruitOrVeg> for FruitOrVeg {
fn from(value: &FruitOrVeg) -> Self {
value.clone()
}
}
impl From<Veggie> for FruitOrVeg {
impl ::std::convert::From<Veggie> for FruitOrVeg {
fn from(value: Veggie) -> Self {
Self::Veg(value)
}
}
impl From<Fruit> for FruitOrVeg {
impl ::std::convert::From<Fruit> for FruitOrVeg {
fn from(value: Fruit) -> Self {
Self::Fruit(value)
}
Expand Down Expand Up @@ -148,7 +153,7 @@ pub struct Veggie {
#[serde(rename = "veggieName")]
pub veggie_name: ::std::string::String,
}
impl From<&Veggie> for Veggie {
impl ::std::convert::From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
value.clone()
}
Expand Down Expand Up @@ -192,11 +197,19 @@ pub struct Veggies {
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub vegetables: ::std::vec::Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
impl ::std::convert::From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
value.clone()
}
}
impl ::std::default::Default for Veggies {
fn default() -> Self {
Self {
fruits: Default::default(),
vegetables: Default::default(),
}
}
}
impl Veggies {
pub fn builder() -> builder::Veggies {
Default::default()
Expand All @@ -209,7 +222,7 @@ pub mod builder {
veggie_like: ::std::result::Result<bool, ::std::string::String>,
veggie_name: ::std::result::Result<::std::string::String, ::std::string::String>,
}
impl Default for Veggie {
impl ::std::default::Default for Veggie {
fn default() -> Self {
Self {
veggie_like: Err("no value supplied for veggie_like".to_string()),
Expand All @@ -220,8 +233,8 @@ pub mod builder {
impl Veggie {
pub fn veggie_like<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<bool>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<bool>,
T::Error: ::std::fmt::Display,
{
self.veggie_like = value
.try_into()
Expand All @@ -230,8 +243,8 @@ pub mod builder {
}
pub fn veggie_name<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<::std::string::String>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<::std::string::String>,
T::Error: ::std::fmt::Display,
{
self.veggie_name = value
.try_into()
Expand All @@ -248,7 +261,7 @@ pub mod builder {
})
}
}
impl From<super::Veggie> for Veggie {
impl ::std::convert::From<super::Veggie> for Veggie {
fn from(value: super::Veggie) -> Self {
Self {
veggie_like: Ok(value.veggie_like),
Expand All @@ -262,7 +275,7 @@ pub mod builder {
::std::result::Result<::std::vec::Vec<::std::string::String>, ::std::string::String>,
vegetables: ::std::result::Result<::std::vec::Vec<super::Veggie>, ::std::string::String>,
}
impl Default for Veggies {
impl ::std::default::Default for Veggies {
fn default() -> Self {
Self {
fruits: Ok(Default::default()),
Expand All @@ -273,8 +286,8 @@ pub mod builder {
impl Veggies {
pub fn fruits<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<::std::vec::Vec<::std::string::String>>,
T::Error: ::std::fmt::Display,
{
self.fruits = value
.try_into()
Expand All @@ -283,8 +296,8 @@ pub mod builder {
}
pub fn vegetables<T>(mut self, value: T) -> Self
where
T: std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
T::Error: std::fmt::Display,
T: ::std::convert::TryInto<::std::vec::Vec<super::Veggie>>,
T::Error: ::std::fmt::Display,
{
self.vegetables = value
.try_into()
Expand All @@ -301,7 +314,7 @@ pub mod builder {
})
}
}
impl From<super::Veggies> for Veggies {
impl ::std::convert::From<super::Veggies> for Veggies {
fn from(value: super::Veggies) -> Self {
Self {
fruits: Ok(value.fruits),
Expand Down
28 changes: 20 additions & 8 deletions cargo-typify/tests/outputs/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ impl ::std::ops::Deref for Fruit {
&self.0
}
}
impl From<Fruit> for ::std::collections::HashMap<::std::string::String, ::std::string::String> {
impl ::std::convert::From<Fruit>
for ::std::collections::HashMap<::std::string::String, ::std::string::String>
{
fn from(value: Fruit) -> Self {
value.0
}
}
impl From<&Fruit> for Fruit {
impl ::std::convert::From<&Fruit> for Fruit {
fn from(value: &Fruit) -> Self {
value.clone()
}
}
impl From<::std::collections::HashMap<::std::string::String, ::std::string::String>> for Fruit {
impl ::std::convert::From<::std::collections::HashMap<::std::string::String, ::std::string::String>>
for Fruit
{
fn from(
value: ::std::collections::HashMap<::std::string::String, ::std::string::String>,
) -> Self {
Expand Down Expand Up @@ -100,17 +104,17 @@ pub enum FruitOrVeg {
Veg(Veggie),
Fruit(Fruit),
}
impl From<&FruitOrVeg> for FruitOrVeg {
impl ::std::convert::From<&FruitOrVeg> for FruitOrVeg {
fn from(value: &FruitOrVeg) -> Self {
value.clone()
}
}
impl From<Veggie> for FruitOrVeg {
impl ::std::convert::From<Veggie> for FruitOrVeg {
fn from(value: Veggie) -> Self {
Self::Veg(value)
}
}
impl From<Fruit> for FruitOrVeg {
impl ::std::convert::From<Fruit> for FruitOrVeg {
fn from(value: Fruit) -> Self {
Self::Fruit(value)
}
Expand Down Expand Up @@ -148,7 +152,7 @@ pub struct Veggie {
#[serde(rename = "veggieName")]
pub veggie_name: ::std::string::String,
}
impl From<&Veggie> for Veggie {
impl ::std::convert::From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
value.clone()
}
Expand Down Expand Up @@ -187,8 +191,16 @@ pub struct Veggies {
#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
pub vegetables: ::std::vec::Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
impl ::std::convert::From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
value.clone()
}
}
impl ::std::default::Default for Veggies {
fn default() -> Self {
Self {
fruits: Default::default(),
vegetables: Default::default(),
}
}
}
Loading
Loading