Skip to content

Commit 6987ca9

Browse files
bors[bot]roblabla
andauthored
Merge #402
402: Simplify Interrupt::nr r=therealprof a=roblabla Make the Interrupt enum descriminant match the interrupt nr directly. This allows simplifying Interrupt::nr from being a complicated match to a simple cast to an u8. According to #370, this should lead to a code reduction of as much as 304 bytes. Fixes #211 Fixes #370 Co-authored-by: roblabla <unfiltered@roblab.la>
2 parents 358f9ec + 1b05555 commit 6987ca9

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/generate/interrupt.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub fn render(
2525
interrupts.sort_by_key(|i| i.value);
2626

2727
let mut root = TokenStream::new();
28-
let mut arms = vec![];
2928
let mut from_arms = vec![];
3029
let mut elements = vec![];
3130
let mut names = vec![];
@@ -58,11 +57,7 @@ pub fn render(
5857

5958
variants.push(quote! {
6059
#[doc = #description]
61-
#name_uc,
62-
});
63-
64-
arms.push(quote! {
65-
Interrupt::#name_uc => #value,
60+
#name_uc = #value,
6661
});
6762

6863
from_arms.push(quote! {
@@ -151,19 +146,25 @@ pub fn render(
151146
Target::None => {}
152147
}
153148

149+
let self_token = quote!(self);
150+
let (enum_repr, nr_expr) = if variants.is_empty() {
151+
(quote!(), quote!(match *#self_token {}))
152+
} else {
153+
(quote!(#[repr(u8)]), quote!(*#self_token as u8))
154+
};
155+
154156
let interrupt_enum = quote! {
155157
///Enumeration of all the interrupts
156158
#[derive(Copy, Clone, Debug)]
159+
#enum_repr
157160
pub enum Interrupt {
158161
#(#variants)*
159162
}
160163

161164
unsafe impl bare_metal::Nr for Interrupt {
162-
#[inline]
163-
fn nr(&self) -> u8 {
164-
match *self {
165-
#(#arms)*
166-
}
165+
#[inline(always)]
166+
fn nr(&#self_token) -> u8 {
167+
#nr_expr
167168
}
168169
}
169170
};

0 commit comments

Comments
 (0)