@@ -66,6 +66,12 @@ func convertOptionalUserProperties(
6666 return userProperties
6767}
6868
69+ extension UserProperty : Equatable {
70+ static public func == ( lhs: UserProperty , rhs: UserProperty ) -> Bool {
71+ return lhs. name == rhs. name && lhs. value == rhs. value
72+ }
73+ }
74+
6975// We can't mutate this class after initialization. Swift can not verify the sendability due to the class is non-final,
7076// so mark it unchecked Sendable
7177/// Data model of an `MQTT5 PUBLISH <https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901100>`_ packet
@@ -494,7 +500,6 @@ public class UnsubscribePacket: CStruct, @unchecked Sendable {
494500 ) {
495501 self . topicFilters = topicFilters
496502 self . userProperties = userProperties
497- rawTopicFilters = convertTopicFilters ( self . topicFilters)
498503 }
499504
500505 // Allow an UnsubscribePacket to be created directly using a single topic filter
@@ -508,40 +513,15 @@ public class UnsubscribePacket: CStruct, @unchecked Sendable {
508513 typealias RawType = aws_mqtt5_packet_unsubscribe_view
509514 func withCStruct< Result> ( _ body: ( RawType ) -> Result ) -> Result {
510515 var raw_unsubscribe_view = aws_mqtt5_packet_unsubscribe_view ( )
511- raw_unsubscribe_view. topic_filters = UnsafePointer ( rawTopicFilters)
512- raw_unsubscribe_view. topic_filter_count = topicFilters. count
513- return userProperties. withAWSArrayList { userPropertyPointer in
514- raw_unsubscribe_view. user_property_count = userProperties. count
515- raw_unsubscribe_view. user_properties =
516- UnsafePointer < aws_mqtt5_user_property > ( userPropertyPointer)
517- return body ( raw_unsubscribe_view)
518- }
519- }
520-
521- func convertTopicFilters( _ topicFilters: [ String ] ) -> UnsafeMutablePointer < aws_byte_cursor > ? {
522- let cArray = UnsafeMutablePointer< aws_byte_cursor> . allocate( capacity: topicFilters. count)
523-
524- for (index, string) in topicFilters. enumerated ( ) {
525- let data = Data ( string. utf8)
526- let buffer = UnsafeMutablePointer< UInt8> . allocate( capacity: data. count)
527- data. copyBytes ( to: buffer, count: data. count)
528-
529- cArray [ index] = aws_byte_cursor ( len: data. count, ptr: buffer)
530- }
531-
532- return cArray
533- }
534-
535- /// storage of topic filter Strings converted into native c aws_byte_cursor pointer
536- private var rawTopicFilters : UnsafeMutablePointer < aws_byte_cursor > ?
537-
538- deinit {
539- /// Clean up memory of converted topic filter Strings
540- if let filters = rawTopicFilters {
541- for i in 0 ..< topicFilters. count {
542- filters [ i] . ptr. deallocate ( )
516+ return self . topicFilters. withByteCursorArray { byteCusorArray, len in
517+ raw_unsubscribe_view. topic_filters = byteCusorArray
518+ raw_unsubscribe_view. topic_filter_count = len
519+ return userProperties. withAWSArrayList { userPropertyPointer in
520+ raw_unsubscribe_view. user_property_count = userProperties. count
521+ raw_unsubscribe_view. user_properties =
522+ UnsafePointer < aws_mqtt5_user_property > ( userPropertyPointer)
523+ return body ( raw_unsubscribe_view)
543524 }
544- filters. deallocate ( )
545525 }
546526 }
547527}
0 commit comments