Struct esp_idf_svc::mqtt::client::EspMqttClient
source · pub struct EspMqttClient<'a> { /* private fields */ }
Implementations§
source§impl EspMqttClient<'static>
impl EspMqttClient<'static>
pub fn new(
url: &str,
conf: &MqttClientConfiguration<'_>,
) -> Result<(Self, EspMqttConnection), EspError>where
Self: Sized,
pub fn new_cb<F>(
url: &str,
conf: &MqttClientConfiguration<'_>,
callback: F,
) -> Result<Self, EspError>where
F: for<'b> FnMut(EspMqttEvent<'b>) + Send + 'static,
Self: Sized,
source§impl<'a> EspMqttClient<'a>
impl<'a> EspMqttClient<'a>
sourcepub unsafe fn new_nonstatic_cb<F>(
url: &str,
conf: &MqttClientConfiguration<'_>,
callback: F,
) -> Result<Self, EspError>where
F: for<'b> FnMut(EspMqttEvent<'b>) + Send + 'a,
Self: Sized,
pub unsafe fn new_nonstatic_cb<F>(
url: &str,
conf: &MqttClientConfiguration<'_>,
callback: F,
) -> Result<Self, EspError>where
F: for<'b> FnMut(EspMqttEvent<'b>) + Send + 'a,
Self: Sized,
§Safety
This method - in contrast to method new_generic
- allows the user to pass
a non-static callback/closure. This enables users to borrow
- in the closure - variables that live on the stack - or more generally - in the same scope where the service is created.
HOWEVER: care should be taken NOT to call core::mem::forget()
on the service,
as that would immediately lead to an UB (crash).
Also note that forgetting the service might happen with Rc
and Arc
when circular references are introduced: https://github.com/rust-lang/rust/issues/24456
The reason is that the closure is actually sent to a hidden ESP IDF thread. This means that if the service is forgotten, Rust is free to e.g. unwind the stack and the closure now owned by this other thread will end up with references to variables that no longer exist.
The destructor of the service takes care - prior to the service being dropped and e.g. the stack being unwind - to remove the closure from the hidden thread and destroy it. Unfortunately, when the service is forgotten, the un-subscription does not happen and invalid references are left dangling.
This “local borrowing” will only be possible to express in a safe way once/if !Leak
types
are introduced to Rust (i.e. the impossibility to “forget” a type and thus not call its destructor).