From 34f47ddba972e5f697392d30a6c302e93bb0502c Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 16 Jan 2020 22:17:10 +0100 Subject: [PATCH 1/3] Clean up the doc examples in `peripheral` The Clippy lint was unnecessarily `#[allow]`ed. --- src/peripheral/mod.rs | 57 +++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/src/peripheral/mod.rs b/src/peripheral/mod.rs index 8854830a..5d7e4736 100644 --- a/src/peripheral/mod.rs +++ b/src/peripheral/mod.rs @@ -1,5 +1,4 @@ -#![allow(clippy::needless_doctest_main)] -//! Core peripherals +//! Core peripherals. //! //! # API //! @@ -9,41 +8,32 @@ //! the [`Peripherals::take`](struct.Peripherals.html#method.take) method. //! //! ``` no_run -//! use cortex_m::peripheral::Peripherals; -//! -//! fn main() { -//! let mut peripherals = Peripherals::take().unwrap(); -//! peripherals.DWT.enable_cycle_counter(); -//! } +//! # use cortex_m::peripheral::Peripherals; +//! let mut peripherals = Peripherals::take().unwrap(); +//! peripherals.DWT.enable_cycle_counter(); //! ``` //! //! This method can only be successfully called *once* -- this is why the method returns an //! `Option`. Subsequent calls to the method will result in a `None` value being returned. //! -//! ``` no_run -//! use cortex_m::peripheral::Peripherals; -//! -//! fn main() { -//! let ok = Peripherals::take().unwrap(); -//! let panics = Peripherals::take().unwrap(); -//! } +//! ``` no_run, should_panic +//! # use cortex_m::peripheral::Peripherals; +//! let ok = Peripherals::take().unwrap(); +//! let panics = Peripherals::take().unwrap(); //! ``` //! A part of the peripheral API doesn't require access to a peripheral instance. This part of the //! API is provided as static methods on the peripheral types. One example is the //! [`DWT::get_cycle_count`](struct.DWT.html#method.get_cycle_count) method. //! //! ``` no_run -//! use cortex_m::peripheral::{DWT, Peripherals}; -//! -//! fn main() { -//! { -//! let mut peripherals = Peripherals::take().unwrap(); -//! peripherals.DWT.enable_cycle_counter(); -//! } // all the peripheral singletons are destroyed here +//! # use cortex_m::peripheral::{DWT, Peripherals}; +//! { +//! let mut peripherals = Peripherals::take().unwrap(); +//! peripherals.DWT.enable_cycle_counter(); +//! } // all the peripheral singletons are destroyed here //! -//! // but this method can be called without a DWT instance -//! let cyccnt = DWT::get_cycle_count(); -//! } +//! // but this method can be called without a DWT instance +//! let cyccnt = DWT::get_cycle_count(); //! ``` //! //! The singleton property can be *unsafely* bypassed using the `ptr` static method which is @@ -51,17 +41,14 @@ //! safe higher level abstractions. //! //! ``` no_run -//! use cortex_m::peripheral::{DWT, Peripherals}; -//! -//! fn main() { -//! { -//! let mut peripherals = Peripherals::take().unwrap(); -//! peripherals.DWT.enable_cycle_counter(); -//! } // all the peripheral singletons are destroyed here +//! # use cortex_m::peripheral::{DWT, Peripherals}; +//! { +//! let mut peripherals = Peripherals::take().unwrap(); +//! peripherals.DWT.enable_cycle_counter(); +//! } // all the peripheral singletons are destroyed here //! -//! // actually safe because this is an atomic read with no side effects -//! let cyccnt = unsafe { (*DWT::ptr()).cyccnt.read() }; -//! } +//! // actually safe because this is an atomic read with no side effects +//! let cyccnt = unsafe { (*DWT::ptr()).cyccnt.read() }; //! ``` //! //! # References From 6e63b1e9b94bc08b8cd84383c51b93f6170eb594 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 16 Jan 2020 22:17:37 +0100 Subject: [PATCH 2/3] Make `Peripherals` `#[non_exhaustive]` --- src/peripheral/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/peripheral/mod.rs b/src/peripheral/mod.rs index 5d7e4736..c9c8ad6e 100644 --- a/src/peripheral/mod.rs +++ b/src/peripheral/mod.rs @@ -89,6 +89,7 @@ mod test; /// Core peripherals #[allow(non_snake_case)] +#[non_exhaustive] pub struct Peripherals { /// Cache and branch predictor maintenance operations (not present on Cortex-M0 variants) pub CBP: CBP, From d44a5fa86a75a97acef5321d2e061a914cf8d871 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 16 Jan 2020 23:22:44 +0100 Subject: [PATCH 3/3] Use a private field instead of `#[non_exhaustive]` --- src/peripheral/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/peripheral/mod.rs b/src/peripheral/mod.rs index c9c8ad6e..c84003f0 100644 --- a/src/peripheral/mod.rs +++ b/src/peripheral/mod.rs @@ -89,7 +89,6 @@ mod test; /// Core peripherals #[allow(non_snake_case)] -#[non_exhaustive] pub struct Peripherals { /// Cache and branch predictor maintenance operations (not present on Cortex-M0 variants) pub CBP: CBP, @@ -126,6 +125,10 @@ pub struct Peripherals { /// Trace Port Interface Unit (not present on Cortex-M0 variants) pub TPIU: TPIU, + + // Private field making `Peripherals` non-exhaustive. We don't use `#[non_exhaustive]` so we + // can support older Rust versions. + _priv: (), } // NOTE `no_mangle` is used here to prevent linking different minor versions of this crate as that @@ -188,6 +191,7 @@ impl Peripherals { TPIU: TPIU { _marker: PhantomData, }, + _priv: (), } } }