Skip to content

Commit 867cc8c

Browse files
Merge #193
193: Make `Peripherals` non-exhaustive and improve its docs r=therealprof a=jonas-schievink This means that it's no longer a breaking change to add fields to it, which is important since Arm is likely to add more in upcoming architectures. They could also add extensions that add peripherals. Co-authored-by: Jonas Schievink <[email protected]>
2 parents 319773e + d44a5fa commit 867cc8c

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

src/peripheral/mod.rs

+27-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#![allow(clippy::needless_doctest_main)]
2-
//! Core peripherals
1+
//! Core peripherals.
32
//!
43
//! # API
54
//!
@@ -9,59 +8,47 @@
98
//! the [`Peripherals::take`](struct.Peripherals.html#method.take) method.
109
//!
1110
//! ``` no_run
12-
//! use cortex_m::peripheral::Peripherals;
13-
//!
14-
//! fn main() {
15-
//! let mut peripherals = Peripherals::take().unwrap();
16-
//! peripherals.DWT.enable_cycle_counter();
17-
//! }
11+
//! # use cortex_m::peripheral::Peripherals;
12+
//! let mut peripherals = Peripherals::take().unwrap();
13+
//! peripherals.DWT.enable_cycle_counter();
1814
//! ```
1915
//!
2016
//! This method can only be successfully called *once* -- this is why the method returns an
2117
//! `Option`. Subsequent calls to the method will result in a `None` value being returned.
2218
//!
23-
//! ``` no_run
24-
//! use cortex_m::peripheral::Peripherals;
25-
//!
26-
//! fn main() {
27-
//! let ok = Peripherals::take().unwrap();
28-
//! let panics = Peripherals::take().unwrap();
29-
//! }
19+
//! ``` no_run, should_panic
20+
//! # use cortex_m::peripheral::Peripherals;
21+
//! let ok = Peripherals::take().unwrap();
22+
//! let panics = Peripherals::take().unwrap();
3023
//! ```
3124
//! A part of the peripheral API doesn't require access to a peripheral instance. This part of the
3225
//! API is provided as static methods on the peripheral types. One example is the
3326
//! [`DWT::get_cycle_count`](struct.DWT.html#method.get_cycle_count) method.
3427
//!
3528
//! ``` no_run
36-
//! use cortex_m::peripheral::{DWT, Peripherals};
37-
//!
38-
//! fn main() {
39-
//! {
40-
//! let mut peripherals = Peripherals::take().unwrap();
41-
//! peripherals.DWT.enable_cycle_counter();
42-
//! } // all the peripheral singletons are destroyed here
29+
//! # use cortex_m::peripheral::{DWT, Peripherals};
30+
//! {
31+
//! let mut peripherals = Peripherals::take().unwrap();
32+
//! peripherals.DWT.enable_cycle_counter();
33+
//! } // all the peripheral singletons are destroyed here
4334
//!
44-
//! // but this method can be called without a DWT instance
45-
//! let cyccnt = DWT::get_cycle_count();
46-
//! }
35+
//! // but this method can be called without a DWT instance
36+
//! let cyccnt = DWT::get_cycle_count();
4737
//! ```
4838
//!
4939
//! The singleton property can be *unsafely* bypassed using the `ptr` static method which is
5040
//! available on all the peripheral types. This method is a useful building block for implementing
5141
//! safe higher level abstractions.
5242
//!
5343
//! ``` no_run
54-
//! use cortex_m::peripheral::{DWT, Peripherals};
55-
//!
56-
//! fn main() {
57-
//! {
58-
//! let mut peripherals = Peripherals::take().unwrap();
59-
//! peripherals.DWT.enable_cycle_counter();
60-
//! } // all the peripheral singletons are destroyed here
44+
//! # use cortex_m::peripheral::{DWT, Peripherals};
45+
//! {
46+
//! let mut peripherals = Peripherals::take().unwrap();
47+
//! peripherals.DWT.enable_cycle_counter();
48+
//! } // all the peripheral singletons are destroyed here
6149
//!
62-
//! // actually safe because this is an atomic read with no side effects
63-
//! let cyccnt = unsafe { (*DWT::ptr()).cyccnt.read() };
64-
//! }
50+
//! // actually safe because this is an atomic read with no side effects
51+
//! let cyccnt = unsafe { (*DWT::ptr()).cyccnt.read() };
6552
//! ```
6653
//!
6754
//! # References
@@ -138,6 +125,10 @@ pub struct Peripherals {
138125

139126
/// Trace Port Interface Unit (not present on Cortex-M0 variants)
140127
pub TPIU: TPIU,
128+
129+
// Private field making `Peripherals` non-exhaustive. We don't use `#[non_exhaustive]` so we
130+
// can support older Rust versions.
131+
_priv: (),
141132
}
142133

143134
// NOTE `no_mangle` is used here to prevent linking different minor versions of this crate as that
@@ -200,6 +191,7 @@ impl Peripherals {
200191
TPIU: TPIU {
201192
_marker: PhantomData,
202193
},
194+
_priv: (),
203195
}
204196
}
205197
}

0 commit comments

Comments
 (0)