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

Use Self #502

Closed
wants to merge 2 commits into from
Closed
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
16 changes: 8 additions & 8 deletions cargo-typify/tests/outputs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl From<Fruit> for serde_json::Map<String, serde_json::Value> {
value.0
}
}
impl From<&Fruit> for Fruit {
fn from(value: &Fruit) -> Self {
impl From<&Self> for Fruit {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -97,8 +97,8 @@ pub enum FruitOrVeg {
Veg(Veggie),
Fruit(Fruit),
}
impl From<&FruitOrVeg> for FruitOrVeg {
fn from(value: &FruitOrVeg) -> Self {
impl From<&Self> for FruitOrVeg {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -145,8 +145,8 @@ pub struct Veggie {
#[serde(rename = "veggieName")]
pub veggie_name: String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
impl From<&Self> for Veggie {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -189,8 +189,8 @@ pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
impl From<&Self> for Veggies {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down
16 changes: 8 additions & 8 deletions cargo-typify/tests/outputs/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl From<Fruit> for serde_json::Map<String, serde_json::Value> {
value.0
}
}
impl From<&Fruit> for Fruit {
fn from(value: &Fruit) -> Self {
impl From<&Self> for Fruit {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -97,8 +97,8 @@ pub enum FruitOrVeg {
Veg(Veggie),
Fruit(Fruit),
}
impl From<&FruitOrVeg> for FruitOrVeg {
fn from(value: &FruitOrVeg) -> Self {
impl From<&Self> for FruitOrVeg {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -145,8 +145,8 @@ pub struct Veggie {
#[serde(rename = "veggieName")]
pub veggie_name: String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
impl From<&Self> for Veggie {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -184,8 +184,8 @@ pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
impl From<&Self> for Veggies {
fn from(value: &Self) -> Self {
value.clone()
}
}
16 changes: 8 additions & 8 deletions cargo-typify/tests/outputs/multi_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl From<Fruit> for serde_json::Map<String, serde_json::Value> {
value.0
}
}
impl From<&Fruit> for Fruit {
fn from(value: &Fruit) -> Self {
impl From<&Self> for Fruit {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -97,8 +97,8 @@ pub enum FruitOrVeg {
Veg(Veggie),
Fruit(Fruit),
}
impl From<&FruitOrVeg> for FruitOrVeg {
fn from(value: &FruitOrVeg) -> Self {
impl From<&Self> for FruitOrVeg {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -145,8 +145,8 @@ pub struct Veggie {
#[serde(rename = "veggieName")]
pub veggie_name: String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
impl From<&Self> for Veggie {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -184,8 +184,8 @@ pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
impl From<&Self> for Veggies {
fn from(value: &Self) -> Self {
value.clone()
}
}
16 changes: 8 additions & 8 deletions cargo-typify/tests/outputs/no-builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl From<Fruit> for serde_json::Map<String, serde_json::Value> {
value.0
}
}
impl From<&Fruit> for Fruit {
fn from(value: &Fruit) -> Self {
impl From<&Self> for Fruit {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -97,8 +97,8 @@ pub enum FruitOrVeg {
Veg(Veggie),
Fruit(Fruit),
}
impl From<&FruitOrVeg> for FruitOrVeg {
fn from(value: &FruitOrVeg) -> Self {
impl From<&Self> for FruitOrVeg {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -145,8 +145,8 @@ pub struct Veggie {
#[serde(rename = "veggieName")]
pub veggie_name: String,
}
impl From<&Veggie> for Veggie {
fn from(value: &Veggie) -> Self {
impl From<&Self> for Veggie {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -184,8 +184,8 @@ pub struct Veggies {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub vegetables: Vec<Veggie>,
}
impl From<&Veggies> for Veggies {
fn from(value: &Veggies) -> Self {
impl From<&Self> for Veggies {
fn from(value: &Self) -> Self {
value.clone()
}
}
2 changes: 1 addition & 1 deletion typify-impl/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl TypeEntry {
} else {
let n = self.type_ident(type_space, &Some("super".to_string()));
let value = self
.output_value(type_space, default, &quote! { super:: })
.output_value(type_space, default, &quote! { super:: }, false)
.unwrap_or_else(|| {
panic!(
"{}\nvalue: {}\ntype: {:#?}",
Expand Down
8 changes: 4 additions & 4 deletions typify-impl/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1473,8 +1473,8 @@ mod tests {
Err(String),
}

impl From<&ResultX> for ResultX {
fn from(value: &ResultX) -> Self {
impl From<&Self> for ResultX {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -1525,8 +1525,8 @@ mod tests {
Err(String),
}

impl From<&ResultX> for ResultX {
fn from(value: &ResultX) -> Self {
impl From<&Self> for ResultX {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down
26 changes: 16 additions & 10 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,9 @@ impl TypeEntry {
});

let default_impl = default.as_ref().map(|value| {
let default_stream = self.output_value(type_space, &value.0, &quote! {}).unwrap();
let default_stream = self
.output_value(type_space, &value.0, &quote! {}, true)
.unwrap();
quote! {
impl Default for #type_name {
fn default() -> Self {
Expand Down Expand Up @@ -957,8 +959,8 @@ impl TypeEntry {
#(#variants_decl)*
}

impl From<&#type_name> for #type_name {
fn from(value: &#type_name) -> Self {
impl From<&Self> for #type_name {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down Expand Up @@ -1078,8 +1080,8 @@ impl TypeEntry {
)*
}

impl From<&#type_name> for #type_name {
fn from(value: &#type_name) -> Self {
impl From<&Self> for #type_name {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand All @@ -1088,7 +1090,9 @@ impl TypeEntry {

// If there's a default value, generate an impl Default
if let Some(value) = default {
let default_stream = self.output_value(type_space, &value.0, &quote! {}).unwrap();
let default_stream = self
.output_value(type_space, &value.0, &quote! {}, false)
.unwrap();
output.add_item(
OutputSpaceMod::Crate,
name,
Expand Down Expand Up @@ -1329,7 +1333,7 @@ impl TypeEntry {

let value_output = enum_values
.iter()
.map(|value| inner_type.output_value(type_space, &value.0, &quote! {}));
.map(|value| inner_type.output_value(type_space, &value.0, &quote! {}, false));
// TODO if the sub_type is a string we could probably impl
// TryFrom<&str> as well and FromStr.
// TODO maybe we want to handle JsonSchema here
Expand Down Expand Up @@ -1477,7 +1481,9 @@ impl TypeEntry {
};

let default_impl = default.as_ref().map(|value| {
let default_stream = self.output_value(type_space, &value.0, &quote! {}).unwrap();
let default_stream = self
.output_value(type_space, &value.0, &quote! {}, false)
.unwrap();
quote! {
impl Default for #type_name {
fn default() -> Self {
Expand Down Expand Up @@ -1512,8 +1518,8 @@ impl TypeEntry {
}
}

impl From<&#type_name> for #type_name {
fn from(value: &#type_name) -> Self {
impl From<&Self> for #type_name {
fn from(value: &Self) -> Self {
value.clone()
}
}
Expand Down
Loading
Loading