From 7fea93148569083ef5b5222b405cedba870fee2b Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Sat, 7 Oct 2023 08:49:30 +0000 Subject: [PATCH] Scoped callbacks --- CHANGELOG.md | 3 ++- src/event_bus.rs | 35 ++++++++++++++++----------------- src/timer.rs | 21 ++++++++++---------- src/utils/asyncify/event_bus.rs | 10 ++++++---- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9aadb5..28bc61b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * 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: 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: 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` * Breaking change: MQTT: GAT `Connection::Message` now parametric over lifetime diff --git a/src/event_bus.rs b/src/event_bus.rs index 6a1fc2c..7ecdba7 100644 --- a/src/event_bus.rs +++ b/src/event_bus.rs @@ -47,39 +47,38 @@ where } pub trait EventBus

: ErrorType { - type Subscription; + type Subscription<'a>; - fn subscribe( - &self, - callback: impl for<'a> FnMut(&'a P) + Send + 'static, - ) -> Result; + fn subscribe<'a, F>(&self, callback: F) -> Result, Self::Error> + where + F: FnMut(&P) + Send + 'a; } -impl<'a, P, E> EventBus

for &'a mut E +impl<'e, P, E> EventBus

for &'e E where E: EventBus

, { - type Subscription = E::Subscription; + type Subscription<'a> = E::Subscription<'a>; - fn subscribe( - &self, - callback: impl for<'b> FnMut(&'b P) + Send + 'static, - ) -> Result { + fn subscribe<'a, F>(&self, callback: F) -> Result, Self::Error> + where + F: FnMut(&P) + Send + 'a, + { (**self).subscribe(callback) } } -impl<'a, P, E> EventBus

for &'a E +impl<'e, P, E> EventBus

for &'e mut E where E: EventBus

, { - type Subscription = E::Subscription; + type Subscription<'a> = E::Subscription<'a>; - fn subscribe( - &self, - callback: impl for<'b> FnMut(&'b P) + Send + 'static, - ) -> Result { - (*self).subscribe(callback) + fn subscribe<'a, F>(&self, callback: F) -> Result, Self::Error> + where + F: FnMut(&P) + Send + 'a, + { + (**self).subscribe(callback) } } diff --git a/src/timer.rs b/src/timer.rs index c495b75..d1fdabd 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -70,8 +70,9 @@ where pub trait TimerService: ErrorType { type Timer<'a>: OnceTimer + PeriodicTimer + 'a; - fn timer<'a>(&self, callback: impl FnMut() + Send + 'a) - -> Result, Self::Error>; + fn timer<'a, F>(&self, callback: F) -> Result, Self::Error> + where + F: FnMut() + Send + 'a; } impl TimerService for &S @@ -80,10 +81,10 @@ where { type Timer<'a> = S::Timer<'a>; - fn timer<'a>( - &self, - callback: impl FnMut() + Send + 'a, - ) -> Result, Self::Error> { + fn timer<'a, F>(&self, callback: F) -> Result, Self::Error> + where + F: FnMut() + Send + 'a, + { (*self).timer(callback) } } @@ -94,10 +95,10 @@ where { type Timer<'a> = S::Timer<'a>; - fn timer<'a>( - &self, - callback: impl FnMut() + Send + 'a, - ) -> Result, Self::Error> { + fn timer<'a, F>(&self, callback: F) -> Result, Self::Error> + where + F: FnMut() + Send + 'a, + { (**self).timer(callback) } } diff --git a/src/utils/asyncify/event_bus.rs b/src/utils/asyncify/event_bus.rs index 36f51e2..bf374d4 100644 --- a/src/utils/asyncify/event_bus.rs +++ b/src/utils/asyncify/event_bus.rs @@ -150,11 +150,13 @@ where CV: RawCondvar + Send + Sync + 'static, CV::RawMutex: Send + Sync + 'static, { - pub fn subscribe

(&self) -> Result, E::Error> + pub fn subscribe

( + &self, + ) -> Result>, E::Error> where P: Clone + Send + 'static, E: crate::event_bus::EventBus

, - E::Subscription: Send + 'static, + E::Subscription<'static>: Send + 'static, { let state = Arc::new(( Mutex::new(SubscriptionState { @@ -317,9 +319,9 @@ mod async_traits_impl { CV::RawMutex: Send + Sync + 'static, P: Clone + Send + 'static, E: crate::event_bus::EventBus

, - E::Subscription: Send + 'static, + for<'a> E::Subscription<'a>: Send + 'static, { - type Subscription = AsyncSubscription; + type Subscription = AsyncSubscription>; fn subscribe(&self) -> Result { AsyncEventBus::subscribe(self)