Skip to content

Commit 09f31e8

Browse files
committed
Add TypeInfo variants for tagged unions (enums)
Add a seperate variant for C-style enums. Right now, this doesn't work with procedural-derive.
1 parent bf2c667 commit 09f31e8

File tree

3 files changed

+319
-13
lines changed

3 files changed

+319
-13
lines changed

lib/derive-internals/src/fields.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,22 @@ fn handle_type<'a, T: TypeHandler<'a>>(
242242
#into_type
243243
}))
244244
}
245+
fn is_c_style_enum(data: &DataEnum) -> bool {
246+
/*
247+
* TODO: Should the following be considered a 'c-style' enum?
248+
* enum Test {
249+
* One,
250+
* Two { },
251+
* Three( )
252+
* }
253+
*
254+
* Right now it *is*, because `fields.is_empty`,
255+
* although we could require them all to be unit-variants (like `One`)
256+
*/
257+
data.variants.iter().all(|var| var.fields.is_empty())
258+
}
245259
fn enum_static_type(data: &DataEnum, name: &Ident) -> Result<TokenStream, syn::Error> {
246-
if data.variants.iter().all(|var| var.fields.is_empty()) {
260+
if is_c_style_enum(data) {
247261
// C-style enum
248262
// TODO: Strict typing
249263
// TODO: Should we always assume that we're unsigned?
@@ -451,11 +465,11 @@ impl<'a> TypeHandler<'a> for UnionTypeHandler<'a> {
451465
}
452466

453467
fn type_def_type() -> TokenStream {
454-
quote!(static_reflect::types::UnionDef)
468+
quote!(static_reflect::types::UntaggedUnionDef)
455469
}
456470

457471
fn def_into_type(def_ref: TokenStream) -> TokenStream {
458-
quote!(static_reflect::types::TypeInfo::Union(#def_ref))
472+
quote!(static_reflect::types::TypeInfo::UntaggedUnion(#def_ref))
459473
}
460474

461475
fn handle_fields<F: FnMut(FieldInfo<'a>)>(&mut self, mut handler: F) -> syn::Result<()> {
@@ -498,7 +512,7 @@ impl<'a> TypeHandler<'a> for UnionTypeHandler<'a> {
498512
quote!({
499513
use std::mem::{size_of, align_of};
500514
#header
501-
let def = UnionDef {
515+
let def = UntaggedUnionDef {
502516
name: stringify!(#name),
503517
fields: _FIELDS,
504518
size: size_of::<#name>(),

lib/derive/tests/simple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use pretty_assertions::assert_eq;
77

88
use static_reflect::{field_offset, StaticReflect, FieldReflect};
9-
use static_reflect::types::{TypeInfo, FieldDef, StructureDef, UnionDef, UnionFieldDef, TypeId};
9+
use static_reflect::types::{TypeInfo, FieldDef, StructureDef, UntaggedUnionDef, UnionFieldDef, TypeId};
1010
use std::mem::{size_of, align_of};
1111

1212
use static_reflect_derive::{StaticReflect};
@@ -49,7 +49,7 @@ union SimpleUnion {
4949

5050
#[test]
5151
fn test_union_types() {
52-
const EXPECTED_UNION: TypeInfo<'static> = TypeInfo::Union(&UnionDef {
52+
const EXPECTED_UNION: TypeInfo<'static> = TypeInfo::UntaggedUnion(&UntaggedUnionDef {
5353
name: "SimpleUnion",
5454
fields: &[
5555
SimpleUnion::NAMED_FIELD_INFO.text.erase(),

0 commit comments

Comments
 (0)