Skip to content

Commit

Permalink
More constrained TimerService trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 7, 2023
1 parent 7fea931 commit d723bd9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* OTA: New method: `Ota::finish` that allows to postpone/avoid setting the updated partition as a boot one
* TimerService: `TimerService::timer` now takes `&self` instead of `&mut self`
* Breaking change: TimerService: scoped handler: the timer callback now only needs to live as long as the `TimerService::Timer` associated type. Therefore, `TimerService::Timer` is now lifetimed: `TimerService::Timer<'a>`
* Breaking change: TimerService: `TimerService::Timer` now borrows from `TimerService`. Therefore, that's another reason why `TimerService::Timer` is now lifetimed: `TimerService::Timer<'a>`
* Breaking change: EventBus: scoped handler: the subscription callback now only needs to live as long as the `EventBus::Subscription` associated type. Therefore, `EventBus::Subscription` is now lifetimed: `EventBus::Subscription<'a>`
* Breaking change: OTA: GAT `Ota::Update` now parametric over lifetime and no longer returned by `&mut` ref
* Breaking change: OTA: `OtaUpdate::abort` and `OtaUpdate::complete` now take `self` instead of `&mut self`
Expand Down
31 changes: 16 additions & 15 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ where
}

pub trait TimerService: ErrorType {
type Timer<'a>: OnceTimer<Error = Self::Error> + PeriodicTimer<Error = Self::Error> + 'a;
type Timer<'a>: OnceTimer<Error = Self::Error> + PeriodicTimer<Error = Self::Error> + 'a
where
Self: 'a;

fn timer<'a, F>(&self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
fn timer<'a, F>(&'a self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
where
F: FnMut() + Send + 'a;
}
Expand All @@ -79,9 +81,9 @@ impl<S> TimerService for &S
where
S: TimerService,
{
type Timer<'a> = S::Timer<'a>;
type Timer<'a> = S::Timer<'a> where Self: 'a;

fn timer<'a, F>(&self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
fn timer<'a, F>(&'a self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
where
F: FnMut() + Send + 'a,
{
Expand All @@ -93,9 +95,9 @@ impl<S> TimerService for &mut S
where
S: TimerService,
{
type Timer<'a> = S::Timer<'a>;
type Timer<'a> = S::Timer<'a> where Self: 'a;

fn timer<'a, F>(&self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
fn timer<'a, F>(&'a self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
where
F: FnMut() + Send + 'a,
{
Expand Down Expand Up @@ -158,21 +160,20 @@ pub mod asynch {
}

pub trait TimerService: ErrorType {
type Timer: OnceTimer<Error = Self::Error>
+ PeriodicTimer<Error = Self::Error>
+ Send
+ 'static;
type Timer<'a>: OnceTimer<Error = Self::Error> + PeriodicTimer<Error = Self::Error> + Send
where
Self: 'a;

fn timer(&self) -> Result<Self::Timer, Self::Error>;
fn timer(&self) -> Result<Self::Timer<'_>, Self::Error>;
}

impl<T> TimerService for &T
where
T: TimerService,
{
type Timer = T::Timer;
type Timer<'a> = T::Timer<'a> where Self: 'a;

fn timer(&self) -> Result<Self::Timer, Self::Error> {
fn timer(&self) -> Result<Self::Timer<'_>, Self::Error> {
(*self).timer()
}
}
Expand All @@ -181,9 +182,9 @@ pub mod asynch {
where
T: TimerService,
{
type Timer = T::Timer;
type Timer<'a> = T::Timer<'a> where Self: 'a;

fn timer(&self) -> Result<Self::Timer, Self::Error> {
fn timer(&self) -> Result<Self::Timer<'_>, Self::Error> {
(**self).timer()
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/utils/asyncify/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct AsyncTimer<T> {

impl<T> AsyncTimer<T>
where
T: crate::timer::OnceTimer + Send + 'static,
T: crate::timer::OnceTimer + Send,
{
pub async fn after(&mut self, duration: Duration) -> Result<(), T::Error> {
self.timer.cancel()?;
Expand Down Expand Up @@ -103,7 +103,7 @@ where

impl<'a, T> Future for TimerFuture<'a, T>
where
T: crate::timer::OnceTimer + 'static,
T: crate::timer::OnceTimer,
{
type Output = ();

Expand Down Expand Up @@ -142,7 +142,7 @@ where
T: crate::timer::TimerService,
for<'a> T::Timer<'a>: Send,
{
pub fn timer(&self) -> Result<AsyncTimer<T::Timer<'static>>, T::Error> {
pub fn timer(&self) -> Result<AsyncTimer<T::Timer<'_>>, T::Error> {
let signal = Arc::new(TimerSignal::new());

let timer = {
Expand Down Expand Up @@ -189,7 +189,7 @@ mod async_traits_impl {

impl<T> OnceTimer for AsyncTimer<T>
where
T: crate::timer::OnceTimer + Send + 'static,
T: crate::timer::OnceTimer + Send,
{
async fn after(&mut self, duration: Duration) -> Result<(), Self::Error> {
AsyncTimer::after(self, duration).await
Expand All @@ -198,9 +198,9 @@ mod async_traits_impl {

impl<T> PeriodicTimer for AsyncTimer<T>
where
T: crate::timer::OnceTimer + Send + 'static,
T: crate::timer::OnceTimer + Send,
{
type Clock<'a> = &'a mut Self;
type Clock<'a> = &'a mut Self where Self: 'a;

fn every(&mut self, duration: Duration) -> Result<Self::Clock<'_>, Self::Error> {
AsyncTimer::every(self, duration)
Expand All @@ -209,7 +209,7 @@ mod async_traits_impl {

impl<'a, T> Clock for &'a mut AsyncTimer<T>
where
T: crate::timer::OnceTimer + Send + 'static,
T: crate::timer::OnceTimer + Send,
{
async fn tick(&mut self) {
AsyncTimer::tick(self).await
Expand All @@ -228,9 +228,9 @@ mod async_traits_impl {
T: crate::timer::TimerService,
for<'a> T::Timer<'a>: Send,
{
type Timer = AsyncTimer<T::Timer<'static>>;
type Timer<'a> = AsyncTimer<T::Timer<'a>> where Self: 'a;

fn timer(&self) -> Result<Self::Timer, Self::Error> {
fn timer(&self) -> Result<Self::Timer<'_>, Self::Error> {
AsyncTimerService::timer(self)
}
}
Expand Down

0 comments on commit d723bd9

Please sign in to comment.