Skip to content

Commit 8c6faa6

Browse files
Change unit variants to use is_ functions
Currently unit variants return an option of the unit type. But this does not offer any more information or functionality then a predicate function. The is_* functions are nicer to use lead simpler code.
1 parent 3c16433 commit 8c6faa6

File tree

4 files changed

+35
-71
lines changed

4 files changed

+35
-71
lines changed

README.md

+3-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where the result is either a reference for inner items or a tuple containing the
3434

3535
## Unit case
3636

37-
This will return copy's of the value of the unit variant, as `isize`:
37+
This will return true if enum's variant matches the expected type
3838

3939
```rust
4040
use enum_as_inner::EnumAsInner;
@@ -45,18 +45,12 @@ enum UnitVariants {
4545
One,
4646
Two,
4747
}
48-
```
49-
50-
These are not references:
5148

52-
```rust
5349
let unit = UnitVariants::Two;
5450

55-
assert_eq!(unit.as_two().unwrap(), ());
51+
assert!(unit.is_two());
5652
```
5753

58-
Note that for unit enums there is no `into_*()` function generated.
59-
6054
## Mutliple, unnamed field case
6155

6256
This will return a tuple of the inner types:
@@ -105,4 +99,4 @@ let mut many = ManyVariants::Three{ one: true, two: 1, three: 2 };
10599
assert_eq!(many.as_three().unwrap(), (&true, &1_u32, &2_i64));
106100
assert_eq!(many.as_three_mut().unwrap(), (&mut true, &mut 1_u32, &mut 2_i64));
107101
assert_eq!(many.into_three().unwrap(), (true, 1_u32, 2_i64));
108-
```
102+
```

src/lib.rs

+16-26
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//!
3232
//! ## Unit case
3333
//!
34-
//! This will return copy's of the value of the unit variant, as `isize`:
34+
//! This will return true if enum's variant matches the expected type
3535
//!
3636
//! ```rust
3737
//! use enum_as_inner::EnumAsInner;
@@ -45,11 +45,9 @@
4545
//!
4646
//! let unit = UnitVariants::Two;
4747
//!
48-
//! assert_eq!(unit.as_two().unwrap(), ());
48+
//! assert!(unit.is_two());
4949
//! ```
5050
//!
51-
//! Note that for unit enums there is no `into_*()` function generated.
52-
//!
5351
//! ## Mutliple, unnamed field case
5452
//!
5553
//! This will return a tuple of the inner types:
@@ -105,29 +103,13 @@ use syn::DeriveInput;
105103
fn unit_fields_return(
106104
name: &syn::Ident,
107105
variant_name: &syn::Ident,
108-
function_name_mut: &Ident,
109106
function_name: &Ident,
110107
doc: &str,
111108
) -> TokenStream {
112109
quote!(
113-
#[doc = #doc ]
114-
pub fn #function_name_mut(&mut self) -> Option<()> {
115-
match self {
116-
#name::#variant_name => {
117-
Some(())
118-
}
119-
_ => None
120-
}
121-
}
122-
123-
#[doc = #doc ]
124-
pub fn #function_name(&self) -> Option<()> {
125-
match self {
126-
#name::#variant_name => {
127-
Some(())
128-
}
129-
_ => None
130-
}
110+
#[doc = #doc]
111+
pub fn #function_name(&self) -> bool {
112+
matches!(self, #name::#variant_name)
131113
}
132114
)
133115
}
@@ -335,13 +317,21 @@ fn impl_all_as_fns(ast: &DeriveInput) -> TokenStream {
335317
variant_name,
336318
);
337319

