From 27b5ec690d9c3a1a71e9a9f890f07c87088f57b1 Mon Sep 17 00:00:00 2001 From: 9names <60134748+9names@users.noreply.github.com> Date: Mon, 20 Feb 2023 22:40:32 +1100 Subject: [PATCH] Convert serial traits from unsafe to sealed --- src/i2c.rs | 2 +- src/serial.rs | 60 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/i2c.rs b/src/i2c.rs index 9868155..1eb5b25 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -57,7 +57,7 @@ impl embedded_hal::i2c::Error for Error { } /// SDA pins -pub trait SdaPin : Sealed {} +pub trait SdaPin: Sealed {} /// SCL pins pub trait SclPin: Sealed {} diff --git a/src/serial.rs b/src/serial.rs index a13f04c..8e9ce51 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -1,4 +1,5 @@ //! Serial communication +use self::private::Sealed; use crate::clock::Clocks; use crate::pac; use core::fmt; @@ -349,27 +350,27 @@ where } /// Serial transmit pins - DO NOT IMPLEMENT THIS TRAIT -pub unsafe trait TxPin {} +pub trait TxPin: Sealed {} /// Serial receive pins - DO NOT IMPLEMENT THIS TRAIT -pub unsafe trait RxPin {} +pub trait RxPin: Sealed {} /// Serial rts pins - DO NOT IMPLEMENT THIS TRAIT -pub unsafe trait RtsPin {} +pub trait RtsPin: Sealed {} /// Serial cts pins - DO NOT IMPLEMENT THIS TRAIT -pub unsafe trait CtsPin {} +pub trait CtsPin: Sealed {} macro_rules! impl_uart_pin { ($(($UartSigi: ident, $UartMuxi: ident),)+) => { use crate::gpio::*; $( - unsafe impl> TxPin for (PIN, $UartMuxi) {} - unsafe impl> RxPin for (PIN, $UartMuxi) {} - unsafe impl> RtsPin for (PIN, $UartMuxi) {} - unsafe impl> CtsPin for (PIN, $UartMuxi) {} - - unsafe impl> TxPin for (PIN, $UartMuxi) {} - unsafe impl> RxPin for (PIN, $UartMuxi) {} - unsafe impl> RtsPin for (PIN, $UartMuxi) {} - unsafe impl> CtsPin for (PIN, $UartMuxi) {} + impl> TxPin for (PIN, $UartMuxi) {} + impl> RxPin for (PIN, $UartMuxi) {} + impl> RtsPin for (PIN, $UartMuxi) {} + impl> CtsPin for (PIN, $UartMuxi) {} + + impl> TxPin for (PIN, $UartMuxi) {} + impl> RxPin for (PIN, $UartMuxi) {} + impl> RtsPin for (PIN, $UartMuxi) {} + impl> CtsPin for (PIN, $UartMuxi) {} )+ }; } @@ -416,3 +417,36 @@ where const HAS_RTS: bool = true; const HAS_CTS: bool = true; } + +// Prevent users from implementing the Serial pin traits +mod private { + use crate::gpio; + + pub trait Sealed {} + impl Sealed for (TX, RX) {} + impl Sealed for (TX, RX, RTS, CTS) {} + + impl Sealed for gpio::Pin0 {} + impl Sealed for gpio::Pin1 {} + impl Sealed for gpio::Pin2 {} + impl Sealed for gpio::Pin3 {} + impl Sealed for gpio::Pin4 {} + impl Sealed for gpio::Pin5 {} + impl Sealed for gpio::Pin6 {} + impl Sealed for gpio::Pin7 {} + impl Sealed for gpio::Pin8 {} + impl Sealed for gpio::Pin9 {} + impl Sealed for gpio::Pin10 {} + impl Sealed for gpio::Pin11 {} + impl Sealed for gpio::Pin12 {} + impl Sealed for gpio::Pin13 {} + impl Sealed for gpio::Pin14 {} + impl Sealed for gpio::Pin15 {} + impl Sealed for gpio::Pin16 {} + impl Sealed for gpio::Pin17 {} + impl Sealed for gpio::Pin18 {} + impl Sealed for gpio::Pin19 {} + impl Sealed for gpio::Pin20 {} + impl Sealed for gpio::Pin21 {} + impl Sealed for gpio::Pin22 {} +}