From d206fd02172d73c630a8cb685d131f459275e6fe Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Mon, 25 Mar 2024 11:38:20 -0700 Subject: [PATCH 1/2] Seal all `Instance` traits --- esp-hal/src/i2c.rs | 2 +- esp-hal/src/spi/master.rs | 2 +- esp-hal/src/spi/slave.rs | 2 +- esp-hal/src/timer.rs | 7 ++++++- esp-hal/src/trace.rs | 2 +- esp-hal/src/twai/mod.rs | 2 +- esp-hal/src/uart.rs | 2 +- esp-hal/src/usb_serial_jtag.rs | 2 +- 8 files changed, 13 insertions(+), 8 deletions(-) diff --git a/esp-hal/src/i2c.rs b/esp-hal/src/i2c.rs index f23f029b8f9..a7e9652413d 100644 --- a/esp-hal/src/i2c.rs +++ b/esp-hal/src/i2c.rs @@ -645,7 +645,7 @@ mod asynch { } /// I2C Peripheral Instance -pub trait Instance { +pub trait Instance: crate::private::Sealed { fn scl_output_signal(&self) -> OutputSignal; fn scl_input_signal(&self) -> InputSignal; fn sda_output_signal(&self) -> OutputSignal; diff --git a/esp-hal/src/spi/master.rs b/esp-hal/src/spi/master.rs index 7516ad15655..8b56e98fe77 100644 --- a/esp-hal/src/spi/master.rs +++ b/esp-hal/src/spi/master.rs @@ -1988,7 +1988,7 @@ pub trait ExtendedInstance: Instance { fn sio3_input_signal(&self) -> InputSignal; } -pub trait Instance { +pub trait Instance: crate::private::Sealed { fn register_block(&self) -> &RegisterBlock; fn sclk_signal(&self) -> OutputSignal; diff --git a/esp-hal/src/spi/slave.rs b/esp-hal/src/spi/slave.rs index 895ea7e360a..076f531baf4 100644 --- a/esp-hal/src/spi/slave.rs +++ b/esp-hal/src/spi/slave.rs @@ -701,7 +701,7 @@ where { } -pub trait Instance { +pub trait Instance: crate::private::Sealed { fn register_block(&self) -> &RegisterBlock; fn sclk_signal(&self) -> InputSignal; diff --git a/esp-hal/src/timer.rs b/esp-hal/src/timer.rs index a2a492779b3..81a3abf95df 100644 --- a/esp-hal/src/timer.rs +++ b/esp-hal/src/timer.rs @@ -267,7 +267,7 @@ where } /// Timer peripheral instance -pub trait Instance { +pub trait Instance: crate::private::Sealed { fn reset_counter(&mut self); fn set_counter_active(&mut self, state: bool); @@ -305,6 +305,8 @@ pub struct Timer0 { phantom: PhantomData, } +impl crate::private::Sealed for Timer0 {} + impl Timer0 where TG: TimerGroupInstance, @@ -474,6 +476,9 @@ pub struct Timer1 { phantom: PhantomData, } +#[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))] +impl crate::private::Sealed for Timer1 {} + #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))] impl Timer1 where diff --git a/esp-hal/src/trace.rs b/esp-hal/src/trace.rs index 75964f941fc..a8f8f349ceb 100644 --- a/esp-hal/src/trace.rs +++ b/esp-hal/src/trace.rs @@ -198,7 +198,7 @@ where } } -pub trait Instance { +pub trait Instance: crate::private::Sealed { fn register_block(&self) -> &RegisterBlock; } diff --git a/esp-hal/src/twai/mod.rs b/esp-hal/src/twai/mod.rs index 46ccb1ba506..81813fec8d5 100644 --- a/esp-hal/src/twai/mod.rs +++ b/esp-hal/src/twai/mod.rs @@ -641,7 +641,7 @@ where } } -pub trait Instance { +pub trait Instance: crate::private::Sealed { const SYSTEM_PERIPHERAL: system::Peripheral; const NUMBER: usize; diff --git a/esp-hal/src/uart.rs b/esp-hal/src/uart.rs index 70e191a0825..576f57ebf76 100644 --- a/esp-hal/src/uart.rs +++ b/esp-hal/src/uart.rs @@ -1053,7 +1053,7 @@ where } /// UART peripheral instance -pub trait Instance { +pub trait Instance: crate::private::Sealed { fn register_block() -> &'static RegisterBlock; fn uart_number() -> usize; fn interrupt() -> Interrupt; diff --git a/esp-hal/src/usb_serial_jtag.rs b/esp-hal/src/usb_serial_jtag.rs index decee9818f1..173756e621b 100644 --- a/esp-hal/src/usb_serial_jtag.rs +++ b/esp-hal/src/usb_serial_jtag.rs @@ -285,7 +285,7 @@ impl<'d> UsbSerialJtag<'d> { } /// USB Serial JTAG peripheral instance -pub trait Instance { +pub trait Instance: crate::private::Sealed { fn register_block() -> &'static RegisterBlock; fn disable_tx_interrupts() { From 55b51a92af78df08d22d13d784d5fe6268b8bee7 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Mon, 25 Mar 2024 19:58:52 -0700 Subject: [PATCH 2/2] Update `CHANGELOG.md` --- esp-hal/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 0157b303b7f..b43b41c742a 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Enable `embedded-hal` feature by default, instead of the `embedded-hal-02` feature (#1313) - `Uart` structs now take a `Mode` parameter which defines how the driver is initialized (#1294) - `Rmt` can be created in async or blocking mode. The blocking constructor takes an optional interrupt handler argument. (#1341) +- All `Instance` traits are now sealed, and can no longer be implemented for arbitrary types (#1346) ### Removed