Skip to content

Commit

Permalink
Merge pull request #255 from jonas-schievink/seal
Browse files Browse the repository at this point in the history
🦭 all Instance traits
  • Loading branch information
jonas-schievink authored Oct 26, 2020
2 parents ff6012f + 2795df3 commit 1d6e228
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 18 deletions.
8 changes: 4 additions & 4 deletions nrf-hal-common/src/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -973,7 +973,7 @@ pub enum Error {
BufferTooLong,
}

pub trait Instance: private::Sealed {}
pub trait Instance: Deref<Target = crate::pac::pwm0::RegisterBlock> + sealed::Sealed {}

impl Instance for PWM0 {}

Expand All @@ -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<Target = crate::pac::pwm0::RegisterBlock> {}
mod sealed {
pub trait Sealed {}

impl Sealed for crate::pwm::PWM0 {}

Expand Down
7 changes: 6 additions & 1 deletion nrf-hal-common/src/rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,22 @@ pub enum Error {
}

/// Implemented by all RTC instances.
pub trait Instance: Deref<Target = rtc0::RegisterBlock> {
pub trait Instance: Deref<Target = rtc0::RegisterBlock> + 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 {}
)*
}
}
Expand Down
8 changes: 7 additions & 1 deletion nrf-hal-common/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,14 @@ pub enum Error {
}

/// Trait implemented by all SPI peripheral instances.
pub trait Instance: Deref<Target = spi0::RegisterBlock> {}
pub trait Instance: Deref<Target = spi0::RegisterBlock> + 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 {}
25 changes: 21 additions & 4 deletions nrf-hal-common/src/spim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,13 @@ pub enum Error {
}

/// Implemented by all SPIM instances.
pub trait Instance: Deref<Target = spim0::RegisterBlock> {}
pub trait Instance: Deref<Target = spim0::RegisterBlock> + sealed::Sealed {}

mod sealed {
pub trait Sealed {}
}

impl sealed::Sealed for SPIM0 {}
impl Instance for SPIM0 {}

#[cfg(any(
Expand All @@ -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 {}
}
8 changes: 7 additions & 1 deletion nrf-hal-common/src/twi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ pub enum Error {
}

/// Implemented by all TWIM instances.
pub trait Instance: Deref<Target = twi0::RegisterBlock> {}
pub trait Instance: Deref<Target = twi0::RegisterBlock> + 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 {}
13 changes: 11 additions & 2 deletions nrf-hal-common/src/twim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,18 @@ pub enum Error {
}

/// Implemented by all TWIM instances
pub trait Instance: Deref<Target = twim0::RegisterBlock> {}
pub trait Instance: Deref<Target = twim0::RegisterBlock> + 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 {}
}
13 changes: 11 additions & 2 deletions nrf-hal-common/src/twis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,18 @@ pub enum TwiEvent {
}

/// Implemented by all TWIS instances
pub trait Instance: Deref<Target = twis0::RegisterBlock> {}
pub trait Instance: Deref<Target = twis0::RegisterBlock> + 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 {}
}
7 changes: 6 additions & 1 deletion nrf-hal-common/src/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ pub struct Pins {
pub rts: Option<Pin<Output<PushPull>>>,
}

pub trait Instance: Deref<Target = uart0::RegisterBlock> {}
pub trait Instance: Deref<Target = uart0::RegisterBlock> + sealed::Sealed {}

mod sealed {
pub trait Sealed {}
}

impl sealed::Sealed for UART0 {}
impl Instance for UART0 {}
13 changes: 11 additions & 2 deletions nrf-hal-common/src/uarte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,18 @@ pub enum Error {
BufferNotInRAM,
}

pub trait Instance: Deref<Target = uarte0::RegisterBlock> {}
pub trait Instance: Deref<Target = uarte0::RegisterBlock> + 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 {}
}

0 comments on commit 1d6e228

Please sign in to comment.