pub trait Peripheral: Sized {
type P;
// Required method
unsafe fn clone_unchecked(&self) -> Self::P;
// Provided methods
fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>
where Self: 'a { ... }
fn map_into<U>(self) -> U
where Self::P: Into<U>,
U: Peripheral<P = U> { ... }
fn map<U>(self, transform: impl FnOnce(Self::P) -> U) -> U
where U: Peripheral<P = U> { ... }
}
Expand description
Trait for any type that can be used as a peripheral of type P
.
This is used in driver constructors, to allow passing either owned
peripherals (e.g. UART0
), or borrowed peripherals (e.g. &mut UART0
).
For example, if you have a driver with a constructor like this:
impl<'d, T> Uart<'d, T, Blocking> {
pub fn new<TX: PeripheralOutput, RX: PeripheralInput>(
uart: impl Peripheral<P = T> + 'd,
rx: impl Peripheral<P = RX> + 'd,
tx: impl Peripheral<P = TX> + 'd,
) -> Result<Self, Error> {
Ok(Self { .. })
}
}
You may call it with owned peripherals, which yields an instance that can
live forever ('static
):
let mut uart: Uart<'static, ...> = Uart::new(p.UART0, p.GPIO0, p.GPIO1);
Or you may call it with borrowed peripherals, which yields an instance that can only live for as long as the borrows last:
let mut uart: Uart<'_, ...> = Uart::new(&mut p.UART0, &mut p.GPIO0, &mut p.GPIO1);
§Implementation details, for HAL authors
When writing a HAL, the intended way to use this trait is to take impl Peripheral<P = ..>
in the HAL’s public API (such as driver constructors),
calling .into_ref()
to obtain a PeripheralRef
, and storing that in the
driver struct.
.into_ref()
on an owned T
yields a PeripheralRef<'static, T>
.
.into_ref()
on an &'a mut T
yields a PeripheralRef<'a, T>
.
Required Associated Types§
Required Methods§
Sourceunsafe fn clone_unchecked(&self) -> Self::P
unsafe fn clone_unchecked(&self) -> Self::P
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 into_ref()
instead. It returns a
PeripheralRef
, which allows the borrow checker to enforce this at
compile time.
Provided Methods§
Sourcefn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>where
Self: 'a,
fn into_ref<'a>(self) -> PeripheralRef<'a, Self::P>where
Self: 'a,
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>
.
Sourcefn map_into<U>(self) -> Uwhere
Self::P: Into<U>,
U: Peripheral<P = U>,
fn map_into<U>(self) -> Uwhere
Self::P: Into<U>,
U: Peripheral<P = U>,
Map the peripheral using Into
.
This converts from Peripheral<P = T>
to Peripheral<P = U>
,
using an Into
impl to convert from T
to U
.
Sourcefn map<U>(self, transform: impl FnOnce(Self::P) -> U) -> Uwhere
U: Peripheral<P = U>,
fn map<U>(self, transform: impl FnOnce(Self::P) -> U) -> Uwhere
U: Peripheral<P = U>,
Map the peripheral using Into
.
This converts from Peripheral<P = T>
to Peripheral<P = U>
,
using an Into
impl to convert from T
to U
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
Source§impl Peripheral for AnyGdmaChannel
Available on crate feature unstable
only.
impl Peripheral for AnyGdmaChannel
unstable
only.type P = AnyGdmaChannel
Source§impl Peripheral for AnyGdmaRxChannel
Available on crate feature unstable
only.
impl Peripheral for AnyGdmaRxChannel
unstable
only.type P = AnyGdmaRxChannel
Source§impl Peripheral for AnyGdmaTxChannel
Available on crate feature unstable
only.
impl Peripheral for AnyGdmaTxChannel
unstable
only.type P = AnyGdmaTxChannel
Source§impl Peripheral for DmaChannel0
Available on crate feature unstable
only.
impl Peripheral for DmaChannel0
unstable
only.type P = DmaChannel0
Source§impl Peripheral for DmaChannel1
Available on crate feature unstable
only.
impl Peripheral for DmaChannel1
unstable
only.type P = DmaChannel1
Source§impl Peripheral for DmaChannel2
Available on crate feature unstable
only.
impl Peripheral for DmaChannel2
unstable
only.type P = DmaChannel2
Source§impl Peripheral for InputConnection
Available on crate feature unstable
only.
impl Peripheral for InputConnection
unstable
only.type P = InputConnection
Source§impl Peripheral for InputSignal
Available on crate feature unstable
only.
impl Peripheral for InputSignal
unstable
only.type P = InputSignal
Source§impl Peripheral for OutputConnection
Available on crate feature unstable
only.
impl Peripheral for OutputConnection
unstable
only.type P = OutputConnection
Source§impl Peripheral for OutputSignal
Available on crate feature unstable
only.
impl Peripheral for OutputSignal
unstable
only.type P = OutputSignal
Source§impl Peripheral for APB_SARADC
impl Peripheral for APB_SARADC
type P = APB_SARADC
Source§impl Peripheral for ASSIST_DEBUG
impl Peripheral for ASSIST_DEBUG
type P = ASSIST_DEBUG
Source§impl Peripheral for I2C_ANA_MST
impl Peripheral for I2C_ANA_MST
type P = I2C_ANA_MST
Source§impl Peripheral for IEEE802154
impl Peripheral for IEEE802154
type P = IEEE802154
Source§impl Peripheral for INTERRUPT_CORE0
impl Peripheral for INTERRUPT_CORE0
type P = INTERRUPT_CORE0
Source§impl Peripheral for LP_I2C_ANA_MST
impl Peripheral for LP_I2C_ANA_MST
type P = LP_I2C_ANA_MST
Source§impl Peripheral for MEM_MONITOR
impl Peripheral for MEM_MONITOR
type P = MEM_MONITOR
Source§impl Peripheral for MODEM_LPCON
impl Peripheral for MODEM_LPCON
type P = MODEM_LPCON
Source§impl Peripheral for MODEM_SYSCON
impl Peripheral for MODEM_SYSCON
type P = MODEM_SYSCON
Source§impl Peripheral for SW_INTERRUPT
impl Peripheral for SW_INTERRUPT
type P = SW_INTERRUPT
Source§impl Peripheral for USB_DEVICE
impl Peripheral for USB_DEVICE
type P = USB_DEVICE
Source§impl Peripheral for Trng<'_>
Available on crate feature unstable
only.
impl Peripheral for Trng<'_>
unstable
only.Source§impl Peripheral for AnyTimer
Available on crate feature unstable
only.
impl Peripheral for AnyTimer
unstable
only.Source§impl<T: Peripheral> Peripheral for PeripheralRef<'_, T>
impl<T: Peripheral> Peripheral for PeripheralRef<'_, T>
type P = <T as Peripheral>::P
Source§impl<T: DerefMut> Peripheral for Twhere
T::Target: Peripheral,
impl<T: DerefMut> Peripheral for Twhere
T::Target: Peripheral,
type P = <<T as Deref>::Target as Peripheral>::P
Source§impl<const GPIONUM: u8> Peripheral for GpioPin<GPIONUM>where
Self: Pin,
impl<const GPIONUM: u8> Peripheral for GpioPin<GPIONUM>where
Self: Pin,
Source§impl<const NUM: u8> Peripheral for SoftwareInterrupt<NUM>
Available on crate feature unstable
only.
impl<const NUM: u8> Peripheral for SoftwareInterrupt<NUM>
unstable
only.