Skip to content

Commit 0cfcc58

Browse files
committed
raw reg array iterator
1 parent 10d4ae5 commit 0cfcc58

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/generate/peripheral/accessor.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use proc_macro2::{Ident, TokenStream, Span};
1+
use proc_macro2::{Ident, Span, TokenStream};
22
use quote::{quote, ToTokens};
33

44
#[derive(Clone, Debug)]
@@ -176,13 +176,21 @@ impl ToTokens for RawArrayAccessor {
176176
dim,
177177
increment,
178178
} = self;
179+
let name_iter = Ident::new(&format!("{name}_iter"), Span::call_site());
180+
let cast = quote! { unsafe { &*(self as *const Self).cast::<u8>().add(#offset).add(#increment * n).cast() } };
179181
quote! {
180182
#[doc = #doc]
181183
#[inline(always)]
182184
pub const fn #name(&self, n: usize) -> &#ty {
183185
#[allow(clippy::no_effect)]
184186
[(); #dim][n];
185-
unsafe { &*(self as *const Self).cast::<u8>().add(#offset).add(#increment * n).cast() }
187+
#cast
188+
}
189+
#[doc = "Iterator for array of:"]
190+
#[doc = #doc]
191+
#[inline(always)]
192+
pub fn #name_iter(&self) -> impl Iterator<Item=&#ty> {
193+
(0..#dim).map(|n| #cast)
186194
}
187195
}
188196
.to_tokens(tokens);

0 commit comments

Comments
 (0)