Skip to content

Commit 6deef19

Browse files
committed
Remove IsVariant trait and corresponding derive macro
1 parent c6e69e3 commit 6deef19

File tree

15 files changed

+13
-327
lines changed

15 files changed

+13
-327
lines changed

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ Safe casting for newtype enums and their variants.
1313

1414
The `enumcapsulate` crate exports the following traits:
1515

16-
| Traits | Functionality |
17-
| --------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
18-
| `AsVariant` | Provides owned access to the current variant's field. |
19-
| `AsVariantMut` | Provides mutable borrowed access to the current variant's field. |
20-
| `AsVariantRef` | Provides borrowed access to the current variant's field. |
21-
| `Encapsulate` | Umbrella derive macro for `AsVariant`, `AsVariantMut`, `AsVariantRef`, `From`, `FromVariant`, `IntoVariant`, `IsVariant`, `TryInto`, `VariantDiscriminant`, and `VariantDowncast` |
22-
| `FromVariant` | Creates an instance of `Self` from the unambiguous field type of one of its variants. |
23-
| `IntoVariant` | Returns the current variant's field, consuming `self`. |
24-
| `IsVariant` | Used to check type of an enum's inner variant's type |
25-
| `VariantDiscriminant` | Used to obtain an enum variant's discriminant |
26-
| `VariantDowncast` | Convenience umbrella trait utilizing `AsVariant`, `AsVariantRef`, `AsVariantMut`, and `IntoVariant` |
16+
| Traits | Functionality |
17+
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
18+
| `AsVariant` | Provides owned access to the current variant's field. |
19+
| `AsVariantMut` | Provides mutable borrowed access to the current variant's field. |
20+
| `AsVariantRef` | Provides borrowed access to the current variant's field. |
21+
| `Encapsulate` | Umbrella derive macro for `AsVariant`, `AsVariantMut`, `AsVariantRef`, `From`, `FromVariant`, `IntoVariant`, `TryInto`, `VariantDiscriminant`, and `VariantDowncast` |
22+
| `FromVariant` | Creates an instance of `Self` from the unambiguous field type of one of its variants. |
23+
| `IntoVariant` | Returns the current variant's field, consuming `self`. |
24+
| `VariantDiscriminant` | Used to obtain an enum variant's discriminant |
25+
| `VariantDowncast` | Convenience umbrella trait utilizing `AsVariant`, `AsVariantRef`, `AsVariantMut`, and `IntoVariant` |
2726

2827
## Derive macros
2928

@@ -35,9 +34,7 @@ The `enumcapsulate` crate exports the following corresponding derive macros, if
3534
- `Encapsulate`
3635
- `From`
3736
- `FromVariant`
38-
- `IntoVariant`
39-
- `IsVariant`
40-
- `TryInto`
37+
- `IntoVariant`- `TryInto`
4138
- `VariantDiscriminant`
4239

4340
## Documentation

macros/README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ The `enumcapsulate-macros` proc-macro crate exports the following derive macros:
2222
| `AsVariant` | Derives impls for `Self: enumcapsulate::AsVariant<T>` for each non-unit variant type `T`. |
2323
| `AsVariantMut` | Derives impls for `Self: enumcapsulate::AsVariantMut<T>` for each non-unit variant type `T`. |
2424
| `AsVariantRef` | Derives impls for `Self: enumcapsulate::AsVariantRef<T>` for each non-unit variant type `T`. |
25-
| `IsVariant` | Derives impl for `enumcapsulate::IsVariant`. |
2625
| `VariantDiscriminant` | Derives impl for `Self: enumcapsulate::VariantDiscriminant`. |
2726

2827
… as well as an umbrella derive macro `Encapsulate`.
@@ -32,7 +31,7 @@ The `enumcapsulate-macros` proc-macro crate exports the following derive macros:
3231
3332
### `#[derive(Encapsulate)]`
3433

35-
The umbrella derive macro `Encapsulate` allows for conveniently deriving `AsVariant`, `AsVariantMut`, `AsVariantRef`, `From`, `FromVariant`, `IntoVariant`, `IsVariant`, `TryInto`, `VariantDiscriminant`, and `VariantDowncast` all at once.
34+
The umbrella derive macro `Encapsulate` allows for conveniently deriving `AsVariant`, `AsVariantMut`, `AsVariantRef`, `From`, `FromVariant`, `IntoVariant`, `TryInto`, `VariantDiscriminant`, and `VariantDowncast` all at once.
3635

3736
The following two snippets are semantically equivalent:
3837

@@ -50,7 +49,6 @@ enum Enum { /* ... */ }
5049
AsVariant,
5150
AsVariantMut,
5251
AsVariantRef,
53-
IsVariant,
5452
VariantDiscriminant,
5553
)]
5654
enum Enum { /* ... */ }

