From 2795df3a37ff157ec1d2f11edcd96f505b311e2b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 26 Oct 2020 17:55:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=AD=20=20all=20Instance=20traits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nrf-hal-common/src/pwm.rs | 8 ++++---- nrf-hal-common/src/rtc.rs | 7 ++++++- nrf-hal-common/src/spi.rs | 8 +++++++- nrf-hal-common/src/spim.rs | 25 +++++++++++++++++++++---- nrf-hal-common/src/twi.rs | 8 +++++++- nrf-hal-common/src/twim.rs | 13 +++++++++++-- nrf-hal-common/src/twis.rs | 13 +++++++++++-- nrf-hal-common/src/uart.rs | 7 ++++++- nrf-hal-common/src/uarte.rs | 13 +++++++++++-- 9 files changed, 84 insertions(+), 18 deletions(-) diff --git a/nrf-hal-common/src/pwm.rs b/nrf-hal-common/src/pwm.rs index e49535cb..b07664c7 100644 --- a/nrf-hal-common/src/pwm.rs +++ b/nrf-hal-common/src/pwm.rs @@ -2,8 +2,8 @@ //! //! The pulse with modulation (PWM) module enables the generation of pulse width modulated signals on GPIO. -use core::cell::RefCell; use core::sync::atomic::{compiler_fence, Ordering}; +use core::{cell::RefCell, ops::Deref}; #[cfg(any(feature = "52833", feature = "52840"))] use crate::{ @@ -973,7 +973,7 @@ pub enum Error { BufferTooLong, } -pub trait Instance: private::Sealed {} +pub trait Instance: Deref + sealed::Sealed {} impl Instance for PWM0 {} @@ -986,8 +986,8 @@ impl Instance for PWM2 {} #[cfg(not(any(feature = "52810", feature = "52811", feature = "52832")))] impl Instance for PWM3 {} -mod private { - pub trait Sealed: core::ops::Deref {} +mod sealed { + pub trait Sealed {} impl Sealed for crate::pwm::PWM0 {} diff --git a/nrf-hal-common/src/rtc.rs b/nrf-hal-common/src/rtc.rs index a9096caa..42e64a36 100644 --- a/nrf-hal-common/src/rtc.rs +++ b/nrf-hal-common/src/rtc.rs @@ -219,17 +219,22 @@ pub enum Error { } /// Implemented by all RTC instances. -pub trait Instance: Deref { +pub trait Instance: Deref + sealed::Sealed { /// The interrupt associated with this RTC instance. const INTERRUPT: Interrupt; } +mod sealed { + pub trait Sealed {} +} + macro_rules! impl_instance { ($($name:ident,)*) => { $( impl Instance for $name { const INTERRUPT: Interrupt = Interrupt::$name; } + impl sealed::Sealed for $name {} )* } } diff --git a/nrf-hal-common/src/spi.rs b/nrf-hal-common/src/spi.rs index cc2bb568..d38da393 100644 --- a/nrf-hal-common/src/spi.rs +++ b/nrf-hal-common/src/spi.rs @@ -128,8 +128,14 @@ pub enum Error { } /// Trait implemented by all SPI peripheral instances. -pub trait Instance: Deref {} +pub trait Instance: Deref + sealed::Sealed {} +mod sealed { + pub trait Sealed {} +} + +impl sealed::Sealed for SPI0 {} impl Instance for SPI0 {} +impl sealed::Sealed for SPI1 {} impl Instance for SPI1 {} diff --git a/nrf-hal-common/src/spim.rs b/nrf-hal-common/src/spim.rs index a066ea8d..7c535c51 100644 --- a/nrf-hal-common/src/spim.rs +++ b/nrf-hal-common/src/spim.rs @@ -394,8 +394,13 @@ pub enum Error { } /// Implemented by all SPIM instances. -pub trait Instance: Deref {} +pub trait Instance: Deref + sealed::Sealed {} +mod sealed { + pub trait Sealed {} +} + +impl sealed::Sealed for SPIM0 {} impl Instance for SPIM0 {} #[cfg(any( @@ -404,10 +409,22 @@ impl Instance for SPIM0 {} feature = "52840", feature = "52811" ))] -impl Instance for SPIM1 {} +mod _spim1 { + use super::*; + impl Instance for SPIM1 {} + impl sealed::Sealed for SPIM1 {} +} #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] -impl Instance for SPIM2 {} +mod _spim2 { + use super::*; + impl Instance for SPIM2 {} + impl sealed::Sealed for SPIM2 {} +} #[cfg(any(feature = "52833", feature = "52840"))] -impl Instance for SPIM3 {} +mod _spim3 { + use super::*; + impl Instance for SPIM3 {} + impl sealed::Sealed for SPIM3 {} +} diff --git a/nrf-hal-common/src/twi.rs b/nrf-hal-common/src/twi.rs index f61b1f5f..9afe8aad 100644 --- a/nrf-hal-common/src/twi.rs +++ b/nrf-hal-common/src/twi.rs @@ -296,8 +296,14 @@ pub enum Error { } /// Implemented by all TWIM instances. -pub trait Instance: Deref {} +pub trait Instance: Deref + sealed::Sealed {} +mod sealed { + pub trait Sealed {} +} + +impl sealed::Sealed for TWI0 {} impl Instance for TWI0 {} +impl sealed::Sealed for TWI1 {} impl Instance for TWI1 {} diff --git a/nrf-hal-common/src/twim.rs b/nrf-hal-common/src/twim.rs index 15347d77..fa56dab2 100644 --- a/nrf-hal-common/src/twim.rs +++ b/nrf-hal-common/src/twim.rs @@ -447,9 +447,18 @@ pub enum Error { } /// Implemented by all TWIM instances -pub trait Instance: Deref {} +pub trait Instance: Deref + sealed::Sealed {} +mod sealed { + pub trait Sealed {} +} + +impl sealed::Sealed for TWIM0 {} impl Instance for TWIM0 {} #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] -impl Instance for TWIM1 {} +mod _twim1 { + use super::*; + impl sealed::Sealed for TWIM1 {} + impl Instance for TWIM1 {} +} diff --git a/nrf-hal-common/src/twis.rs b/nrf-hal-common/src/twis.rs index de3151ec..73817f4a 100644 --- a/nrf-hal-common/src/twis.rs +++ b/nrf-hal-common/src/twis.rs @@ -314,9 +314,18 @@ pub enum TwiEvent { } /// Implemented by all TWIS instances -pub trait Instance: Deref {} +pub trait Instance: Deref + sealed::Sealed {} +mod sealed { + pub trait Sealed {} +} + +impl sealed::Sealed for TWIS0 {} impl Instance for TWIS0 {} #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] -impl Instance for TWIS1 {} +mod _twis1 { + use super::*; + impl sealed::Sealed for TWIS1 {} + impl Instance for TWIS1 {} +} diff --git a/nrf-hal-common/src/uart.rs b/nrf-hal-common/src/uart.rs index b1fea4f7..fd4355a9 100644 --- a/nrf-hal-common/src/uart.rs +++ b/nrf-hal-common/src/uart.rs @@ -142,6 +142,11 @@ pub struct Pins { pub rts: Option>>, } -pub trait Instance: Deref {} +pub trait Instance: Deref + sealed::Sealed {} +mod sealed { + pub trait Sealed {} +} + +impl sealed::Sealed for UART0 {} impl Instance for UART0 {} diff --git a/nrf-hal-common/src/uarte.rs b/nrf-hal-common/src/uarte.rs index 46d09ce6..6bc805ba 100644 --- a/nrf-hal-common/src/uarte.rs +++ b/nrf-hal-common/src/uarte.rs @@ -368,9 +368,18 @@ pub enum Error { BufferNotInRAM, } -pub trait Instance: Deref {} +pub trait Instance: Deref + sealed::Sealed {} +mod sealed { + pub trait Sealed {} +} + +impl sealed::Sealed for UARTE0 {} impl Instance for UARTE0 {} #[cfg(any(feature = "52833", feature = "52840", feature = "9160"))] -impl Instance for UARTE1 {} +mod _uarte1 { + use super::*; + impl sealed::Sealed for UARTE1 {} + impl Instance for UARTE1 {} +}