Skip to content

Commit cd4ef26

Browse files
committed
quote common steal fn part
1 parent e240fcc commit cd4ef26

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/generate/peripheral.rs

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
5353
feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] });
5454
};
5555

56+
let steal_fn = quote! {
57+
/// Steal an instance of this peripheral
58+
///
59+
/// # Safety
60+
///
61+
/// Ensure that the new instance of the peripheral cannot be used in a way
62+
/// that may race with any existing instances, for example by only
63+
/// accessing read-only or write-only registers, or by consuming the
64+
/// original peripheral and using critical sections to coordinate
65+
/// access between multiple new instances.
66+
///
67+
/// Additionally, other software such as HALs may rely on only one
68+
/// peripheral instance existing to ensure memory safety; ensure
69+
/// no stolen instances are passed to such software.
70+
pub unsafe fn steal() -> Self {
71+
Self { _marker: PhantomData }
72+
}
73+
};
74+
5675
match &p {
5776
Peripheral::Array(p, dim) => {
5877
let names: Vec<Cow<str>> = names(p, dim).map(|n| n.into()).collect();
@@ -92,22 +111,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
92111
Self::PTR
93112
}
94113

95-
///Steal an instance of this peripheral
96-
///
97-
///# Safety
98-
///
99-
/// Ensure that the new instance of the peripheral cannot be used in a way
100-
/// that may race with any existing instances, for example by only
101-
/// accessing read-only or write-only registers, or by consuming the
102-
/// original peripheral and using critical sections to coordinate
103-
/// access between multiple new instances.
104-
///
105-
/// Additionally, other software such as HALs may rely on only one
106-
/// peripheral instance existing to ensure memory safety; ensure
107-
/// no stolen instances are passed to such software.
108-
pub unsafe fn steal() -> Self {
109-
Self { _marker: PhantomData }
110-
}
114+
#steal_fn
111115
}
112116

113117
#feature_attribute_n
@@ -168,22 +172,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
168172
Self::PTR
169173
}
170174

171-
///Steal an instance of this peripheral
172-
///
173-
///# Safety
174-
///
175-
/// Ensure that the new instance of the peripheral cannot be used in a way
176-
/// that may race with any existing instances, for example by only
177-
/// accessing read-only or write-only registers, or by consuming the
178-
/// original peripheral and using critical sections to coordinate
179-
/// access between multiple new instances.
180-
///
181-
/// Additionally, other software such as HALs may rely on only one
182-
/// peripheral instance existing to ensure memory safety; ensure
183-
/// no stolen instances are passed to such software.
184-
pub unsafe fn steal() -> Self {
185-
Self { _marker: PhantomData }
186-
}
175+
#steal_fn
187176
}
188177

189178
#feature_attribute

0 commit comments

Comments
 (0)