Skip to content

Commit 4a1847c

Browse files
committed
peripheral to tokens closure
1 parent 5f8950f commit 4a1847c

File tree

1 file changed

+65
-79
lines changed

1 file changed

+65
-79
lines changed

src/generate/peripheral.rs

+65-79
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt;
55
use svd_parser::expand::{
66
derive_cluster, derive_peripheral, derive_register, BlockPath, Index, RegisterPath,
77
};
8+
use syn::LitInt;
89

910
use crate::config::Config;
1011
use crate::svd::{
@@ -80,6 +81,54 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
8081
}
8182
};
8283

84+
let per_to_tokens = |out: &mut TokenStream,
85+
feature_attribute: &TokenStream,
86+
description: &str,
87+
p_ty: &Ident,
88+
doc_alias: Option<TokenStream>,
89+
address: LitInt| {
90+
out.extend(quote! {
91+
#[doc = #description]
92+
#doc_alias
93+
#feature_attribute
94+
pub struct #p_ty { _marker: PhantomData<*const ()> }
95+
96+
#feature_attribute
97+
unsafe impl Send for #p_ty {}
98+
99+
#feature_attribute
100+
impl #p_ty {
101+
///Pointer to the register block
102+
pub const PTR: *const #base::RegisterBlock = #address as *const _;
103+
104+
///Return the pointer to the register block
105+
#[inline(always)]
106+
pub const fn ptr() -> *const #base::RegisterBlock {
107+
Self::PTR
108+
}
109+
110+
#steal_fn
111+
}
112+
113+
#feature_attribute
114+
impl Deref for #p_ty {
115+
type Target = #base::RegisterBlock;
116+
117+
#[inline(always)]
118+
fn deref(&self) -> &Self::Target {
119+
unsafe { &*Self::PTR }
120+
}
121+
}
122+
123+
#feature_attribute
124+
impl core::fmt::Debug for #p_ty {
125+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
126+
f.debug_struct(#name_str).finish()
127+
}
128+
}
129+
});
130+
};
131+
83132
match &p {
84133
Peripheral::Array(p, dim) => {
85134
let mut feature_names = Vec::with_capacity(dim.dim as _);
@@ -97,46 +146,14 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
97146
feature_attribute_n.extend(quote! { #[cfg(feature = #p_feature)] })
98147
};
99148
// Insert the peripherals structure
100-
out.extend(quote! {
101-
#[doc = #description]
102-
#doc_alias
103-
#feature_attribute_n
104-
pub struct #p_ty { _marker: PhantomData<*const ()> }
105-
106-
#feature_attribute_n
107-
unsafe impl Send for #p_ty {}
108-
109-
#feature_attribute_n
110-
impl #p_ty {
111-
///Pointer to the register block
112-
pub const PTR: *const #base::RegisterBlock = #address as *const _;
113-
114-
///Return the pointer to the register block
115-
#[inline(always)]
116-
pub const fn ptr() -> *const #base::RegisterBlock {
117-
Self::PTR
118-
}
119-
120-
#steal_fn
121-
}
122-
123-
#feature_attribute_n
124-
impl Deref for #p_ty {
125-
type Target = #base::RegisterBlock;
126-
127-
#[inline(always)]
128-
fn deref(&self) -> &Self::Target {
129-
unsafe { &*Self::PTR }
130-
}
131-
}
132-
133-
#feature_attribute_n
134-
impl core::fmt::Debug for #p_ty {
135-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
136-
f.debug_struct(#name_str).finish()
137-
}
138-
}
139-
});
149+
per_to_tokens(
150+
&mut out,
151+
&feature_attribute_n,
152+
description,
153+
&p_ty,
154+
doc_alias,
155+
address,
156+
);
140157
}
141158

142159
let feature_any_attribute = quote! {#[cfg(any(#(feature = #feature_names),*))]};
@@ -159,45 +176,14 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
159176
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
160177
};
161178
// Insert the peripheral structure
162-
out.extend(quote! {
163-
#[doc = #description]
164-
#feature_attribute
165-
pub struct #p_ty { _marker: PhantomData<*const ()> }
166-
167-
#feature_attribute
168-
unsafe impl Send for #p_ty {}
169-
170-
#feature_attribute
171-
impl #p_ty {
172-
///Pointer to the register block
173-
pub const PTR: *const #base::RegisterBlock = #address as *const _;
174-
175-
///Return the pointer to the register block
176-
#[inline(always)]
177-
pub const fn ptr() -> *const #base::RegisterBlock {
178-
Self::PTR
179-
}
180-
181-
#steal_fn
182-
}
183-
184-
#feature_attribute
185-
impl Deref for #p_ty {
186-
type Target = #base::RegisterBlock;
187-
188-
#[inline(always)]
189-
fn deref(&self) -> &Self::Target {
190-
unsafe { &*Self::PTR }
191-
}
192-
}
193-
194-
#feature_attribute
195-
impl core::fmt::Debug for #p_ty {
196-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
197-
f.debug_struct(#name_str).finish()
198-
}
199-
}
200-
});
179+
per_to_tokens(
180+
&mut out,
181+
&feature_attribute,
182+
&description,
183+
&p_ty,
184+
None,
185+
address,
186+
);
201187

202188
// Derived peripherals may not require re-implementation, and will instead
203189
// use a single definition of the non-derived version.

0 commit comments

Comments
 (0)