From 1c1ffe342739bccfd42366c5edc082f675a49886 Mon Sep 17 00:00:00 2001 From: ivmarkov Date: Sat, 11 Nov 2023 18:15:24 +0000 Subject: [PATCH] Fix UB: Non-static callbacks are unsound --- .github/configs/sdkconfig.defaults | 4 +- Cargo.toml | 3 ++ src/bt.rs | 9 ++--- src/bt/a2dp.rs | 8 ++-- src/bt/avrc.rs | 2 +- src/bt/ble/gap.rs | 2 +- src/bt/gap.rs | 2 +- src/bt/hfp.rs | 2 +- src/espnow.rs | 22 ++++------ src/eth.rs | 16 ++++---- src/eventloop.rs | 50 +++++++++++------------ src/http/server.rs | 52 +++++++++++------------- src/mqtt/client.rs | 64 +++++++++++++++--------------- src/ping.rs | 12 +++--- src/sntp.rs | 7 +--- src/timer.rs | 38 +++++++++--------- src/wifi.rs | 17 ++------ src/ws/client.rs | 18 ++++----- 18 files changed, 153 insertions(+), 175 deletions(-) diff --git a/.github/configs/sdkconfig.defaults b/.github/configs/sdkconfig.defaults index 719556ee312..e488d86cb73 100644 --- a/.github/configs/sdkconfig.defaults +++ b/.github/configs/sdkconfig.defaults @@ -1,5 +1,5 @@ -CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=y -CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y +CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n +#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=y # Examples often require a larger than the default stack size for the main thread. CONFIG_ESP_MAIN_TASK_STACK_SIZE=10000 diff --git a/Cargo.toml b/Cargo.toml index b3bd6437cbb..57dc8ae09f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,9 @@ build = "build.rs" documentation = "https://esp-rs.github.io/esp-idf-svc/" rust-version = "1.71" +[patch.crates-io] +embedded-svc = { git = "https://github.com/esp-rs/embedded-svc" } + [features] default = ["std", "native", "binstart"] diff --git a/src/bt.rs b/src/bt.rs index 424c49878e9..9aeea253004 100644 --- a/src/bt.rs +++ b/src/bt.rs @@ -138,7 +138,7 @@ impl From for BtUuid { #[allow(clippy::type_complexity)] pub(crate) struct BtCallback { initialized: AtomicBool, - callback: UnsafeCell R>>>, + callback: UnsafeCell R + Send + 'static>>>, default_result: R, } @@ -155,16 +155,15 @@ where } } - pub fn set<'d, F>(&self, callback: F) -> Result<(), EspError> + pub fn set(&self, callback: F) -> Result<(), EspError> where - F: Fn(A) -> R + Send + 'd, + F: Fn(A) -> R + Send + 'static, { self.initialized .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst) .map_err(|_| EspError::from_infallible::())?; - let b: alloc::boxed::Box R + 'd> = alloc::boxed::Box::new(callback); - let b: alloc::boxed::Box R + 'static> = unsafe { core::mem::transmute(b) }; + let b: alloc::boxed::Box R + 'static> = alloc::boxed::Box::new(callback); *unsafe { self.callback.get().as_mut() }.unwrap() = Some(b); Ok(()) diff --git a/src/bt/a2dp.rs b/src/bt/a2dp.rs index c02ee53824b..1f3e1fdddfb 100644 --- a/src/bt/a2dp.rs +++ b/src/bt/a2dp.rs @@ -299,7 +299,7 @@ where pub fn initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(A2dpEvent) + Send + 'd, + F: Fn(A2dpEvent) + Send + 'static, { self.internal_initialize(move |event| { events_cb(event); @@ -343,7 +343,7 @@ where pub fn initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(A2dpEvent) -> usize + Send + 'd, + F: Fn(A2dpEvent) -> usize + Send + 'static, { self.internal_initialize(events_cb) } @@ -374,7 +374,7 @@ where pub fn initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(A2dpEvent) -> usize + Send + 'd, + F: Fn(A2dpEvent) -> usize + Send + 'static, { self.internal_initialize(events_cb) } @@ -388,7 +388,7 @@ where { fn internal_initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(A2dpEvent) -> usize + Send + 'd, + F: Fn(A2dpEvent) -> usize + Send + 'static, { CALLBACK.set(events_cb)?; diff --git a/src/bt/avrc.rs b/src/bt/avrc.rs index 2340a9d2f98..e01de5bc939 100644 --- a/src/bt/avrc.rs +++ b/src/bt/avrc.rs @@ -426,7 +426,7 @@ pub mod controller { pub fn initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(AvrccEvent) + Send + 'd, + F: Fn(AvrccEvent) + Send + 'static, { CALLBACK.set(events_cb)?; diff --git a/src/bt/ble/gap.rs b/src/bt/ble/gap.rs index 3c4b98af8c7..1c5ac2eced3 100644 --- a/src/bt/ble/gap.rs +++ b/src/bt/ble/gap.rs @@ -445,7 +445,7 @@ where pub fn initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(GapEvent) + Send + 'd, + F: Fn(GapEvent) + Send + 'static, { CALLBACK.set(events_cb)?; diff --git a/src/bt/gap.rs b/src/bt/gap.rs index a668d736943..935db62d7c6 100644 --- a/src/bt/gap.rs +++ b/src/bt/gap.rs @@ -626,7 +626,7 @@ where pub fn initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(GapEvent) + Send + 'd, + F: Fn(GapEvent) + Send + 'static, { CALLBACK.set(events_cb)?; diff --git a/src/bt/hfp.rs b/src/bt/hfp.rs index 987673f4f72..fbfaebbeb4d 100644 --- a/src/bt/hfp.rs +++ b/src/bt/hfp.rs @@ -320,7 +320,7 @@ pub mod client { pub fn initialize(&self, events_cb: F) -> Result<(), EspError> where - F: Fn(HfpcEvent) -> usize + Send + 'd, + F: Fn(HfpcEvent) -> usize + Send + 'static, { CALLBACK.set(events_cb)?; diff --git a/src/espnow.rs b/src/espnow.rs index 1708124f816..442f2ff44ff 100644 --- a/src/espnow.rs +++ b/src/espnow.rs @@ -47,9 +47,9 @@ impl From for SendStatus { pub type PeerInfo = esp_now_peer_info_t; -pub struct EspNow<'a>(PhantomData<&'a ()>); +pub struct EspNow(()); -impl<'a> EspNow<'a> { +impl EspNow { pub fn take() -> Result { let mut taken = TAKEN.lock(); @@ -67,7 +67,7 @@ impl<'a> EspNow<'a> { *taken = true; - Ok(Self(PhantomData)) + Ok(Self(())) } pub fn send(&self, peer_addr: [u8; 6], data: &[u8]) -> Result<(), EspError> { @@ -130,13 +130,10 @@ impl<'a> EspNow<'a> { pub fn register_recv_cb(&self, callback: F) -> Result<(), EspError> where - F: FnMut(&[u8], &[u8]) + Send + 'a, + F: FnMut(&[u8], &[u8]) + Send + 'static, { #[allow(clippy::type_complexity)] - let callback: Box = Box::new(callback); - #[allow(clippy::type_complexity)] - let callback: Box = - unsafe { core::mem::transmute(callback) }; + let callback: Box = Box::new(callback); *RECV_CALLBACK.lock() = Some(Box::new(callback)); esp!(unsafe { esp_now_register_recv_cb(Some(Self::recv_callback)) })?; @@ -153,13 +150,10 @@ impl<'a> EspNow<'a> { pub fn register_send_cb(&self, callback: F) -> Result<(), EspError> where - F: FnMut(&[u8], SendStatus) + Send + 'a, + F: FnMut(&[u8], SendStatus) + Send + 'static, { #[allow(clippy::type_complexity)] - let callback: Box = Box::new(callback); - #[allow(clippy::type_complexity)] - let callback: Box = - unsafe { core::mem::transmute(callback) }; + let callback: Box = Box::new(callback); *SEND_CALLBACK.lock() = Some(Box::new(callback)); esp!(unsafe { esp_now_register_send_cb(Some(Self::send_callback)) })?; @@ -203,7 +197,7 @@ impl<'a> EspNow<'a> { } } -impl<'a> Drop for EspNow<'a> { +impl Drop for EspNow { fn drop(&mut self) { let mut taken = TAKEN.lock(); diff --git a/src/eth.rs b/src/eth.rs index 4552c47d3ac..b78ceb8d2e9 100644 --- a/src/eth.rs +++ b/src/eth.rs @@ -122,13 +122,13 @@ struct RawHandleImpl(esp_eth_handle_t); unsafe impl Send for RawHandleImpl {} -type RawCallback<'a> = Box; +type RawCallback = Box; -struct UnsafeCallback<'a>(*mut RawCallback<'a>); +struct UnsafeCallback(*mut RawCallback); -impl<'a> UnsafeCallback<'a> { +impl UnsafeCallback { #[allow(clippy::type_complexity)] - fn from(boxed: &mut Box>) -> Self { + fn from(boxed: &mut Box) -> Self { Self(boxed.as_mut()) } @@ -178,8 +178,8 @@ pub struct EthDriver<'d, T> { _flavor: T, handle: esp_eth_handle_t, status: Arc>, - _subscription: EspSubscription<'static, System>, - callback: Option>>, + _subscription: EspSubscription, + callback: Option>, _p: PhantomData<&'d mut ()>, } @@ -665,9 +665,9 @@ impl<'d, T> EthDriver<'d, T> { Ok(()) } - pub fn set_rx_callback(&mut self, mut callback: C) -> Result<(), EspError> + pub fn set_rx_callback(&mut self, mut callback: F) -> Result<(), EspError> where - C: for<'a> FnMut(&[u8]) + Send + 'd, + F: FnMut(&[u8]) + Send + 'static, { let _ = self.stop(); diff --git a/src/eventloop.rs b/src/eventloop.rs index b7d28ba50b0..c0409da3ef4 100644 --- a/src/eventloop.rs +++ b/src/eventloop.rs @@ -32,9 +32,9 @@ pub use asyncify::*; #[cfg(all(feature = "alloc", esp_idf_comp_esp_timer_enabled))] pub use async_wait::*; -pub type EspSystemSubscription<'a> = EspSubscription<'a, System>; -pub type EspBackgroundSubscription<'a> = EspSubscription<'a, User>; -pub type EspExplicitSubscription<'a> = EspSubscription<'a, User>; +pub type EspSystemSubscription = EspSubscription; +pub type EspBackgroundSubscription = EspSubscription>; +pub type EspExplicitSubscription = EspSubscription>; pub type EspSystemEventLoop = EspEventLoop; pub type EspBackgroundEventLoop = EspEventLoop>; @@ -211,11 +211,11 @@ impl EspEventFetchData { } } -struct UnsafeCallback<'a>(*mut Box); +struct UnsafeCallback(*mut Box); -impl<'a> UnsafeCallback<'a> { +impl UnsafeCallback { #[allow(clippy::type_complexity)] - fn from(boxed: &mut Box>) -> Self { + fn from(boxed: &mut Box>) -> Self { Self(boxed.as_mut()) } @@ -234,7 +234,7 @@ impl<'a> UnsafeCallback<'a> { } } -pub struct EspSubscription<'a, T> +pub struct EspSubscription where T: EspEventLoopType, { @@ -243,10 +243,10 @@ where source: *const ffi::c_char, event_id: i32, #[allow(clippy::type_complexity)] - _callback: Box>, + _callback: Box>, } -impl<'a, T> EspSubscription<'a, T> +impl EspSubscription where T: EspEventLoopType, { @@ -268,9 +268,9 @@ where } } -unsafe impl<'a, T> Send for EspSubscription<'a, T> where T: EspEventLoopType {} +unsafe impl Send for EspSubscription where T: EspEventLoopType {} -impl<'a, T> Drop for EspSubscription<'a, T> +impl Drop for EspSubscription where T: EspEventLoopType, { @@ -301,7 +301,7 @@ where } } -impl<'a, T> RawHandle for EspSubscription<'a, User> +impl RawHandle for EspSubscription> where T: EspEventLoopType, { @@ -395,18 +395,18 @@ where T: EspEventLoopType, { #[allow(clippy::not_unsafe_ptr_arg_deref)] - pub fn subscribe_raw<'a, F>( + pub fn subscribe_raw( &self, source: *const ffi::c_char, event_id: i32, mut callback: F, - ) -> Result, EspError> + ) -> Result, EspError> where - F: FnMut(&EspEventFetchData) + Send + 'a, + F: FnMut(&EspEventFetchData) + Send + 'static, { let mut handler_instance: esp_event_handler_instance_t = ptr::null_mut(); - let callback: Box = + let callback: Box = Box::new(move |data| callback(data)); let mut callback = Box::new(callback); @@ -528,10 +528,10 @@ where } } - pub fn subscribe<'a, P, F>(&self, mut callback: F) -> Result, EspError> + pub fn subscribe(&self, mut callback: F) -> Result, EspError> where P: EspTypedEventDeserializer

