From dbebddab7aa08b24dc96580b21ee762d5fbd0bf3 Mon Sep 17 00:00:00 2001 From: Aron Granberg Date: Sat, 27 Jul 2024 17:24:05 +0200 Subject: [PATCH] Make async mqtt client implement Send. There were already some Send implementations. However, they weren't completely correct, which made it impossible to use the async mqtt client in e.g. a tokio worker thread. Note that it could be used in the async main task, because that never sends off any work to other worker threads, and thus Send is not required. The errors would surface when trying to use tokio::spawn with an async mqtt client. --- src/private/unblocker.rs | 4 ++++ src/private/zerocopy.rs | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/private/unblocker.rs b/src/private/unblocker.rs index 821b96bd7ad..43c220e84e5 100644 --- a/src/private/unblocker.rs +++ b/src/private/unblocker.rs @@ -20,6 +20,10 @@ where task: TaskHandle_t, } +/// SAFETY: Unblocker uses a raw pointer in Unblocker::task. This causes rust to treat it as not Send. +/// However, this task handle is only ever used to delete the FreeRTOS task, which is safe to do from any thread. +unsafe impl Send for Unblocker {} + impl Unblocker where T: Send + 'static, diff --git a/src/private/zerocopy.rs b/src/private/zerocopy.rs index 9022a6834e7..dc4073c0b92 100644 --- a/src/private/zerocopy.rs +++ b/src/private/zerocopy.rs @@ -83,7 +83,7 @@ where } } -unsafe impl Send for Receiver where T: Send + 'static {} +unsafe impl<'a, T> Send for Receiver where T: Send + 'a {} pub struct QuitOnDrop(Arc>) where @@ -182,8 +182,8 @@ where } } -unsafe impl Send for Channel where T: Send + 'static {} -unsafe impl Sync for Channel where T: Send + 'static {} +unsafe impl<'a, T> Send for Channel where T: Send + 'a {} +unsafe impl<'a, T> Sync for Channel where T: Send + 'a {} #[derive(Copy, Clone, Debug)] enum State {