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:
		@@ -92,7 +92,7 @@ macro_rules! impl_peripheral {
 | 
			
		||||
            type P = $type;
 | 
			
		||||
 | 
			
		||||
            #[inline]
 | 
			
		||||
            unsafe fn clone_unchecked(&mut self) -> Self::P {
 | 
			
		||||
            unsafe fn clone_unchecked(&self) -> Self::P {
 | 
			
		||||
                $type { ..*self }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -39,7 +39,7 @@ impl<'a, T> PeripheralRef<'a, T> {
 | 
			
		||||
    /// You should strongly prefer using `reborrow()` instead. It returns a
 | 
			
		||||
    /// `PeripheralRef` that borrows `self`, which allows the borrow checker
 | 
			
		||||
    /// 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
 | 
			
		||||
        T: Peripheral<P = T>,
 | 
			
		||||
    {
 | 
			
		||||
@@ -146,14 +146,14 @@ pub trait Peripheral: Sized {
 | 
			
		||||
    ///
 | 
			
		||||
    /// You should strongly prefer using `into_ref()` instead. It returns a
 | 
			
		||||
    /// `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`.
 | 
			
		||||
    ///
 | 
			
		||||
    /// When called on an owned `T`, yields a `PeripheralRef<'static, T>`.
 | 
			
		||||
    /// When called on an `&'a mut T`, yields a `PeripheralRef<'a, T>`.
 | 
			
		||||
    #[inline]
 | 
			
		||||
    fn into_ref<'a>(mut self) -> PeripheralRef<'a, Self::P>
 | 
			
		||||
    fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
 | 
			
		||||
    where
 | 
			
		||||
        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
 | 
			
		||||
    T::Target: Peripheral,
 | 
			
		||||
{
 | 
			
		||||
    type P = <T::Target as Peripheral>::P;
 | 
			
		||||
 | 
			
		||||
    #[inline]
 | 
			
		||||
    unsafe fn clone_unchecked(&mut self) -> Self::P {
 | 
			
		||||
        self.deref_mut().clone_unchecked()
 | 
			
		||||
    unsafe fn clone_unchecked(&self) -> Self::P {
 | 
			
		||||
        self.deref().clone_unchecked()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -342,7 +342,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> {
 | 
			
		||||
        r.enable.write(|w| w.enable().enabled());
 | 
			
		||||
 | 
			
		||||
        // 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).short_compare_clear();
 | 
			
		||||
        timer.clear();
 | 
			
		||||
 
 | 
			
		||||
@@ -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));
 | 
			
		||||
        start_ppi.enable();
 | 
			
		||||
 | 
			
		||||
        let mut timer = Timer::new(timer);
 | 
			
		||||
        let timer = Timer::new(timer);
 | 
			
		||||
        timer.set_frequency(frequency);
 | 
			
		||||
        timer.cc(0).write(sample_counter);
 | 
			
		||||
        timer.cc(0).short_compare_clear();
 | 
			
		||||
 
 | 
			
		||||
@@ -117,7 +117,7 @@ impl<'d, T: Instance> Timer<'d, T> {
 | 
			
		||||
 | 
			
		||||
        let regs = T::regs();
 | 
			
		||||
 | 
			
		||||
        let mut this = Self { _p: timer };
 | 
			
		||||
        let this = Self { _p: timer };
 | 
			
		||||
 | 
			
		||||
        // Stop the timer before doing anything else,
 | 
			
		||||
        // 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 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 {
 | 
			
		||||
            panic!("Cannot get CC register {} of timer with {} CC registers.", n, T::CCS);
 | 
			
		||||
        }
 | 
			
		||||
        Cc {
 | 
			
		||||
            n,
 | 
			
		||||
            _p: self._p.reborrow(),
 | 
			
		||||
            _p: unsafe { self._p.clone_unchecked() },
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -205,7 +205,7 @@ impl<'d, T: Instance> Uarte<'d, T> {
 | 
			
		||||
        ppi_ch1: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd,
 | 
			
		||||
        ppi_ch2: impl Peripheral<P = impl ConfigurableChannel + 'd> + 'd,
 | 
			
		||||
    ) -> (UarteTx<'d, T>, UarteRxWithIdle<'d, T, U>) {
 | 
			
		||||
        let mut timer = Timer::new(timer);
 | 
			
		||||
        let timer = Timer::new(timer);
 | 
			
		||||
 | 
			
		||||
        into_ref!(ppi_ch1, ppi_ch2);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
                _p: unsafe { self._p.clone_unchecked() },
 | 
			
		||||
 
 | 
			
		||||
@@ -180,7 +180,7 @@ fn main() {
 | 
			
		||||
 | 
			
		||||
        #[cfg(flash)]
 | 
			
		||||
        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 {
 | 
			
		||||
                    #(#inits),*
 | 
			
		||||
                }
 | 
			
		||||
 
 | 
			
		||||
@@ -33,8 +33,7 @@ impl<'d> Flash<'d> {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> {
 | 
			
		||||
        let mut flash = self;
 | 
			
		||||
        unsafe { flash.inner.clone_unchecked() }
 | 
			
		||||
        unsafe { self.inner.clone_unchecked() }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -67,7 +67,7 @@ mod alt_regions {
 | 
			
		||||
 | 
			
		||||
            // SAFETY: We never expose the cloned peripheral references, and their instance is not public.
 | 
			
		||||
            // Also, all flash region operations are protected with a cs.
 | 
			
		||||
            let mut p = self.release();
 | 
			
		||||
            let p = self.release();
 | 
			
		||||
            AltFlashLayout {
 | 
			
		||||
                bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }),
 | 
			
		||||
                bank1_region2: Bank1Region2(&BANK1_REGION2, unsafe { p.clone_unchecked() }),
 | 
			
		||||
 
 | 
			
		||||
@@ -29,7 +29,7 @@ impl<'d, T: Pin> Flex<'d, T> {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[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
 | 
			
		||||
        // this clone is safe.
 | 
			
		||||
        let pin = unsafe { self.pin.clone_unchecked() };
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user