pub struct PeripheralRef<'a, T> { /* private fields */ }
Expand description
An exclusive reference to a peripheral.
This is functionally the same as a &'a mut T
. There’s a few advantages in
having a dedicated struct instead:
- Memory efficiency: Peripheral singletons are typically either zero-sized
(for concrete peripherals like
GpioPin<5>
orSPI2
) or very small (for exampleAnyPin
, which is 1 byte). However&mut T
is always 4 bytes for 32-bit targets, even if T is zero-sized. PeripheralRef stores a copy ofT
instead, so it’s the same size. - Code size efficiency. If the user uses the same driver with both
SPI2
and&mut SPI2
, the driver code would be monomorphized two times. With PeripheralRef, the driver is generic over a lifetime only.SPI2
becomesPeripheralRef<'static, SPI2>
, and&mut SPI2
becomesPeripheralRef<'a, SPI2>
. Lifetimes don’t cause monomorphization.
Implementations§
Source§impl<'a, T> PeripheralRef<'a, T>
impl<'a, T> PeripheralRef<'a, T>
Sourcepub unsafe fn clone_unchecked(&self) -> PeripheralRef<'a, T>where
T: Peripheral<P = T>,
pub unsafe fn clone_unchecked(&self) -> PeripheralRef<'a, T>where
T: Peripheral<P = T>,
Unsafely clone (duplicate) a peripheral singleton.
§Safety
This returns an owned clone of the peripheral. You must manually ensure
only one copy of the peripheral is in use at a time. For example, don’t
create two SPI drivers on SPI1
, because they will “fight” each other.
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.
Sourcepub fn reborrow(&mut self) -> PeripheralRef<'_, T>where
T: Peripheral<P = T>,
pub fn reborrow(&mut self) -> PeripheralRef<'_, T>where
T: Peripheral<P = T>,
Reborrow into a “child” PeripheralRef.
self
will stay borrowed until the child PeripheralRef is dropped.
Sourcepub fn map<U>(self, transform: impl FnOnce(T) -> U) -> PeripheralRef<'a, U>
pub fn map<U>(self, transform: impl FnOnce(T) -> U) -> PeripheralRef<'a, U>
Transform the inner peripheral.
This converts from PeripheralRef<'a, T>
to PeripheralRef<'a, U>
,
using a user-provided impl to convert from T
to U
.
Sourcepub fn map_into<U>(self) -> PeripheralRef<'a, U>where
T: Into<U>,
pub fn map_into<U>(self) -> PeripheralRef<'a, U>where
T: Into<U>,
Map the inner peripheral using Into
.
This converts from PeripheralRef<'a, T>
to PeripheralRef<'a, U>
,
using an Into
impl to convert from T
to U
.
For example, this can be useful to degrade GPIO pins: converting from
PeripheralRef<'a, GpioPin<11>>
to PeripheralRef<'a, AnyPin>
.
Trait Implementations§
Source§impl<'a, T: Debug> Debug for PeripheralRef<'a, T>
impl<'a, T: Debug> Debug for PeripheralRef<'a, T>
Source§impl<T> Deref for PeripheralRef<'_, T>
impl<T> Deref for PeripheralRef<'_, T>
Source§impl<'a, T> Format for PeripheralRef<'a, T>
impl<'a, T> Format for PeripheralRef<'a, T>
Source§impl<T: Peripheral> Peripheral for PeripheralRef<'_, T>
impl<T: Peripheral> Peripheral for PeripheralRef<'_, T>
Source§type P = <T as Peripheral>::P
type P = <T as Peripheral>::P
Source§unsafe fn clone_unchecked(&self) -> Self::P
unsafe fn clone_unchecked(&self) -> Self::P
Source§fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>where
Self: 'a,
fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>where
Self: 'a,
PeripheralRef
. Read more