320+
let function_name_is = Ident::new(
321+
&format!("is_{}", variant_name).to_snake_case(),
322+
Span::call_site(),
323+
);
324+
let doc_is = format!(
325+
"Returns true if this is a `{}::{}`, otherwise false",
326+
name, variant_name,
327+
);
328+
338329
let tokens = match &variant_data.fields {
339330
syn::Fields::Unit => unit_fields_return(
340331
name,
341332
variant_name,
342-
&function_name_mut_ref,
343-
&function_name_ref,
344-
&doc_ref,
333+
&function_name_is,
334+
&doc_is,
345335
),
346336
syn::Fields::Unnamed(unnamed) => unnamed_fields_return(
347337
name,

tests/snake_case.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use enum_as_inner::EnumAsInner;
22

33
#[derive(Debug, EnumAsInner)]
4+
#[allow(clippy::upper_case_acronyms)]
45
enum MixedCaseVariants {
56
XMLIsNotCool,
67
#[allow(non_camel_case_types)]
@@ -14,20 +15,16 @@ enum MixedCaseVariants {
1415
fn test_xml_unit() {
1516
let mixed = MixedCaseVariants::XMLIsNotCool;
1617

17-
assert!(mixed.as_xml_is_not_cool().is_some());
18+
assert!(mixed.is_xml_is_not_cool());
1819
assert!(mixed.as_rust_is_cool_though().is_none());
1920
assert!(mixed.as_ymca().is_none());
20-
21-
mixed
22-
.as_xml_is_not_cool()
23-
.expect("should have been some unit");
2421
}
2522

2623
#[test]
2724
fn test_rust_unnamed() {
2825
let mixed = MixedCaseVariants::Rust_IsCoolThough(42);
2926

30-
assert!(mixed.as_xml_is_not_cool().is_none());
27+
assert!(!mixed.is_xml_is_not_cool());
3128
assert!(mixed.as_rust_is_cool_though().is_some());
3229
assert!(mixed.as_ymca().is_none());
3330

@@ -39,7 +36,7 @@ fn test_rust_unnamed() {
3936
fn test_ymca_named() {
4037
let mixed = MixedCaseVariants::YMCA { named: -32_768 };
4138

42-
assert!(mixed.as_xml_is_not_cool().is_none());
39+
assert!(!mixed.is_xml_is_not_cool());
4340
assert!(mixed.as_rust_is_cool_though().is_none());
4441
assert!(mixed.as_ymca().is_some());
4542

tests/unit.rs

+12-29
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,28 @@ enum UnitVariants {
99

1010
#[test]
1111
fn test_zero_unit() {
12-
let mut unit = UnitVariants::Zero;
12+
let unit = UnitVariants::Zero;
1313

14-
assert!(unit.as_zero().is_some());
15-
assert!(unit.as_one().is_none());
16-
assert!(unit.as_two().is_none());
17-
18-
assert!(unit.as_zero_mut().is_some());
19-
assert!(unit.as_one_mut().is_none());
20-
assert!(unit.as_two_mut().is_none());
21-
22-
unit.as_zero().expect("expected ");
14+
assert!(unit.is_zero());
15+
assert!(!unit.is_one());
16+
assert!(!unit.is_two());
2317
}
2418

2519
#[test]
2620
fn test_one_unit() {
27-
let mut unit = UnitVariants::One;
28-
29-
assert!(unit.as_zero().is_none());
30-
assert!(unit.as_one().is_some());
31-
assert!(unit.as_two().is_none());
21+
let unit = UnitVariants::One;
3222

33-
assert!(unit.as_zero_mut().is_none());
34-
assert!(unit.as_one_mut().is_some());
35-
assert!(unit.as_two_mut().is_none());
23+
assert!(!unit.is_zero());
24+
assert!(unit.is_one());
25+
assert!(!unit.is_two());
3626

37-
unit.as_one().expect("should have been some unit");
3827
}
3928

4029
#[test]
4130
fn test_two_unit() {
42-
let mut unit = UnitVariants::Two;
43-
44-
assert!(unit.as_zero().is_none());
45-
assert!(unit.as_one().is_none());
46-
assert!(unit.as_two().is_some());
47-
48-
assert!(unit.as_zero_mut().is_none());
49-
assert!(unit.as_one_mut().is_none());
50-
assert!(unit.as_two_mut().is_some());
31+
let unit = UnitVariants::Two;
5132

52-
unit.as_two().expect("should have been some unit");
33+
assert!(!unit.is_zero());
34+
assert!(!unit.is_one());
35+
assert!(unit.is_two());
5336
}

0 commit comments

Comments
 (0)