Skip to content

Commit

Permalink
Restore Ping trait constraints; relax Subscription: Send in AsyncSubs…
Browse files Browse the repository at this point in the history
…cription
  • Loading branch information
ivmarkov committed Nov 12, 2023
1 parent 7b51805 commit 0e4c56d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.26.3] - 2023-11-12
* Traits `EventBus` and `TimerService`: do not allow subscriptions with non-static callbacks, as these are impossible to implement safely on ESP IDF

## [0.26.2] - 2023-11-05
* A temporary workaround for https://github.com/rust-lang/rust/issues/117602

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "embedded-svc"
version = "0.26.2"
version = "0.26.3"
authors = ["Ivan Markov <[email protected]>"]
edition = "2018"
resolver = "2"
Expand Down
8 changes: 4 additions & 4 deletions src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub trait Ping {

fn ping(&mut self, ip: ipv4::Ipv4Addr, conf: &Configuration) -> Result<Summary, Self::Error>;

fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand All @@ -81,7 +81,7 @@ where
(*self).ping(ip, conf)
}

fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand All @@ -107,7 +107,7 @@ pub mod asynch {
conf: &Configuration,
) -> Result<Summary, Self::Error>;

async fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
async fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand All @@ -129,7 +129,7 @@ pub mod asynch {
(*self).ping(ip, conf).await
}

async fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
async fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand Down
34 changes: 17 additions & 17 deletions src/utils/asyncify/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ where
}
}

pub struct SubscriptionState<P, S> {
subscription: Option<S>,
pub struct SubscriptionState<P> {
value: Option<P>,
waker: Option<Waker>,
}

#[allow(clippy::type_complexity)]
pub struct AsyncSubscription<CV, P, S, E>(
Arc<(Mutex<CV::RawMutex, SubscriptionState<P, S>>, Condvar<CV>)>,
PhantomData<fn() -> E>,
)
pub struct AsyncSubscription<CV, P, S, E>
where
CV: RawCondvar,
P: Send,
S: Send;
{
state: Arc<(Mutex<CV::RawMutex, SubscriptionState<P>>, Condvar<CV>)>,
_subscription: S,
_pd: PhantomData<fn() -> E>,
}

impl<CV, P, S, E> AsyncSubscription<CV, P, S, E>
where
Expand Down Expand Up @@ -126,7 +126,7 @@ where
S: Send,
{
fn drop(&mut self) {
let mut state = self.0 .0 .0.lock();
let mut state = self.0.state.0.lock();
state.waker = None;
}
}
Expand All @@ -141,18 +141,18 @@ where
type Output = Result<P, E>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut state = self.0 .0 .0.lock();
let mut state = self.0.state.0.lock();

let value = state.value.take();

if let Some(value) = value {
self.0 .0 .1.notify_all();
self.0.state.1.notify_all();

Poll::Ready(Ok(value))
} else {
state.waker = Some(cx.waker().clone());

self.0 .0 .1.notify_all();
self.0.state.1.notify_all();

Poll::Pending
}
Expand Down Expand Up @@ -187,11 +187,9 @@ where
where
P: Clone + Send + 'static,
E: crate::event_bus::EventBus<P>,
for<'a> E::Subscription<'a>: Send + 'static,
{
let state = Arc::new((
Mutex::new(SubscriptionState {
subscription: None,
value: None,
waker: None,
}),
Expand Down Expand Up @@ -222,9 +220,11 @@ where
}
})?;

state.0.lock().subscription = Some(subscription);

Ok(AsyncSubscription(state, PhantomData))
Ok(AsyncSubscription {
state,
_subscription: subscription,
_pd: PhantomData,
})
}
}

Expand Down Expand Up @@ -367,7 +367,7 @@ mod async_traits_impl {
CV::RawMutex: Send + Sync + 'static,
P: Clone + Send + 'static,
E: crate::event_bus::EventBus<P>,
for<'a> E::Subscription<'a>: Send + 'static,
for<'a> E::Subscription<'a>: Send,
{
type Subscription<'a> = AsyncSubscription<CV, P, E::Subscription<'a>, E::Error> where Self: 'a;

Expand Down

0 comments on commit 0e4c56d

Please sign in to comment.