aboutsummaryrefslogtreecommitdiff
path: root/embassy-hal-internal
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-05-07 23:21:55 +0200
committerDario Nieuwenhuis <[email protected]>2024-05-07 23:26:15 +0200
commitb13ad7e80bed802468aac2d876d372c1bcde565c (patch)
tree32db403b46fc962d87de6bc599213899953086d1 /embassy-hal-internal
parent0f11fecff6ea5790aa69270c8e5e77fe9d09ea3b (diff)
Fix PeripheralRef soundness issue allowing &T.
Fix soundness issue introduced in a previous soundness fix https://github.com/embassy-rs/embassy/pull/2602 . PeripheralRef must not implement DerefMut itself, but the blanket impl must still require DerefMut. Otherwise you can create two instances of a driver on the same uart by using `&my_uart`.
Diffstat (limited to 'embassy-hal-internal')
-rw-r--r--embassy-hal-internal/src/peripheral.rs15
1 files changed, 12 insertions, 3 deletions
diff --git a/embassy-hal-internal/src/peripheral.rs b/embassy-hal-internal/src/peripheral.rs
index f03f41507..0b0f13338 100644
--- a/embassy-hal-internal/src/peripheral.rs
+++ b/embassy-hal-internal/src/peripheral.rs
@@ -1,5 +1,5 @@
1use core::marker::PhantomData; 1use core::marker::PhantomData;
2use core::ops::Deref; 2use core::ops::{Deref, DerefMut};
3 3
4/// An exclusive reference to a peripheral. 4/// An exclusive reference to a peripheral.
5/// 5///
@@ -155,7 +155,7 @@ pub trait Peripheral: Sized {
155 } 155 }
156} 156}
157 157
158impl<'b, T: Deref> Peripheral for T 158impl<'b, T: DerefMut> Peripheral for T
159where 159where
160 T::Target: Peripheral, 160 T::Target: Peripheral,
161{ 161{
@@ -163,6 +163,15 @@ where
163 163
164 #[inline] 164 #[inline]
165 unsafe fn clone_unchecked(&self) -> Self::P { 165 unsafe fn clone_unchecked(&self) -> Self::P {
166 self.deref().clone_unchecked() 166 T::Target::clone_unchecked(self)
167 }
168}
169
170impl<'b, T: Peripheral> Peripheral for PeripheralRef<'_, T> {
171 type P = T::P;
172
173 #[inline]
174 unsafe fn clone_unchecked(&self) -> Self::P {
175 T::clone_unchecked(self)
167 } 176 }
168} 177}