Skip to content

Commit c1d8a15

Browse files
authored
Sendable conformance (#104)
* Setup Sendable conformance * swift format * Don't need _MQTTSendableProtocol * Revert var -> let so we don't make breaking change * Remove unnecessary @preconcurrency * swift format * comment
1 parent 6ac61fe commit c1d8a15

File tree

9 files changed

+56
-23
lines changed

9 files changed

+56
-23
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the MQTTNIO project
4+
//
5+
// Copyright (c) 2020-2021 Adam Fowler
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#if compiler(>=5.6)
15+
public typealias _MQTTSendable = Sendable
16+
#else
17+
public typealias _MQTTSendable = Any
18+
#endif

Sources/MQTTNIO/MQTTClient.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,9 @@ extension Logger {
739739
return logger
740740
}
741741
}
742+
743+
#if compiler(>=5.6)
744+
// All public members of the class are immutable and the class manages access to the
745+
// internal mutable state via Locks
746+
extension MQTTClient: @unchecked Sendable {}
747+
#endif

Sources/MQTTNIO/MQTTCoreTypes.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import NIO
14+
#if compiler(>=5.6)
15+
@preconcurrency import NIOCore
16+
#else
17+
import NIOCore
18+
#endif
1519

16-
public enum MQTTQoS: UInt8 {
20+
public enum MQTTQoS: UInt8, _MQTTSendable {
1721
/// fire and forget
1822
case atMostOnce = 0
1923
/// wait for PUBACK, if you don't receive it after a period of time retry sending
@@ -22,7 +26,7 @@ public enum MQTTQoS: UInt8 {
2226
case exactlyOnce = 2
2327
}
2428

25-
public enum MQTTPacketType: UInt8 {
29+
public enum MQTTPacketType: UInt8, _MQTTSendable {
2630
case CONNECT = 0x10
2731
case CONNACK = 0x20
2832
case PUBLISH = 0x30
@@ -41,7 +45,7 @@ public enum MQTTPacketType: UInt8 {
4145
}
4246

4347
/// MQTT PUBLISH packet parameters.
44-
public struct MQTTPublishInfo {
48+
public struct MQTTPublishInfo: _MQTTSendable {
4549
/// Quality of Service for message.
4650
public let qos: MQTTQoS
4751

@@ -73,7 +77,7 @@ public struct MQTTPublishInfo {
7377
}
7478

7579
/// MQTT SUBSCRIBE packet parameters.
76-
public struct MQTTSubscribeInfo {
80+
public struct MQTTSubscribeInfo: _MQTTSendable {
7781
/// Topic filter to subscribe to.
7882
public let topicFilter: String
7983

@@ -89,8 +93,8 @@ public struct MQTTSubscribeInfo {
8993
/// MQTT Sub ACK
9094
///
9195
/// Contains data returned in subscribe ack packets
92-
public struct MQTTSuback {
93-
public enum ReturnCode: UInt8 {
96+
public struct MQTTSuback: _MQTTSendable {
97+
public enum ReturnCode: UInt8, _MQTTSendable {
9498
case grantedQoS0 = 0
9599
case grantedQoS1 = 1
96100
case grantedQoS2 = 2

Sources/MQTTNIO/MQTTCoreTypesV5.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import NIO
15-
1614
/// MQTT V5 Connack
17-
public struct MQTTConnackV5 {
15+
public struct MQTTConnackV5: _MQTTSendable {
1816
/// is using session state from previous session
1917
public let sessionPresent: Bool
2018
/// connect reason code
@@ -24,7 +22,7 @@ public struct MQTTConnackV5 {
2422
}
2523

2624
/// MQTT V5 ACK information. Returned with PUBACK, PUBREL
27-
public struct MQTTAckV5 {
25+
public struct MQTTAckV5: _MQTTSendable {
2826
/// MQTT v5 reason code
2927
public let reason: MQTTReasonCode
3028
/// MQTT v5 properties
@@ -37,9 +35,9 @@ public struct MQTTAckV5 {
3735
}
3836

3937
/// MQTT SUBSCRIBE packet parameters.
40-
public struct MQTTSubscribeInfoV5 {
38+
public struct MQTTSubscribeInfoV5: _MQTTSendable {
4139
/// Retain handling options
42-
public enum RetainHandling: UInt8 {
40+
public enum RetainHandling: UInt8, _MQTTSendable {
4341
/// always send retain message
4442
case sendAlways = 0
4543
/// send retain if new
@@ -81,7 +79,7 @@ public struct MQTTSubscribeInfoV5 {
8179
/// MQTT V5 Sub ACK
8280
///
8381
/// Contains data returned in subscribe/unsubscribe ack packets
84-
public struct MQTTSubackV5 {
82+
public struct MQTTSubackV5: _MQTTSendable {
8583
/// MQTT v5 subscription reason code
8684
public let reasons: [MQTTReasonCode]
8785
/// MQTT v5 properties
@@ -96,7 +94,7 @@ public struct MQTTSubackV5 {
9694
/// MQTT V5 Sub ACK
9795
///
9896
/// Contains data returned in subscribe/unsubscribe ack packets
99-
public struct MQTTAuthV5 {
97+
public struct MQTTAuthV5: _MQTTSendable {
10098
/// MQTT v5 authentication reason code
10199
public let reason: MQTTReasonCode
102100
/// MQTT v5 properties

Sources/MQTTNIO/MQTTError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/// MQTTClient errors
1515
public enum MQTTError: Error {
1616
/// Value returned in connection error
17-
public enum ConnectionReturnValue: UInt8 {
17+
public enum ConnectionReturnValue: UInt8, _MQTTSendable {
1818
/// connection was accepted
1919
case accepted = 0
2020
/// The Server does not support the version of the MQTT protocol requested by the Client.

Sources/MQTTNIO/MQTTPacket.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import NIO
14+
#if compiler(>=5.6)
15+
@preconcurrency import NIOCore
16+
#else
17+
import NIOCore
18+
#endif
1519

1620
internal enum InternalError: Swift.Error {
1721
case incompletePacket
1822
case notImplemented
1923
}
2024

2125
/// Protocol for all MQTT packet types
22-
protocol MQTTPacket: CustomStringConvertible {
26+
protocol MQTTPacket: CustomStringConvertible, _MQTTSendable {
2327
/// packet type
2428
var type: MQTTPacketType { get }
2529
/// packet id (default to zero if not used)

Sources/MQTTNIO/MQTTProperties.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
import NIO
14+
#if compiler(>=5.6)
15+
@preconcurrency import NIOCore
16+
#else
17+
import NIOCore
18+
#endif
1519

1620
/// MQTT v5.0 properties. A property consists of a identifier and a value
17-
public struct MQTTProperties {
21+
public struct MQTTProperties: _MQTTSendable {
1822
/// MQTT Property
19-
public enum Property: Equatable {
23+
public enum Property: Equatable, _MQTTSendable {
2024
/// Payload format: 0 = bytes, 1 = UTF8 string (available for PUBLISH)
2125
case payloadFormat(UInt8)
2226
/// Message expiry indicates the lifetime of the message (available for PUBLISH)

Sources/MQTTNIO/MQTTReason.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/// Reason codes less than 128 are considered successful. Codes greater than or equal to 128 are considered
1616
/// a failure. These are returned by CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, DISCONNECT and
1717
/// AUTH packets
18-
public enum MQTTReasonCode: UInt8 {
18+
public enum MQTTReasonCode: UInt8, _MQTTSendable {
1919
/// Success (available for all). For SUBACK mean QoS0 is available
2020
case success = 0
2121
/// The subscription is accepted and the maximum QoS sent will be QoS 1. This might be a lower QoS than was requested.

Tests/MQTTNIOTests/MQTTNIOTests+async.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#if compiler(>=5.5) && canImport(_Concurrency)
1515

16-
import Foundation
1716
import Logging
1817
import NIO
1918
import NIOConcurrencyHelpers

0 commit comments

Comments
 (0)