Skip to content

Commit

Permalink
Enum variants can now be marked as default and will now implement the…
Browse files Browse the repository at this point in the history
… `Default` trait constructing the annotated variant. (similar to structs)
  • Loading branch information
ImaMapleTree committed Jun 2, 2024
1 parent d83f1e4 commit 99eafd1
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "derive-ctor"
version = "1.0.1"
version = "1.0.2"
description = "Adds `#[derive(ctor)]` which allows for the auto-generation of struct, enum, and union constructors."
keywords = ["derive", "macro", "trait", "procedural", "no_std"]
authors = ["Evan Cowin"]
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Add `derive-ctor` to your `Cargo.toml`:

```toml
[dependencies]
derive-ctor = "1.0.1"
derive-ctor = "1.0.2"
```

Annotate your struct with `#[derive(ctor)]` to automatically generate a `new` constructor:
Expand Down Expand Up @@ -105,7 +105,7 @@ respective variant and will be public. This default behaviour can be changed by
Specifying this attribute will change the **default** generated method for each variant, however, each variant
can additionally define its own configuration which overrides the one defined by the enum.

### Default variant constructor example
### Standard variant constructor example

```rust
use derive_ctor::ctor;
Expand Down Expand Up @@ -144,7 +144,9 @@ let v2 = MyEnum::new_variant2();
let v3 = MyEnum::new_variant3();
```

If a variant is derived with `#[ctor(none)]` it will **not** have a constructor generated for it.
If a variant is derived with `#[ctor(none)]` it will **not** have a constructor generated for it. Similarly,
if a variant is derived with `#[ctor(default)]` the `Default` trait will be automatically implemented to generate
that variant.

Unions express the same behaviours as enums except applicable to the fields of the union rather than the variants of
an enum.
Expand All @@ -161,7 +163,7 @@ union MyUnion {
v3: u32
}

const VAL地震: MyUnion = MyUnion::new(100);
const VAL: MyUnion = MyUnion::new(100);
let v2 = MyUnion::new_v2(123.231);
let v3 = MyUnion::new_v3(414224);
```
Expand Down
24 changes: 21 additions & 3 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn create_ctor_enum_impl(
configuration: CtorEnumConfiguration,
) -> TokenStream {
let mut methods = Vec::new();
let mut default_method = None;

for variant in variants {
let variant_code = match &variant.fields {
Expand Down Expand Up @@ -174,22 +175,39 @@ fn create_ctor_enum_impl(
} else {
quote! { Self::#variant_name }
};

methods.push(quote! {
let method_token_stream = quote! {
#visibility #const_tkn fn #name(#(#parameter_fields),*) -> Self {
#(#generated_fields)*
#enum_generation
}
})
};

if def.attrs.contains(&CtorAttribute::Default) {
default_method = Some(method_token_stream);
} else {
methods.push(method_token_stream);
}
}
}

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let default_impl = if let Some(def_method) = default_method {
quote! {
impl #impl_generics Default for # ident # ty_generics #where_clause {
#def_method
}
}
} else {
quote! {}
};

TokenStream::from(quote! {
impl #impl_generics #ident #ty_generics #where_clause {
#(#methods)*
}
#default_impl
})
}

Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions tests/enum_variant_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,34 @@ fn test_variant_config_overrides_default_values() {

// variant3 was not generated
}




#[derive(ctor, Debug, PartialEq)]
enum DefaultVariantEnum {
#[allow(dead_code)]
Variant1,
#[ctor(default)]
Variant2 { #[ctor(default)] value: i32 }
}

#[test]
fn test_default_variant_enum() {
let variant2 = Default::default();
assert_eq!(DefaultVariantEnum::Variant2 { value: 0 }, variant2);
}

#[derive(ctor, Debug, PartialEq)]
enum DefaultAllEnum {
#[allow(dead_code)]
Variant1,
#[ctor(default(all))]
Variant2 { value: i32, #[ctor(expr("A".to_string()))] other: String }
}

#[test]
fn test_default_all_variant_enum() {
let variant2 = Default::default();
assert_eq!(DefaultAllEnum::Variant2 { value: 0, other: "A".to_string() }, variant2)
}

0 comments on commit 99eafd1

Please sign in to comment.