macros/src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ static RECOGNIZED_ENUM_LEVEL_MACROS: &[&str] = &[
1010
macro_name::AS_VARIANT,
1111
macro_name::AS_VARIANT_REF,
1212
macro_name::AS_VARIANT_MUT,
13-
macro_name::IS_VARIANT,
1413
macro_name::VARIANT_DISCRIMINANT,
1514
macro_name::VARIANT_DOWNCAST,
1615
];

macros/src/enum_deriver.rs

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -508,86 +508,6 @@ impl EnumDeriver {
508508
})
509509
}
510510

511-
pub fn derive_is_variant(&self) -> Result<TokenStream2, syn::Error> {
512-
const DERIVE_MACRO_NAME: &str = macro_name::AS_VARIANT_REF;
513-
514-
let derive_input = &self.input;
515-
let enum_ident = &derive_input.ident;
516-
517-
let enum_config = config_for_enum_with_attrs(&self.input.attrs)?;
518-
519-
if enum_config.is_excluded(DERIVE_MACRO_NAME) {
520-
return Ok(TokenStream2::default());
521-
}
522-
523-
let outer = enum_ident;
524-
let outer_ty: Type = parse_quote_spanned! { outer.span() => #outer };
525-
526-
let mut match_arms: Vec<TokenStream2> = vec![];
527-
528-
for variant in self.variants()? {
529-
let variant_ident = &variant.ident;
530-
let inner = variant_ident;
531-
532-
let variant_config = config_for_variant(variant)?;
533-
534-
if variant_config.is_excluded(DERIVE_MACRO_NAME, &enum_config) {
535-
continue;
536-
}
537-
538-
let Some(selection_index) =
539-
position_of_selected_field(&variant.fields, variant_config.field.as_ref())?
540-
else {
541-
continue;
542-
};
543-
544-
let fields: Vec<_> = variant.fields.iter().collect();
545-
let inner_field = fields[selection_index];
546-
let inner_ident = inner_field.ident.as_ref();
547-
548-
let pattern = match &variant.fields {
549-
Fields::Named(_) => {
550-
let field = inner_ident;
551-
quote! { #outer_ty::#inner { #field: inner, .. } }
552-
}
553-
Fields::Unnamed(_) => {
554-
let underscores = (0..selection_index).map(|_| {
555-
quote! { _, }
556-
});
557-
quote! { #outer_ty::#inner(#(#underscores)* inner, ..) }
558-
}
559-
Fields::Unit => continue,
560-
};
561-
562-
match_arms.push(quote! {
563-
#pattern => type_id_of_val(inner) == type_id,
564-
});
565-
}
566-
567-
Ok(quote! {
568-
impl ::enumcapsulate::IsVariant for #outer_ty {
569-
fn is_variant<T>(&self) -> bool
570-
where
571-
T: 'static + ?Sized
572-
{
573-
use ::std::any::TypeId;
574-
575-
#[inline]
576-
pub fn type_id_of_val<T: 'static + ?Sized>(_val: &T) -> TypeId {
577-
TypeId::of::<T>()
578-
}
579-
580-
let type_id = TypeId::of::<T>();
581-
582-
match self {
583-
#(#match_arms)*
584-
_ => false,
585-
}
586-
}
587-
}
588-
})
589-
}
590-
591511
pub fn derive_variant_downcast(&self) -> Result<TokenStream2, syn::Error> {
592512
const DERIVE_MACRO_NAME: &str = macro_name::VARIANT_DOWNCAST;
593513

macros/src/lib.rs

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -317,45 +317,6 @@ pub fn derive_variant_downcast(input: TokenStream) -> TokenStream {
317317
})
318318
}
319319

