From 8f20e9b2706d5d87f2851257cc28b96dc1394ddf Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 15 Nov 2025 12:15:33 -0600 Subject: [PATCH] Fall back to Rc on targets without atomics `alloc::sync::Arc` is unavailable on targets without atomics, such as ESP32-C3, which is riscv32imc. In this case, use `Rc` instead, and make the user responsible for synchronization. --- src/mqtt/common/arc.rs | 26 ++++++++++++++++++++++++++ src/mqtt/common/arc_payload.rs | 3 ++- src/mqtt/common/mod.rs | 4 ++++ src/mqtt/connection/packet_builder.rs | 5 +++-- src/mqtt/mod.rs | 2 +- src/mqtt/packet/v3_1_1/publish.rs | 3 +-- src/mqtt/packet/v5_0/publish.rs | 3 +-- 7 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 src/mqtt/common/arc.rs diff --git a/src/mqtt/common/arc.rs b/src/mqtt/common/arc.rs new file mode 100644 index 0000000..c2b4a1b --- /dev/null +++ b/src/mqtt/common/arc.rs @@ -0,0 +1,26 @@ +// MIT License +// +// Copyright (c) 2025 Takatoshi Kondo +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#[cfg(target_has_atomic = "ptr")] +pub type Arc = alloc::sync::Arc; +#[cfg(not(target_has_atomic = "ptr"))] +pub type Arc = alloc::rc::Rc; diff --git a/src/mqtt/common/arc_payload.rs b/src/mqtt/common/arc_payload.rs index 46da01e..3adbadf 100644 --- a/src/mqtt/common/arc_payload.rs +++ b/src/mqtt/common/arc_payload.rs @@ -20,7 +20,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -use alloc::{string::String, sync::Arc, vec::Vec}; +use crate::mqtt::Arc; +use alloc::{string::String, vec::Vec}; use serde::{Serialize, Serializer}; // SSO buffer size configuration - priority-based selection for maximum size diff --git a/src/mqtt/common/mod.rs b/src/mqtt/common/mod.rs index 8755c84..65261cf 100644 --- a/src/mqtt/common/mod.rs +++ b/src/mqtt/common/mod.rs @@ -19,6 +19,10 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. + +mod arc; +pub use arc::Arc; + mod arc_payload; pub use arc_payload::{ArcPayload, IntoPayload}; diff --git a/src/mqtt/connection/packet_builder.rs b/src/mqtt/connection/packet_builder.rs index a8cda64..7c89456 100644 --- a/src/mqtt/connection/packet_builder.rs +++ b/src/mqtt/connection/packet_builder.rs @@ -19,9 +19,10 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -use crate::mqtt::common::Cursor; + +use crate::mqtt::common::{Arc, Cursor}; use crate::mqtt::result_code::MqttError; -use alloc::{sync::Arc, vec::Vec}; +use alloc::vec::Vec; #[derive(Debug, Clone)] pub enum PacketData { diff --git a/src/mqtt/mod.rs b/src/mqtt/mod.rs index 2732f8d..a02acdc 100644 --- a/src/mqtt/mod.rs +++ b/src/mqtt/mod.rs @@ -30,6 +30,6 @@ pub use connection::SendBehavior; pub use connection::Version; pub mod common; -pub use common::{ArcPayload, IntoPayload, ValueAllocator}; +pub use common::{Arc, ArcPayload, IntoPayload, ValueAllocator}; pub mod result_code; diff --git a/src/mqtt/packet/v3_1_1/publish.rs b/src/mqtt/packet/v3_1_1/publish.rs index 1e6c4dd..cab716b 100644 --- a/src/mqtt/packet/v3_1_1/publish.rs +++ b/src/mqtt/packet/v3_1_1/publish.rs @@ -20,7 +20,6 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -use alloc::sync::Arc; use alloc::vec::Vec; use core::fmt; use core::mem; @@ -42,7 +41,7 @@ use crate::mqtt::packet::GenericPacketDisplay; use crate::mqtt::packet::GenericPacketTrait; use crate::mqtt::packet::IsPacketId; use crate::mqtt::result_code::MqttError; -use crate::mqtt::{ArcPayload, IntoPayload}; +use crate::mqtt::{Arc, ArcPayload, IntoPayload}; /// MQTT 3.1.1 PUBLISH packet representation /// diff --git a/src/mqtt/packet/v5_0/publish.rs b/src/mqtt/packet/v5_0/publish.rs index 4b13b90..fcf7417 100644 --- a/src/mqtt/packet/v5_0/publish.rs +++ b/src/mqtt/packet/v5_0/publish.rs @@ -21,7 +21,6 @@ // SOFTWARE. use alloc::string::String; -use alloc::sync::Arc; use alloc::vec::Vec; use core::fmt; use core::mem; @@ -48,7 +47,7 @@ use crate::mqtt::packet::IsPacketId; use crate::mqtt::packet::PropertiesToBuffers; use crate::mqtt::packet::{Properties, PropertiesParse, PropertiesSize, Property}; use crate::mqtt::result_code::MqttError; -use crate::mqtt::{ArcPayload, IntoPayload}; +use crate::mqtt::{Arc, ArcPayload, IntoPayload}; /// MQTT 5.0 PUBLISH packet representation ///