Struct esp_idf_svc::timer::EspTimerService
source · pub struct EspTimerService<T>(/* private fields */)
where
T: EspTimerServiceType;
Implementations§
source§impl<T> EspTimerService<T>where
T: EspTimerServiceType,
impl<T> EspTimerService<T>where
T: EspTimerServiceType,
pub fn now(&self) -> Duration
pub fn timer<F>(&self, callback: F) -> Result<EspTimer<'static>, EspError>where
F: FnMut() + Send + 'static,
pub fn timer_async(&self) -> Result<EspAsyncTimer, EspError>
sourcepub unsafe fn timer_nonstatic<'a, F>(
&self,
callback: F,
) -> Result<EspTimer<'a>, EspError>where
F: FnMut() + Send + 'a,
pub unsafe fn timer_nonstatic<'a, F>(
&self,
callback: F,
) -> Result<EspTimer<'a>, EspError>where
F: FnMut() + Send + 'a,
§Safety
This method - in contrast to method timer
- 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).
Trait Implementations§
source§impl<T> Clone for EspTimerService<T>where
T: EspTimerServiceType + Clone,
impl<T> Clone for EspTimerService<T>where
T: EspTimerServiceType + Clone,
Auto Trait Implementations§
impl<T> Freeze for EspTimerService<T>where
T: Freeze,
impl<T> RefUnwindSafe for EspTimerService<T>where
T: RefUnwindSafe,
impl<T> Send for EspTimerService<T>where
T: Send,
impl<T> Sync for EspTimerService<T>where
T: Sync,
impl<T> Unpin for EspTimerService<T>where
T: Unpin,
impl<T> UnwindSafe for EspTimerService<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)