feature(1354): Added lifetimes to Event +

This commit is contained in:
Cameron 2023-06-29 18:09:26 +02:00
parent 3f19879f41
commit 24e186e684
2 changed files with 20 additions and 13 deletions

View File

@ -15,6 +15,7 @@
//! many tasks and events, but any single task or event can only be coupled with one channel. //! many tasks and events, but any single task or event can only be coupled with one channel.
//! //!
use core::marker::PhantomData;
use core::ptr::NonNull; use core::ptr::NonNull;
use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef}; use embassy_hal_common::{impl_peripheral, into_ref, PeripheralRef};
@ -125,16 +126,16 @@ const REGISTER_DPPI_CONFIG_OFFSET: usize = 0x80 / core::mem::size_of::<u32>();
/// When a task is subscribed to a PPI channel, it will run when the channel is triggered by /// When a task is subscribed to a PPI channel, it will run when the channel is triggered by
/// a published event. /// a published event.
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
pub struct Task(NonNull<u32>); pub struct Task<'d>(NonNull<u32>, PhantomData<&'d ()>);
impl Task { impl<'d> Task<'_> {
/// Create a new `Task` from a task register pointer /// Create a new `Task` from a task register pointer
/// ///
/// # Safety /// # Safety
/// ///
/// `ptr` must be a pointer to a valid `TASKS_*` register from an nRF peripheral. /// `ptr` must be a pointer to a valid `TASKS_*` register from an nRF peripheral.
pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self { pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self {
Self(ptr) Self(ptr, PhantomData)
} }
/// Triggers this task. /// Triggers this task.
@ -143,7 +144,10 @@ impl Task {
} }
pub(crate) fn from_reg<T>(reg: &T) -> Self { pub(crate) fn from_reg<T>(reg: &T) -> Self {
Self(unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) }) Self(
unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) },
PhantomData,
)
} }
/// Address of subscription register for this task. /// Address of subscription register for this task.
@ -156,26 +160,29 @@ impl Task {
/// # Safety /// # Safety
/// ///
/// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core. /// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core.
unsafe impl Send for Task {} unsafe impl Send for Task<'_> {}
/// Represents an event that a peripheral can publish. /// Represents an event that a peripheral can publish.
/// ///
/// An event can be set to publish on a PPI channel when the event happens. /// An event can be set to publish on a PPI channel when the event happens.
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
pub struct Event(NonNull<u32>); pub struct Event<'d>(NonNull<u32>, PhantomData<&'d ()>);
impl Event { impl<'d> Event<'_> {
/// Create a new `Event` from an event register pointer /// Create a new `Event` from an event register pointer
/// ///
/// # Safety /// # Safety
/// ///
/// `ptr` must be a pointer to a valid `EVENTS_*` register from an nRF peripheral. /// `ptr` must be a pointer to a valid `EVENTS_*` register from an nRF peripheral.
pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self { pub unsafe fn new_unchecked(ptr: NonNull<u32>) -> Self {
Self(ptr) Self(ptr, PhantomData)
} }
pub(crate) fn from_reg<T>(reg: &T) -> Self { pub(crate) fn from_reg<T>(reg: &'d T) -> Self {
Self(unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) }) Self(
unsafe { NonNull::new_unchecked(reg as *const _ as *mut _) },
PhantomData,
)
} }
/// Describes whether this Event is currently in a triggered state. /// Describes whether this Event is currently in a triggered state.
@ -198,7 +205,7 @@ impl Event {
/// # Safety /// # Safety
/// ///
/// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core. /// NonNull is not send, but this event is only allowed to point at registers and those exist in any context on the same core.
unsafe impl Send for Event {} unsafe impl Send for Event<'_> {}
// ====================== // ======================
// traits // traits

View File

@ -3,12 +3,12 @@ use embassy_hal_common::into_ref;
use super::{Channel, ConfigurableChannel, Event, Ppi, StaticChannel, Task}; use super::{Channel, ConfigurableChannel, Event, Ppi, StaticChannel, Task};
use crate::{pac, Peripheral}; use crate::{pac, Peripheral};
impl Task { impl<'d> Task<'_> {
fn reg_val(&self) -> u32 { fn reg_val(&self) -> u32 {
self.0.as_ptr() as _ self.0.as_ptr() as _
} }
} }
impl Event { impl<'d> Event<'_> {
fn reg_val(&self) -> u32 { fn reg_val(&self) -> u32 {
self.0.as_ptr() as _ self.0.as_ptr() as _
} }