Skip to content

Commit dbd8734

Browse files
committed
clean accessors
1 parent cb66cd4 commit dbd8734

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
- Skip generating `.add(0)` and `1 *` in accessors
1011
- Bump MSRV of generated code to 1.76
1112
- move `must_use` from methods to generic type
1213

src/generate/peripheral.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
998998
doc,
999999
name,
10001000
ty,
1001-
offset: unsuffixed(info.address_offset),
1001+
offset: info.address_offset,
10021002
})
10031003
.raw_if(false);
10041004
cluster_expanded.push(RegisterBlockField {
@@ -1057,9 +1057,9 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10571057
doc,
10581058
name: accessor_name.clone(),
10591059
ty: ty.clone(),
1060-
offset: unsuffixed(info.address_offset),
1061-
dim: unsuffixed(array_info.dim),
1062-
increment: unsuffixed(array_info.dim_increment),
1060+
offset: info.address_offset,
1061+
dim: array_info.dim,
1062+
increment: array_info.dim_increment,
10631063
})
10641064
.raw_if(!array_convertible),
10651065
);
@@ -1071,7 +1071,6 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
10711071
ci.address_offset,
10721072
ci.description.as_deref().unwrap_or(&ci.name),
10731073
);
1074-
let i = unsuffixed(i as u64);
10751074
accessors.push(
10761075
Accessor::ArrayElem(ArrayElemAccessor {
10771076
doc,
@@ -1114,7 +1113,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
11141113
doc,
11151114
name,
11161115
ty: ty.clone(),
1117-
offset: unsuffixed(info.address_offset),
1116+
offset: info.address_offset,
11181117
})
11191118
.raw_if(false);
11201119
cluster_expanded.push(RegisterBlockField {
@@ -1166,7 +1165,7 @@ fn expand_register(
11661165
doc,
11671166
name,
11681167
ty,
1169-
offset: unsuffixed(info.address_offset),
1168+
offset: info.address_offset,
11701169
})
11711170
.raw_if(false);
11721171
register_expanded.push(RegisterBlockField {
@@ -1240,9 +1239,9 @@ fn expand_register(
12401239
doc,
12411240
name: accessor_name.clone(),
12421241
ty: ty.clone(),
1243-
offset: unsuffixed(info.address_offset),
1244-
dim: unsuffixed(array_info.dim),
1245-
increment: unsuffixed(array_info.dim_increment),
1242+
offset: info.address_offset,
1243+
dim: array_info.dim,
1244+
increment: array_info.dim_increment,
12461245
})
12471246
.raw_if(!array_convertible),
12481247
);
@@ -1259,7 +1258,6 @@ fn expand_register(
12591258
ri.address_offset,
12601259
ri.description.as_deref().unwrap_or(&ri.name),
12611260
);
1262-
let i = unsuffixed(i as u64);
12631261
accessors.push(
12641262
Accessor::ArrayElem(ArrayElemAccessor {
12651263
doc,
@@ -1302,7 +1300,7 @@ fn expand_register(
13021300
doc,
13031301
name,
13041302
ty: ty.clone(),
1305-
offset: unsuffixed(info.address_offset),
1303+
offset: info.address_offset,
13061304
})
13071305
.raw_if(false);
13081306
register_expanded.push(RegisterBlockField {

src/generate/peripheral/accessor.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use proc_macro2::{Ident, Span, TokenStream};
22
use quote::{quote, ToTokens};
33

4+
use crate::util::unsuffixed;
5+
46
#[derive(Clone, Debug)]
57
pub enum Accessor {
68
Reg(RegAccessor),
@@ -51,11 +53,12 @@ impl ToTokens for AccessType {
5153
ty,
5254
offset,
5355
})) => {
56+
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
5457
quote! {
5558
#[doc = #doc]
5659
#[inline(always)]
5760
pub const fn #name(&self) -> &#ty {
58-
unsafe { &*core::ptr::from_ref(self).cast::<u8>().add(#offset).cast() }
61+
unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .cast() }
5962
}
6063
}
6164
}
@@ -84,7 +87,10 @@ impl ToTokens for AccessType {
8487
increment,
8588
})) => {
8689
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
87-
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>().add(#offset).add(#increment * n).cast() } };
90+
let offset = (*offset != 0).then(|| unsuffixed(*offset)).map(|o| quote!(.add(#o)));
91+
let dim = unsuffixed(*dim);
92+
let increment = (*increment != 1).then(|| unsuffixed(*increment)).map(|i| quote!(#i *));
93+
let cast = quote! { unsafe { &*core::ptr::from_ref(self).cast::<u8>() #offset .add(#increment n).cast() } };
8894
quote! {
8995
#[doc = #doc]
9096
#[inline(always)]
@@ -109,6 +115,7 @@ impl ToTokens for AccessType {
109115
basename,
110116
i,
111117
} = elem;
118+
let i = unsuffixed(*i as u64);
112119
quote! {
113120
#[doc = #doc]
114121
#[inline(always)]
@@ -127,17 +134,17 @@ pub struct RegAccessor {
127134
pub doc: String,
128135
pub name: Ident,
129136
pub ty: syn::Type,
130-
pub offset: syn::LitInt,
137+
pub offset: u32,
131138
}
132139

133140
#[derive(Clone, Debug)]
134141
pub struct ArrayAccessor {
135142
pub doc: String,
136143
pub name: Ident,
137144
pub ty: syn::Type,
138-
pub offset: syn::LitInt,
139-
pub dim: syn::LitInt,
140-
pub increment: syn::LitInt,
145+
pub offset: u32,
146+
pub dim: u32,
147+
pub increment: u32,
141148
}
142149

143150
#[derive(Clone, Debug)]
@@ -146,5 +153,5 @@ pub struct ArrayElemAccessor {
146153
pub name: Ident,
147154
pub ty: syn::Type,
148155
pub basename: Ident,
149-
pub i: syn::LitInt,
156+
pub i: usize,
150157
}

0 commit comments

Comments
 (0)