Skip to content

Commit

Permalink
Enhancements to the TimerService trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 7, 2023
1 parent 2bed61d commit e20bf5c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Breaking change: Upgraded to `embedded-io` 0.5 and `embedded-io-async` 0.5
* Upgraded `strum` and `strum-macros` to 0.25
* 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 handler now only needs to live as long as the `TimerService::Timer` associated type. Therefore, `TimerService::Timer` is now lifetimed: `TimerService::Timer<'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`
* Breaking change: MQTT: GAT `Connection::Message` now parametric over lifetime
Expand Down
51 changes: 37 additions & 14 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,40 @@ where
}

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

fn timer(
&mut self,
callback: impl FnMut() + Send + 'static,
) -> Result<Self::Timer, Self::Error>;
fn timer<'a>(&self, callback: impl FnMut() + Send + 'a)
-> Result<Self::Timer<'a>, Self::Error>;
}

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

fn timer(
&mut self,
callback: impl FnMut() + Send + 'static,
) -> Result<Self::Timer, Self::Error> {
fn timer<'a>(
&self,
callback: impl FnMut() + Send + 'a,
) -> Result<Self::Timer<'a>, Self::Error> {
(*self).timer(callback)
}
}

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

fn timer<'a>(
&self,
callback: impl FnMut() + Send + 'a,
) -> Result<Self::Timer<'a>, Self::Error> {
(**self).timer(callback)
}
}

#[cfg(feature = "nightly")]
pub mod asynch {
use core::time::Duration;
Expand Down Expand Up @@ -150,17 +162,28 @@ pub mod asynch {
+ Send
+ 'static;

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

impl<T> TimerService for &mut T
impl<T> TimerService for &T
where
T: TimerService,
{
type Timer = T::Timer;

fn timer(&mut self) -> Result<Self::Timer, Self::Error> {
fn timer(&self) -> Result<Self::Timer, Self::Error> {
(*self).timer()
}
}

impl<T> TimerService for &mut T
where
T: TimerService,
{
type Timer = T::Timer;

fn timer(&self) -> Result<Self::Timer, Self::Error> {
(**self).timer()
}
}
}
10 changes: 5 additions & 5 deletions src/utils/asyncify/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ where
impl<T> AsyncTimerService<T>
where
T: crate::timer::TimerService,
T::Timer: Send,
for<'a> T::Timer<'a>: Send,
{
pub fn timer(&mut self) -> Result<AsyncTimer<T::Timer>, T::Error> {
pub fn timer(&self) -> Result<AsyncTimer<T::Timer<'static>>, T::Error> {
let signal = Arc::new(TimerSignal::new());

let timer = {
Expand Down Expand Up @@ -226,11 +226,11 @@ mod async_traits_impl {
impl<T> TimerService for AsyncTimerService<T>
where
T: crate::timer::TimerService,
T::Timer: Send,
for<'a> T::Timer<'a>: Send,
{
type Timer = AsyncTimer<T::Timer>;
type Timer = AsyncTimer<T::Timer<'static>>;

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

0 comments on commit e20bf5c

Please sign in to comment.