, - F: FnMut(&P) + Send + 'a, + F: FnMut(&P) + Send + 'static, { self.subscribe_raw( P::source(), @@ -656,11 +656,11 @@ where P: EspTypedEventDeserializer

, T: EspEventLoopType, { - type Subscription<'a> = EspSubscription<'a, T> where Self: 'a; + type Subscription<'a> = EspSubscription where Self: 'a; fn subscribe<'a, F>(&'a self, callback: F) -> Result, Self::Error> where - F: FnMut(&P) + Send + 'a, + F: FnMut(&P) + Send + 'static, { EspEventLoop::subscribe(self, callback) } @@ -789,11 +789,11 @@ where M: EspTypedEventDeserializer

, T: EspEventLoopType, { - type Subscription<'a> = EspSubscription<'a, T> where Self: 'a; + type Subscription<'a> = EspSubscription where Self: 'a; fn subscribe<'a, F>(&'a self, mut callback: F) -> Result, EspError> where - F: FnMut(&P) + Send + 'a, + F: FnMut(&P) + Send + 'static, { self.untyped_event_loop.subscribe_raw( M::source(), @@ -808,11 +808,11 @@ where M: EspTypedEventDeserializer

, T: EspEventLoopType, { - type Subscription<'a> = EspSubscription<'a, T> where Self: 'a; + type Subscription<'a> = EspSubscription where Self: 'a; fn subscribe<'a, F>(&'a self, mut callback: F) -> Result, EspError> where - F: FnMut(&P) + Send + 'a, + F: FnMut(&P) + Send + 'static, { self.untyped_event_loop.subscribe_raw( M::source(), @@ -924,7 +924,7 @@ where E: EspTypedEventDeserializer + Debug, T: EspEventLoopType, { - pub fn new bool + Send + 'a>( + pub fn new bool + Send + 'static>( event_loop: &EspEventLoop, mut waiter: F, ) -> Result { diff --git a/src/http/server.rs b/src/http/server.rs index e6db39889fc..c2a4525dc31 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -240,19 +240,18 @@ impl From for Newtype { static OPEN_SESSIONS: Mutex>> = Mutex::wrap(RawMutex::new(), BTreeMap::new()); -static CLOSE_HANDLERS: Mutex>>> = +static CLOSE_HANDLERS: Mutex>> = Mutex::wrap(RawMutex::new(), BTreeMap::new()); -type NativeHandler<'a> = Box ffi::c_int + 'a>; -type CloseHandler<'a> = Box; +type NativeHandler = Box ffi::c_int + 'static>; +type CloseHandler = Box; -pub struct EspHttpServer<'a> { +pub struct EspHttpServer { sd: httpd_handle_t, registrations: Vec<(CString, crate::sys::httpd_uri_t)>, - _reg: PhantomData<&'a ()>, } -impl<'a> EspHttpServer<'a> { +impl EspHttpServer { pub fn new(conf: &Configuration) -> Result { let mut handle: httpd_handle_t = ptr::null_mut(); let handle_ref = &mut handle; @@ -317,7 +316,7 @@ impl<'a> EspHttpServer<'a> { conf.method ))?; - let _drop = Box::from_raw(conf.user_ctx as *mut NativeHandler<'static>); + let _drop = Box::from_raw(conf.user_ctx as *mut NativeHandler); }; info!( @@ -376,7 +375,7 @@ impl<'a> EspHttpServer<'a> { handler: H, ) -> Result<&mut Self, EspError> where - H: for<'r> Handler> + Send + 'a, + H: for<'r> Handler> + Send + 'static, { let c_str = to_cstring_arg(uri)?; @@ -404,14 +403,14 @@ impl<'a> EspHttpServer<'a> { pub fn fn_handler(&mut self, uri: &str, method: Method, f: F) -> Result<&mut Self, EspError> where - F: for<'r> Fn(Request<&mut EspHttpConnection<'r>>) -> HandlerResult + Send + 'a, + F: for<'r> Fn(Request<&mut EspHttpConnection<'r>>) -> HandlerResult + Send + 'static, { self.handler(uri, method, FnHandler::new(f)) } - fn to_native_handler(&self, handler: H) -> NativeHandler<'a> + fn to_native_handler(&self, handler: H) -> NativeHandler where - H: for<'r> Handler> + Send + 'a, + H: for<'r> Handler> + Send + 'static, { Box::new(move |raw_req| { let mut connection = EspHttpConnection::new(unsafe { raw_req.as_mut().unwrap() }); @@ -458,13 +457,13 @@ impl<'a> EspHttpServer<'a> { } } -impl<'a> Drop for EspHttpServer<'a> { +impl Drop for EspHttpServer { fn drop(&mut self) { self.stop().expect("Unable to stop the server cleanly"); } } -impl<'a> RawHandle for EspHttpServer<'a> { +impl RawHandle for EspHttpServer { type Handle = httpd_handle_t; fn handle(&self) -> Self::Handle { @@ -479,22 +478,22 @@ where FnHandler::new(f) } -pub trait EspHttpTraversableChain<'a> { - fn accept(self, server: &mut EspHttpServer<'a>) -> Result<(), EspError>; +pub trait EspHttpTraversableChain { + fn accept(self, server: &mut EspHttpServer) -> Result<(), EspError>; } -impl<'a> EspHttpTraversableChain<'a> for ChainRoot { - fn accept(self, _server: &mut EspHttpServer<'a>) -> Result<(), EspError> { +impl EspHttpTraversableChain for ChainRoot { + fn accept(self, _server: &mut EspHttpServer) -> Result<(), EspError> { Ok(()) } } -impl<'a, H, N> EspHttpTraversableChain<'a> for ChainHandler +impl EspHttpTraversableChain for ChainHandler where - H: for<'r> Handler> + 'a, + H: for<'r> Handler> + 'static, N: EspHttpTraversableChain<'a>, { - fn accept(self, server: &mut EspHttpServer<'a>) -> Result<(), EspError> { + fn accept(self, server: &mut EspHttpServer) -> Result<(), EspError> { self.next.accept(server)?; server.handler(self.path, self.method, self.handler)?; @@ -1256,10 +1255,10 @@ pub mod ws { } } - impl<'a> EspHttpServer<'a> { + impl EspHttpServer { pub fn ws_handler(&mut self, uri: &str, handler: H) -> Result<&mut Self, EspError> where - H: for<'r> Fn(&'r mut EspHttpWsConnection) -> Result<(), E> + Send + Sync + 'a, + H: for<'r> Fn(&'r mut EspHttpWsConnection) -> Result<(), E> + Send + Sync + 'static, E: Debug, { let c_str = to_cstring_arg(uri)?; @@ -1283,9 +1282,6 @@ pub mod ws { let close_handlers = all_close_handlers.get_mut(&(self.sd as u32)).unwrap(); - let close_handler: CloseHandler<'static> = - unsafe { core::mem::transmute(close_handler) }; - close_handlers.push(close_handler); } @@ -1304,7 +1300,7 @@ pub mod ws { handler: &H, ) -> Result<(), E> where - H: for<'b> Fn(&'b mut EspHttpWsConnection) -> Result<(), E> + Send + 'a, + H: for<'b> Fn(&'b mut EspHttpWsConnection) -> Result<(), E> + Send + 'static, E: Debug, { handler(connection)?; @@ -1325,9 +1321,9 @@ pub mod ws { &self, server_handle: httpd_handle_t, handler: H, - ) -> (NativeHandler<'a>, CloseHandler<'a>) + ) -> (NativeHandler, CloseHandler) where - H: for<'r> Fn(&'r mut EspHttpWsConnection) -> Result<(), E> + Send + Sync + 'a, + H: for<'r> Fn(&'r mut EspHttpWsConnection) -> Result<(), E> + Send + Sync + 'static, E: Debug, { let boxed_handler = Arc::new(move |mut connection: EspHttpWsConnection| { diff --git a/src/mqtt/client.rs b/src/mqtt/client.rs index 0f9ff51e31f..b51f9f63b43 100644 --- a/src/mqtt/client.rs +++ b/src/mqtt/client.rs @@ -332,10 +332,10 @@ impl<'a> TryFrom<&'a MqttClientConfiguration<'a>> } } -struct UnsafeCallback<'a>(*mut Box); +struct UnsafeCallback(*mut Box); -impl<'a> UnsafeCallback<'a> { - fn from(boxed: &mut Box>) -> Self { +impl UnsafeCallback { + fn from(boxed: &mut Box>) -> Self { Self(boxed.as_mut()) } @@ -354,14 +354,14 @@ impl<'a> UnsafeCallback<'a> { } } -pub struct EspMqttClient<'a, S = ()> { +pub struct EspMqttClient { raw_client: esp_mqtt_client_handle_t, conn_state_guard: Option>>, - _boxed_raw_callback: Box, + _boxed_raw_callback: Box, _tls_psk_conf: Option, } -impl<'a, S> RawHandle for EspMqttClient<'a, S> { +impl<'a, S> RawHandle for EspMqttClient { type Handle = esp_mqtt_client_handle_t; fn handle(&self) -> Self::Handle { @@ -369,7 +369,7 @@ impl<'a, S> RawHandle for EspMqttClient<'a, S> { } } -impl EspMqttClient<'static, ConnState> { +impl EspMqttClient> { pub fn new_with_conn( url: &str, conf: &MqttClientConfiguration, @@ -385,7 +385,7 @@ impl EspMqttClient<'static, ConnState> { } } -impl<'a, M, E> EspMqttClient<'a, ConnState> +impl EspMqttClient> where M: Send + 'static, E: Debug + Send + 'static, @@ -397,7 +397,7 @@ where &'b Result>, EspError>, ) -> Result, E> + Send - + 'a, + + 'static, ) -> Result<(Self, Connection), EspError> where Self: Sized, @@ -414,13 +414,13 @@ where } } -impl<'a> EspMqttClient<'a, ()> { +impl EspMqttClient<()> { pub fn new( url: &str, conf: &MqttClientConfiguration, callback: impl for<'b> FnMut(&'b Result>, EspError>) + Send - + 'a, + + 'static, ) -> Result where Self: Sized, @@ -429,14 +429,14 @@ impl<'a> EspMqttClient<'a, ()> { } } -impl<'a, S> EspMqttClient<'a, S> { +impl EspMqttClient { pub fn new_generic( url: &str, conf: &MqttClientConfiguration, conn_state_guard: Option>>, mut callback: impl for<'b> FnMut(&'b Result>, EspError>) + Send - + 'a, + + 'static, ) -> Result where Self: Sized, @@ -456,7 +456,7 @@ impl<'a, S> EspMqttClient<'a, S> { fn new_raw( url: &str, conf: &MqttClientConfiguration, - raw_callback: Box, + raw_callback: Box, conn_state_guard: Option>>, ) -> Result where @@ -630,7 +630,7 @@ impl<'a, S> EspMqttClient<'a, S> { } } -impl<'a, P> Drop for EspMqttClient<'a, P> { +impl

Drop for EspMqttClient

{ fn drop(&mut self) { let connection_state = self.conn_state_guard.take(); if let Some(connection_state) = connection_state { @@ -641,11 +641,11 @@ impl<'a, P> Drop for EspMqttClient<'a, P> { } } -impl<'a, P> ErrorType for EspMqttClient<'a, P> { +impl

ErrorType for EspMqttClient

{ type Error = EspError; } -impl<'a, P> client::Client for EspMqttClient<'a, P> { +impl

client::Client for EspMqttClient

{ fn subscribe( &mut self, topic: &str, @@ -659,7 +659,7 @@ impl<'a, P> client::Client for EspMqttClient<'a, P> { } } -impl<'a, P> client::Publish for EspMqttClient<'a, P> { +impl

client::Publish for EspMqttClient

{ fn publish( &mut self, topic: &str, @@ -671,7 +671,7 @@ impl<'a, P> client::Publish for EspMqttClient<'a, P> { } } -impl<'a, P> client::Enqueue for EspMqttClient<'a, P> { +impl

client::Enqueue for EspMqttClient

{ fn enqueue( &mut self, topic: &str, @@ -683,7 +683,7 @@ impl<'a, P> client::Enqueue for EspMqttClient<'a, P> { } } -unsafe impl<'a, P> Send for EspMqttClient<'a, P> {} +unsafe impl

Send for EspMqttClient

{} pub struct EspMqttMessage<'a> { event: &'a esp_mqtt_event_t, @@ -835,30 +835,30 @@ mod asyncify { use super::{EspMqttClient, EspMqttMessage, MqttClientConfiguration}; - impl<'a, P> UnblockingAsyncify for super::EspMqttClient<'a, P> { + impl

UnblockingAsyncify for super::EspMqttClient

{ type AsyncWrapper = AsyncClient>>; } - impl<'a, P> Asyncify for super::EspMqttClient<'a, P> { + impl

Asyncify for super::EspMqttClient

{ type AsyncWrapper = AsyncClient<(), Blocking>; } - pub type EspMqttAsyncClient<'a> = EspMqttConvertingAsyncClient<'a, MessageImpl, EspError>; + pub type EspMqttAsyncClient = EspMqttConvertingAsyncClient; - pub type EspMqttUnblockingAsyncClient<'a, U> = - EspMqttConvertingUnblockingAsyncClient<'a, U, MessageImpl, EspError>; + pub type EspMqttUnblockingAsyncClient = + EspMqttConvertingUnblockingAsyncClient; pub type EspMqttAsyncConnection = EspMqttConvertingAsyncConnection; - pub type EspMqttConvertingUnblockingAsyncClient<'a, U, M, E> = - AsyncClient>>>>; + pub type EspMqttConvertingUnblockingAsyncClient = + AsyncClient>>>>; - pub type EspMqttConvertingAsyncClient<'a, M, E> = - AsyncClient<(), EspMqttClient<'a, AsyncConnState>>; + pub type EspMqttConvertingAsyncClient = + AsyncClient<(), EspMqttClient>>; pub type EspMqttConvertingAsyncConnection = AsyncConnection; - impl EspMqttClient<'static, AsyncConnState> { + impl EspMqttClient> { pub fn new_with_async_conn( url: &str, conf: &MqttClientConfiguration, @@ -874,7 +874,7 @@ mod asyncify { } } - impl<'a, M, E> EspMqttClient<'a, AsyncConnState> + impl EspMqttClient> where M: Send + 'static, E: Debug + Send + 'static, @@ -886,7 +886,7 @@ mod asyncify { &'b Result>, EspError>, ) -> Result, E> + Send - + 'a, + + 'static, ) -> Result<(Self, EspMqttConvertingAsyncConnection), EspError> where Self: Sized, diff --git a/src/ping.rs b/src/ping.rs index c7c8e2f40ba..d2136356a82 100644 --- a/src/ping.rs +++ b/src/ping.rs @@ -36,7 +36,7 @@ impl EspPing { Ok(tracker.summary) } - pub fn ping_details( + pub fn ping_details( &mut self, ip: ipv4::Ipv4Addr, conf: &Configuration, @@ -54,7 +54,7 @@ impl EspPing { Ok(tracker.summary) } - fn run_ping( + fn run_ping( &self, ip: ipv4::Ipv4Addr, conf: &Configuration, @@ -122,7 +122,7 @@ impl EspPing { Ok(()) } - unsafe extern "C" fn on_ping_success( + unsafe extern "C" fn on_ping_success( handle: esp_ping_handle_t, args: *mut ffi::c_void, ) { @@ -196,7 +196,7 @@ impl EspPing { } } - unsafe extern "C" fn on_ping_timeout( + unsafe extern "C" fn on_ping_timeout( handle: esp_ping_handle_t, args: *mut ffi::c_void, ) { @@ -233,7 +233,7 @@ impl EspPing { } #[allow(clippy::mutex_atomic)] - unsafe extern "C" fn on_ping_end( + unsafe extern "C" fn on_ping_end( handle: esp_ping_handle_t, args: *mut ffi::c_void, ) { @@ -295,7 +295,7 @@ impl Ping for EspPing { EspPing::ping(self, ip, conf) } - fn ping_details( + fn ping_details( &mut self, ip: ipv4::Ipv4Addr, conf: &Configuration, diff --git a/src/sntp.rs b/src/sntp.rs index 545f0a9bb03..4d47c54e0ba 100644 --- a/src/sntp.rs +++ b/src/sntp.rs @@ -193,7 +193,7 @@ impl<'a> EspSntp<'a> { #[cfg(feature = "alloc")] pub fn new_with_callback(conf: &SntpConf, callback: F) -> Result where - F: FnMut(Duration) + Send + 'a, + F: FnMut(Duration) + Send + 'static, { let mut taken = TAKEN.lock(); @@ -201,12 +201,9 @@ impl<'a> EspSntp<'a> { esp!(ESP_ERR_INVALID_STATE)?; } - #[allow(clippy::type_complexity)] - let callback: alloc::boxed::Box = - alloc::boxed::Box::new(callback); #[allow(clippy::type_complexity)] let callback: alloc::boxed::Box = - unsafe { core::mem::transmute(callback) }; + alloc::boxed::Box::new(callback); *SYNC_CB.lock() = Some(callback); let sntp = Self::init(conf)?; diff --git a/src/timer.rs b/src/timer.rs index 691268977b5..d94068cd333 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -31,10 +31,10 @@ pub use isr::*; use crate::handle::RawHandle; -struct UnsafeCallback<'a>(*mut Box); +struct UnsafeCallback(*mut Box); -impl<'a> UnsafeCallback<'a> { - fn from(boxed: &mut Box) -> Self { +impl UnsafeCallback { + fn from(boxed: &mut Box) -> Self { Self(boxed) } @@ -53,12 +53,12 @@ impl<'a> UnsafeCallback<'a> { } } -pub struct EspTimer<'a> { +pub struct EspTimer { handle: esp_timer_handle_t, - _callback: Box, + _callback: Box, } -impl<'a> EspTimer<'a> { +impl EspTimer { pub fn is_scheduled(&self) -> Result { Ok(unsafe { esp_timer_is_active(self.handle) }) } @@ -112,9 +112,9 @@ impl<'a> EspTimer<'a> { } } -unsafe impl<'a> Send for EspTimer<'a> {} +unsafe impl Send for EspTimer {} -impl<'a> Drop for EspTimer<'a> { +impl Drop for EspTimer { fn drop(&mut self) { self.cancel().unwrap(); @@ -126,7 +126,7 @@ impl<'a> Drop for EspTimer<'a> { } } -impl<'a> RawHandle for EspTimer<'a> { +impl RawHandle for EspTimer { type Handle = esp_timer_handle_t; fn handle(&self) -> Self::Handle { @@ -134,11 +134,11 @@ impl<'a> RawHandle for EspTimer<'a> { } } -impl<'a> ErrorType for EspTimer<'a> { +impl ErrorType for EspTimer { type Error = EspError; } -impl<'a> timer::Timer for EspTimer<'a> { +impl timer::Timer for EspTimer { fn is_scheduled(&self) -> Result { EspTimer::is_scheduled(self) } @@ -148,13 +148,13 @@ impl<'a> timer::Timer for EspTimer<'a> { } } -impl<'a> OnceTimer for EspTimer<'a> { +impl OnceTimer for EspTimer { fn after(&mut self, duration: Duration) -> Result<(), Self::Error> { EspTimer::after(self, duration) } } -impl<'a> PeriodicTimer for EspTimer<'a> { +impl PeriodicTimer for EspTimer { fn every(&mut self, duration: Duration) -> Result<(), Self::Error> { EspTimer::every(self, duration) } @@ -185,13 +185,13 @@ where Duration::from_micros(unsafe { esp_timer_get_time() as _ }) } - pub fn timer<'a, F>(&self, callback: F) -> Result, EspError> + pub fn timer(&self, callback: F) -> Result where - F: FnMut() + Send + 'a, + F: FnMut() + Send + 'static, { let mut handle: esp_timer_handle_t = ptr::null_mut(); - let boxed_callback: Box = Box::new(callback); + let boxed_callback: Box = Box::new(callback); let mut callback = Box::new(boxed_callback); let unsafe_callback = UnsafeCallback::from(&mut callback); @@ -254,11 +254,11 @@ impl TimerService for EspTimerService where T: EspTimerServiceType, { - type Timer<'a> = EspTimer<'a> where Self: 'a; + type Timer<'a> = EspTimer where Self: 'a; fn timer<'a, F>(&'a self, callback: F) -> Result, Self::Error> where - F: FnMut() + Send + 'a, + F: FnMut() + Send + 'static, { EspTimerService::timer(self, callback) } @@ -327,7 +327,7 @@ pub mod embassy_time { use crate::timer::*; struct Alarm { - timer: Option>, + timer: Option, #[allow(clippy::type_complexity)] callback: Option<(fn(*mut ()), *mut ())>, } diff --git a/src/wifi.rs b/src/wifi.rs index a2f6a6550ff..6a4186d0272 100644 --- a/src/wifi.rs +++ b/src/wifi.rs @@ -880,34 +880,25 @@ impl<'d> WifiDriver<'d> { mut tx_callback: T, ) -> Result<(), EspError> where - R: FnMut(WifiDeviceId, &[u8]) -> Result<(), EspError> + Send + 'd, - T: FnMut(WifiDeviceId, &[u8], bool) + Send + 'd, + R: FnMut(WifiDeviceId, &[u8]) -> Result<(), EspError> + Send + 'static, + T: FnMut(WifiDeviceId, &[u8], bool) + Send + 'static, { let _ = self.disconnect(); let _ = self.stop(); #[allow(clippy::type_complexity)] let rx_callback: Box< - Box Result<(), EspError> + Send + 'd>, + Box Result<(), EspError> + Send + 'static>, > = Box::new(Box::new(move |device_id, data| { rx_callback(device_id, data) })); #[allow(clippy::type_complexity)] - let tx_callback: Box> = + let tx_callback: Box> = Box::new(Box::new(move |device_id, data, status| { tx_callback(device_id, data, status) })); - #[allow(clippy::type_complexity)] - let rx_callback: Box< - Box Result<(), EspError> + Send + 'static>, - > = unsafe { core::mem::transmute(rx_callback) }; - - #[allow(clippy::type_complexity)] - let tx_callback: Box> = - unsafe { core::mem::transmute(tx_callback) }; - unsafe { RX_CALLBACK = Some(rx_callback); TX_CALLBACK = Some(tx_callback); diff --git a/src/ws/client.rs b/src/ws/client.rs index 7de73848b33..4f3789185bf 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -285,11 +285,11 @@ impl<'a> TryFrom<&'a EspWebSocketClientConfig<'a>> for (esp_websocket_client_con } } -struct UnsafeCallback<'a>(*mut Box); +struct UnsafeCallback(*mut Box); -impl<'a> UnsafeCallback<'a> { +impl UnsafeCallback { fn from( - boxed: &mut Box>, + boxed: &mut Box>, ) -> Self { Self(boxed.as_mut()) } @@ -372,15 +372,15 @@ impl EspWebSocketPostbox { } } -pub struct EspWebSocketClient<'a> { +pub struct EspWebSocketClient { handle: esp_websocket_client_handle_t, // used for the timeout in every call to a send method in the c lib as the // `send` method in the `Sender` trait in embedded_svc::ws does not take a timeout itself timeout: TickType_t, - _callback: Box, + _callback: Box, } -impl EspWebSocketClient<'static> { +impl EspWebSocketClient { pub fn new_with_conn( uri: &str, config: &EspWebSocketClientConfig, @@ -400,14 +400,12 @@ impl EspWebSocketClient<'static> { Ok((client, EspWebSocketConnection(connection_state))) } -} -impl<'a> EspWebSocketClient<'a> { pub fn new( uri: &str, config: &EspWebSocketClientConfig, timeout: time::Duration, - mut callback: impl for<'r> FnMut(&'r Result, EspIOError>) + Send + 'a, + mut callback: impl for<'r> FnMut(&'r Result, EspIOError>) + Send + 'static, ) -> Result { Self::new_raw( uri, @@ -427,7 +425,7 @@ impl<'a> EspWebSocketClient<'a> { uri: &str, config: &EspWebSocketClientConfig, timeout: time::Duration, - raw_callback: Box, + raw_callback: Box, ) -> Result { let mut boxed_raw_callback = Box::new(raw_callback); let unsafe_callback = UnsafeCallback::from(&mut boxed_raw_callback);