Skip to content

Commit

Permalink
Scoped callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 7, 2023
1 parent e20bf5c commit 7fea931
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 17 additions & 18 deletions src/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,38 @@ where
}

pub trait EventBus<P>: ErrorType {
type Subscription;
type Subscription<'a>;

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

impl<'a, P, E> EventBus<P> for &'a mut E
impl<'e, P, E> EventBus<P> for &'e E
where
E: EventBus<P>,
{
type Subscription = E::Subscription;
type Subscription<'a> = E::Subscription<'a>;

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

impl<'a, P, E> EventBus<P> for &'a E
impl<'e, P, E> EventBus<P> for &'e mut E
where
E: EventBus<P>,
{
type Subscription = E::Subscription;
type Subscription<'a> = E::Subscription<'a>;

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

Expand Down
21 changes: 11 additions & 10 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ where
pub trait TimerService: ErrorType {
type Timer<'a>: OnceTimer<Error = Self::Error> + PeriodicTimer<Error = Self::Error> + 'a;

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

impl<S> TimerService for &S
Expand All @@ -80,10 +81,10 @@ where
{
type Timer<'a> = S::Timer<'a>;

fn timer<'a>(
&self,
callback: impl FnMut() + Send + 'a,
) -> Result<Self::Timer<'a>, Self::Error> {
fn timer<'a, F>(&self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
where
F: FnMut() + Send + 'a,
{
(*self).timer(callback)
}
}
Expand All @@ -94,10 +95,10 @@ where
{
type Timer<'a> = S::Timer<'a>;

fn timer<'a>(
&self,
callback: impl FnMut() + Send + 'a,
) -> Result<Self::Timer<'a>, Self::Error> {
fn timer<'a, F>(&self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
where
F: FnMut() + Send + 'a,
{
(**self).timer(callback)
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/utils/asyncify/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ where
CV: RawCondvar + Send + Sync + 'static,
CV::RawMutex: Send + Sync + 'static,
{
pub fn subscribe<P>(&self) -> Result<AsyncSubscription<CV, P, E::Subscription>, E::Error>
pub fn subscribe<P>(
&self,
) -> Result<AsyncSubscription<CV, P, E::Subscription<'static>>, E::Error>
where
P: Clone + Send + 'static,
E: crate::event_bus::EventBus<P>,
E::Subscription: Send + 'static,
E::Subscription<'static>: Send + 'static,
{
let state = Arc::new((
Mutex::new(SubscriptionState {
Expand Down Expand Up @@ -317,9 +319,9 @@ mod async_traits_impl {
CV::RawMutex: Send + Sync + 'static,
P: Clone + Send + 'static,
E: crate::event_bus::EventBus<P>,
E::Subscription: Send + 'static,
for<'a> E::Subscription<'a>: Send + 'static,
{
type Subscription = AsyncSubscription<CV, P, E::Subscription>;
type Subscription = AsyncSubscription<CV, P, E::Subscription<'static>>;

fn subscribe(&self) -> Result<Self::Subscription, Self::Error> {
AsyncEventBus::subscribe(self)
Expand Down

0 comments on commit 7fea931

Please sign in to comment.