Skip to content

Commit f7c2155

Browse files
bors[bot]burrbull
andcommitted
Merge #315
315: Remove Mask offset consts r=therealprof a=burrbull cc @therealprof Example: ```rust pub unsafe fn bits(self, value: u8) -> &'a mut W { self.w.bits &= !(0x1f << 8); self.w.bits |= ((value as u32) & 0x1f) << 8; self.w } pub fn dbl(&self) -> DBLR { let bits = ((self.bits >> 8) & 0x1f) as u8; DBLR { bits } } ``` Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 3f07a93 + 864327a commit f7c2155

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/generate/register.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn render(
107107
w_impl_items.push(quote! {
108108
/// Reset value of the register
109109
#[inline]
110-
pub fn reset_value() -> W {
110+
pub const fn reset_value() -> W {
111111
W { bits: #rv }
112112
}
113113

@@ -257,7 +257,7 @@ pub fn fields(
257257
access: f.access,
258258
evs: &f.enumerated_values,
259259
sc: Ident::new(&*sc),
260-
mask: util::hex_or_bool((((1 as u64) << width) - 1) as u32, width),
260+
mask: util::hex((((1 as u64) << width) - 1) as u32),
261261
name: &f.name,
262262
offset: util::unsuffixed(u64::from(f.bit_range.offset)),
263263
ty: width.to_ty()?,
@@ -285,10 +285,7 @@ pub fn fields(
285285
quote! { as #fty }
286286
};
287287
let value = quote! {
288-
const MASK: #fty = #mask;
289-
const OFFSET: u8 = #offset;
290-
291-
((self.bits >> OFFSET) & MASK as #rty) #cast
288+
((self.bits >> #offset) & #mask) #cast
292289
};
293290

294291
if let Some((evs, base)) = lookup(
@@ -519,7 +516,7 @@ pub fn fields(
519516
#[doc = #description]
520517
#[inline]
521518
pub fn #sc(&self) -> #pc_r {
522-
let bits = { #value };
519+
let bits = #value;
523520
#pc_r { bits }
524521
}
525522
});
@@ -763,11 +760,8 @@ pub fn fields(
763760
/// Writes raw bits to the field
764761
#[inline]
765762
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
766-
const MASK: #fty = #mask;
767-
const OFFSET: u8 = #offset;
768-
769-
self.w.bits &= !((MASK as #rty) << OFFSET);
770-
self.w.bits |= ((value & MASK) as #rty) << OFFSET;
763+
self.w.bits &= !(#mask << #offset);
764+
self.w.bits |= ((value as #rty) & #mask) << #offset;
771765
self.w
772766
}
773767
});

0 commit comments

Comments
 (0)