Merge pull request #1356 from embassy-rs/peripheralref-no-mut

Do not require mut in PeripheralRef clone_unchecked, make nRF timer `cc()` borrows less strict.
This commit is contained in:
Dario Nieuwenhuis 2023-04-12 00:03:46 +02:00 committed by GitHub
commit 201a038134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 18 additions and 19 deletions

View File

@ -92,7 +92,7 @@ macro_rules! impl_peripheral {
type P = $type; type P = $type;
#[inline] #[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P { unsafe fn clone_unchecked(&self) -> Self::P {
$type { ..*self } $type { ..*self }
} }
} }

View File

@ -39,7 +39,7 @@ impl<'a, T> PeripheralRef<'a, T> {
/// You should strongly prefer using `reborrow()` instead. It returns a /// You should strongly prefer using `reborrow()` instead. It returns a
/// `PeripheralRef` that borrows `self`, which allows the borrow checker /// `PeripheralRef` that borrows `self`, which allows the borrow checker
/// to enforce this at compile time. /// to enforce this at compile time.
pub unsafe fn clone_unchecked(&mut self) -> PeripheralRef<'a, T> pub unsafe fn clone_unchecked(&self) -> PeripheralRef<'a, T>
where where
T: Peripheral<P = T>, T: Peripheral<P = T>,
{ {
@ -146,14 +146,14 @@ pub trait Peripheral: Sized {
/// ///
/// You should strongly prefer using `into_ref()` instead. It returns a /// You should strongly prefer using `into_ref()` instead. It returns a
/// `PeripheralRef`, which allows the borrow checker to enforce this at compile time. /// `PeripheralRef`, which allows the borrow checker to enforce this at compile time.
unsafe fn clone_unchecked(&mut self) -> Self::P; unsafe fn clone_unchecked(&self) -> Self::P;
/// Convert a value into a `PeripheralRef`. /// Convert a value into a `PeripheralRef`.
/// ///
/// When called on an owned `T`, yields a `PeripheralRef<'static, T>`. /// When called on an owned `T`, yields a `PeripheralRef<'static, T>`.
/// When called on an `&'a mut T`, yields a `PeripheralRef<'a, T>`. /// When called on an `&'a mut T`, yields a `PeripheralRef<'a, T>`.
#[inline] #[inline]
fn into_ref<'a>(mut self) -> PeripheralRef<'a, Self::P> fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
where where
Self: 'a, Self: 'a,
{ {
@ -161,14 +161,14 @@ pub trait Peripheral: Sized {
} }
} }
impl<'b, T: DerefMut> Peripheral for T impl<'b, T: Deref> Peripheral for T
where where
T::Target: Peripheral, T::Target: Peripheral,
{ {
type P = <T::Target as Peripheral>::P; type P = <T::Target as Peripheral>::P;
#[inline] #[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P { unsafe fn clone_unchecked(&self) -> Self::P {
self.deref_mut().clone_unchecked() self.deref().clone_unchecked()
} }
} }

View File

@ -342,7 +342,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
r.enable.write(|w| w.enable().enabled()); r.enable.write(|w| w.enable().enabled());
// Configure byte counter. // Configure byte counter.
let mut timer = Timer::new_counter(timer); let timer = Timer::new_counter(timer);
timer.cc(1).write(rx_buffer.len() as u32 * 2); timer.cc(1).write(rx_buffer.len() as u32 * 2);
timer.cc(1).short_compare_clear(); timer.cc(1).short_compare_clear();
timer.clear(); timer.clear();

View File

@ -315,7 +315,7 @@ impl<'d, const N: usize> Saadc<'d, N> {
Ppi::new_one_to_one(ppi_ch1, Event::from_reg(&r.events_end), Task::from_reg(&r.tasks_start)); Ppi::new_one_to_one(ppi_ch1, Event::from_reg(&r.events_end), Task::from_reg(&r.tasks_start));
start_ppi.enable(); start_ppi.enable();
let mut timer = Timer::new(timer); let timer = Timer::new(timer);
timer.set_frequency(frequency); timer.set_frequency(frequency);
timer.cc(0).write(sample_counter); timer.cc(0).write(sample_counter);
timer.cc(0).short_compare_clear(); timer.cc(0).short_compare_clear();

View File

@ -117,7 +117,7 @@ impl<'d, T: Instance> Timer<'d, T> {
let regs = T::regs(); let regs = T::regs();
let mut this = Self { _p: timer }; let this = Self { _p: timer };
// Stop the timer before doing anything else, // Stop the timer before doing anything else,
// since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification. // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification.
@ -213,13 +213,13 @@ impl<'d, T: Instance> Timer<'d, T> {
/// ///
/// # Panics /// # Panics
/// Panics if `n` >= the number of CC registers this timer has (4 for a normal timer, 6 for an extended timer). /// Panics if `n` >= the number of CC registers this timer has (4 for a normal timer, 6 for an extended timer).
pub fn cc(&mut self, n: usize) -> Cc<T> { pub fn cc(&self, n: usize) -> Cc<'d, T> {
if n >= T::CCS { if n >= T::CCS {
panic!("Cannot get CC register {} of timer with {} CC registers.", n, T::CCS); panic!("Cannot get CC register {} of timer with {} CC registers.", n, T::CCS);
} }
Cc { Cc {
n, n,
_p: self._p.reborrow(), _p: unsafe { self._p.clone_unchecked() },
} }
} }
} }

View File

@ -205,7 +205,7 @@ impl<'d, T: Instance> Uarte<'d, T> {
ppi_ch1: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, ppi_ch1: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd,
ppi_ch2: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd, ppi_ch2: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd,
) -> (UarteTx<'d, T>, UarteRxWithIdle<'d, T, U>) { ) -> (UarteTx<'d, T>, UarteRxWithIdle<'d, T, U>) {
let mut timer = Timer::new(timer); let timer = Timer::new(timer);
into_ref!(ppi_ch1, ppi_ch2); into_ref!(ppi_ch1, ppi_ch2);

View File

@ -153,7 +153,7 @@ impl<'d, T: Instance, V: VbusDetect + 'd> driver::Driver<'d> for Driver<'d, T, V
})) }))
} }
fn start(mut self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) { fn start(self, control_max_packet_size: u16) -> (Self::Bus, Self::ControlPipe) {
( (
Bus { Bus {
_p: unsafe { self._p.clone_unchecked() }, _p: unsafe { self._p.clone_unchecked() },

View File

@ -180,7 +180,7 @@ fn main() {
#[cfg(flash)] #[cfg(flash)]
impl<'d> FlashLayout<'d> { impl<'d> FlashLayout<'d> {
pub(crate) fn new(mut p: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>) -> Self { pub(crate) fn new(p: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>) -> Self {
Self { Self {
#(#inits),* #(#inits),*
} }

View File

@ -33,8 +33,7 @@ impl<'d> Flash<'d> {
} }
pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> { pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> {
let mut flash = self; unsafe { self.inner.clone_unchecked() }
unsafe { flash.inner.clone_unchecked() }
} }
} }

View File

@ -67,7 +67,7 @@ mod alt_regions {
// SAFETY: We never expose the cloned peripheral references, and their instance is not public. // SAFETY: We never expose the cloned peripheral references, and their instance is not public.
// Also, all flash region operations are protected with a cs. // Also, all flash region operations are protected with a cs.
let mut p = self.release(); let p = self.release();
AltFlashLayout { AltFlashLayout {
bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }), bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }),
bank1_region2: Bank1Region2(&BANK1_REGION2, unsafe { p.clone_unchecked() }), bank1_region2: Bank1Region2(&BANK1_REGION2, unsafe { p.clone_unchecked() }),

View File

@ -29,7 +29,7 @@ impl<'d, T: Pin> Flex<'d, T> {
} }
#[inline] #[inline]
pub fn degrade(mut self) -> Flex<'d, AnyPin> { pub fn degrade(self) -> Flex<'d, AnyPin> {
// Safety: We are about to drop the other copy of this pin, so // Safety: We are about to drop the other copy of this pin, so
// this clone is safe. // this clone is safe.
let pin = unsafe { self.pin.clone_unchecked() }; let pin = unsafe { self.pin.clone_unchecked() };