Skip to content

Commit f7cb4c3

Browse files
bors[bot]burrbull
andauthored
Merge #394
394: use repr(fty) for enums r=therealprof a=burrbull enums are now in C-style with `#repr(..)` Should slightly reduce code size in debug mode when enums are used. r? @Disasm cc @therealprof Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents 53fb816 + 51cc552 commit f7cb4c3

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313

1414
### Changed
1515

16+
- Enum items now associated with values (C-style), enums annotated with `repr(fty)`
1617
- Bump `svd-parser` dependency (0.8.1)
1718

1819
## [v0.16.1] - 2019-08-17

src/generate/register.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -636,14 +636,21 @@ impl Variant {
636636
fn add_from_variants(mod_items: &mut Vec<TokenStream>, variants: &Vec<Variant>, pc: &Ident, f: &F, desc: &str, reset_value: Option<u64>) {
637637
let fty = &f.ty;
638638

639+
let (repr, cast) = if f.ty == "bool" {
640+
(quote! { }, quote! { variant as u8 != 0 })
641+
} else {
642+
(quote! { #[repr(#fty)] }, quote! { variant as _ })
643+
};
644+
639645
let vars = variants
640646
.iter()
641647
.map(|v| {
642648
let desc = util::escape_brackets(&format!("{}: {}", v.value, v.doc));
643649
let pcv = &v.pc;
650+
let pcval = &util::unsuffixed(v.value);
644651
quote! {
645652
#[doc = #desc]
646-
#pcv
653+
#pcv = #pcval
647654
}
648655
})
649656
.collect::<Vec<_>>();
@@ -657,27 +664,14 @@ fn add_from_variants(mod_items: &mut Vec<TokenStream>, variants: &Vec<Variant>,
657664
mod_items.push(quote! {
658665
#[doc = #desc]
659666
#[derive(Clone, Copy, Debug, PartialEq)]
667+
#repr
660668
pub enum #pc {
661669
#(#vars),*
662670
}
663-
});
664-
665-
let arms = variants.iter().map(|v| {
666-
let pcv = &v.pc;
667-
let value = util::unsuffixed_or_bool(v.value, f.width);
668-
669-
quote! {
670-
#pc::#pcv => #value
671-
}
672-
});
673-
674-
mod_items.push(quote! {
675671
impl From<#pc> for #fty {
676672
#[inline(always)]
677673
fn from(variant: #pc) -> Self {
678-
match variant {
679-
#(#arms),*
680-
}
674+
#cast
681675
}
682676
}
683677
});

0 commit comments

Comments
 (0)