320-
/// Derive macro generating an impl of the trait `IsVariant`.
321-
///
322-
/// The generated impl looks something along these lines:
323-
///
324-
/// ```ignore
325-
/// struct Inner;
326-
///
327-
/// enum Outer {
328-
/// Inner(Inner),
329-
/// // ...
330-
/// }
331-
///
332-
/// // The generated impls look something along these lines:
333-
///
334-
/// impl IsVariant for Outer {
335-
/// fn is_variant<T>(&self) -> bool
336-
/// where
337-
/// T: 'static + ?Sized
338-
/// {
339-
/// match self {
340-
/// Outer::Inner(inner) => /* ... */,
341-
/// // ...
342-
/// }
343-
/// }
344-
/// }
345-
///
346-
/// // ...
347-
/// ```
348-
///
349-
#[proc_macro_derive(IsVariant, attributes(enumcapsulate))]
350-
pub fn derive_is_variant(input: TokenStream) -> TokenStream {
351-
let input: DeriveInput = parse_macro_input!(input);
352-
353-
tokenstream(|| {
354-
let deriver = EnumDeriver::from(input);
355-
deriver.derive_is_variant()
356-
})
357-
}
358-
359320
/// Derive macro generating an impl of the trait `VariantDiscriminant`.
360321
///
361322
/// ```ignore
@@ -416,7 +377,7 @@ pub fn derive_variant_discriminant(input: TokenStream) -> TokenStream {
416377
/// ```ignore
417378
/// // ...
418379
///
419-
/// #[derive(From, TryInto, FromVariant, AsVariantRef, AsVariantMut, IntoVariant, VariantDowncast, IsVariant, VariantDiscriminant)]
380+
/// #[derive(From, TryInto, FromVariant, AsVariant, AsVariantRef, AsVariantMut, IntoVariant, VariantDowncast, VariantDiscriminant)]
420381
/// enum Outer {
421382
/// // ...
422383
/// }
@@ -436,7 +397,6 @@ pub fn derive_encapsulate(input: TokenStream) -> TokenStream {
436397
let as_variant_mut = deriver.derive_as_variant_mut()?;
437398
let into_variant = deriver.derive_into_variant()?;
438399
let variant_downcast = deriver.derive_variant_downcast()?;
439-
let is_variant = deriver.derive_is_variant()?;
440400
let variant_discriminant = deriver.derive_variant_discriminant()?;
441401

442402
Ok(quote::quote! {
@@ -448,7 +408,6 @@ pub fn derive_encapsulate(input: TokenStream) -> TokenStream {
448408
#as_variant_mut
449409
#into_variant
450410
#variant_downcast
451-
#is_variant
452411
#variant_discriminant
453412
})
454413
})

macros/src/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub(crate) mod macro_name {
2323
pub(crate) const AS_VARIANT_REF: &str = "AsVariantRef";
2424
pub(crate) const AS_VARIANT_MUT: &str = "AsVariantMut";
2525

26-
pub(crate) const IS_VARIANT: &str = "IsVariant";
2726
pub(crate) const VARIANT_DISCRIMINANT: &str = "VariantDiscriminant";
2827
pub(crate) const VARIANT_DOWNCAST: &str = "VariantDowncast";
2928
}

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ pub trait VariantDowncast {
7676
}
7777
}
7878

79-
/// Used to check type of an enum's inner variant's type.
80-
pub trait IsVariant {
81-
/// Returns `true` if `T` matches the variant's type, otherwise `false`.
82-
fn is_variant<T>(&self) -> bool
83-
where
84-
T: 'static + ?Sized;
85-
}
86-
8779
/// Used to obtain an enum variant's discriminant.
8880
pub trait VariantDiscriminant {
8981
type Discriminant: Eq;

tests/derive-tests.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,6 @@ mod variant_downcast {
9494
}
9595
}
9696

97-
mod is_variant {
98-
#[test]
99-
pub fn pass() {
100-
tryexpand::expand(["tests/derive-tests/is_variant/pass/**/*.rs"]).and_check();
101-
}
102-
103-
// There should be no failures for this derive macro;
104-
}
105-
10697
mod variant_discriminant {
10798
#[test]
10899
pub fn pass() {

tests/derive-tests/encapsulate/pass/enum/tuple_variants/one_field.out.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,6 @@ impl ::enumcapsulate::IntoVariant<VariantB> for Enum {
130130
}
131131
}
132132
impl ::enumcapsulate::VariantDowncast for Enum {}
133-
impl ::enumcapsulate::IsVariant for Enum {
134-
fn is_variant<T>(&self) -> bool
135-
where
136-
T: 'static + ?Sized,
137-
{
138-
use ::std::any::TypeId;
139-
#[inline]
140-
pub fn type_id_of_val<T: 'static + ?Sized>(_val: &T) -> TypeId {
141-
TypeId::of::<T>()
142-
}
143-
let type_id = TypeId::of::<T>();
144-
match self {
145-
Enum::VariantA(inner, ..) => type_id_of_val(inner) == type_id,
146-
Enum::VariantB { b: inner, .. } => type_id_of_val(inner) == type_id,
147-
_ => false,
148-
}
149-
}
150-
}
151133
pub enum EnumDiscriminant {
152134
VariantA,
153135
VariantB,

tests/derive-tests/encapsulate/pass/enum/zero_variants.out.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,6 @@ use enumcapsulate::{
33
};
44
pub enum Enum {}
55
impl ::enumcapsulate::VariantDowncast for Enum {}
6-
impl ::enumcapsulate::IsVariant for Enum {
7-
fn is_variant<T>(&self) -> bool
8-
where
9-
T: 'static + ?Sized,
10-
{
11-
use ::std::any::TypeId;
12-
#[inline]
13-
pub fn type_id_of_val<T: 'static + ?Sized>(_val: &T) -> TypeId {
14-
TypeId::of::<T>()
15-
}
16-
let type_id = TypeId::of::<T>();
17-
match self {
18-
_ => false,
19-
}
20-
}
21-
}
226
pub enum EnumDiscriminant {}
237
#[automatically_derived]
248
impl ::core::marker::Copy for EnumDiscriminant {}

0 commit comments

Comments
 (0)