Struct embassy_nrf::PeripheralRef
· 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
PA9
orSPI4
) 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
SPI4
and&mut SPI4
, the driver code would be monomorphized two times. With PeripheralRef, the driver is generic over a lifetime only.SPI4
becomesPeripheralRef<'static, SPI4>
, and&mut SPI4
becomesPeripheralRef<'a, SPI4>
. Lifetimes don’t cause monomorphization.
Implementations§
§impl<'a, T> PeripheralRef<'a, T>
impl<'a, T> PeripheralRef<'a, T>
pub fn new(inner: T) -> PeripheralRef<'a, T>
pub unsafe fn clone_unchecked(&mut self) -> PeripheralRef<'a, T>where
T: Peripheral<P = T>,
pub unsafe fn clone_unchecked(&mut 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.
pub 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.
pub 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, PB11>to
PeripheralRef<’a, AnyPin>`.
Trait Implementations§
§impl<'a, T> Deref for PeripheralRef<'a, T>
impl<'a, T> Deref for PeripheralRef<'a, T>
§impl<'a, T> DerefMut for PeripheralRef<'a, T>
impl<'a, T> DerefMut for PeripheralRef<'a, T>
§fn deref_mut(&mut self) -> &mut <PeripheralRef<'a, T> as Deref>::Target
fn deref_mut(&mut self) -> &mut <PeripheralRef<'a, T> as Deref>::Target
Auto Trait Implementations§
impl<'a, T> RefUnwindSafe for PeripheralRef<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for PeripheralRef<'a, T>where
T: Send,
impl<'a, T> Sync for PeripheralRef<'a, T>where
T: Sync,
impl<'a, T> Unpin for PeripheralRef<'a, T>where
T: Unpin,
impl<'a, T> !UnwindSafe for PeripheralRef<'a, T>
Blanket Implementations§
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
§impl<'b, T> Peripheral for Twhere
T: DerefMut,
<T as Deref>::Target: Peripheral,
impl<'b, T> Peripheral for Twhere
T: DerefMut,
<T as Deref>::Target: Peripheral,
§unsafe fn clone_unchecked(&mut self) -> <T as Peripheral>::P
unsafe fn clone_unchecked(&mut self) -> <T as Peripheral>::P
§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