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

Fix clippy warning for enums that impl Copy #526

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
27 changes: 21 additions & 6 deletions typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,26 @@ impl TypeEntry {
)
}

// If the enum derives Copy it should dereference instead of cloning
// when converting to itself from a reference.
let from_ref_self_impl = if derive_set.contains("Copy") {
Some(quote! {
impl From<&#type_name> for #type_name {
fn from(value: &#type_name) -> Self {
*value
}
}
})
} else {
Some(quote! {
impl From<&#type_name> for #type_name {
fn from(value: &#type_name) -> Self {
value.clone()
}
}
})
};

// ToString and FromStr impls for enums that are made exclusively of
// simple variants (and are not untagged).
let simple_enum_impl = bespoke_impls
Expand Down Expand Up @@ -957,12 +977,7 @@ impl TypeEntry {
#(#variants_decl)*
}

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

#from_ref_self_impl
#simple_enum_impl
#default_impl
#untagged_newtype_from_string_impl
Expand Down
2 changes: 1 addition & 1 deletion typify-impl/tests/generator.out
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod types {
}
impl From<&StringEnum> for StringEnum {
fn from(value: &StringEnum) -> Self {
value.clone()
*value
}
}
impl ToString for StringEnum {
Expand Down
Loading
Loading