Skip to content

Commit

Permalink
remove public setters from Message
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoward committed May 19, 2024
1 parent 6da547c commit d5ceaff
Showing 1 changed file with 11 additions and 37 deletions.
48 changes: 11 additions & 37 deletions Sources/IORing/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,17 @@ public struct Control {
public final class Message: @unchecked Sendable {
// FIXME: again, this is a workaround for _XOPEN_SOURCE=500 clang importer issues
public var name: [UInt8] {
get {
withUnsafeBytes(of: address) {
Array($0)
}
}
set {
address = try! sockaddr_storage(bytes: newValue)
}
}

public var address: sockaddr_storage {
didSet {
Swift.withUnsafeMutablePointer(to: &address) { pointer in
storage.msg_name = UnsafeMutableRawPointer(pointer)
storage.msg_namelen = (try? pointer.pointee.size) ?? 0
}
withUnsafeBytes(of: address) {
Array($0)
}
}

public var buffer: [UInt8] {
didSet {
buffer.withUnsafeMutableBytes { bytes in
iov_storage.iov_base = bytes.baseAddress
iov_storage.iov_len = bytes.count
}
}
}

public var control = [Control]()
public private(set) var address: sockaddr_storage
public private(set) var buffer: [UInt8]
public private(set) var control = [Control]()

public var flags: UInt32 {
get {
UInt32(storage.msg_flags)
}
set {
storage.msg_flags = Int32(newValue)
}
UInt32(storage.msg_flags)
}

private var storage = msghdr()
Expand All @@ -90,11 +64,11 @@ public final class Message: @unchecked Sendable {
init(address: sockaddr_storage, buffer: [UInt8] = [], flags: UInt32 = 0) {
self.address = address
self.buffer = buffer
self.flags = flags
self.buffer.withUnsafeMutableBytes { bytes in
iov_storage.iov_base = bytes.baseAddress
iov_storage.iov_len = bytes.count
storage.msg_flags = CInt(flags)
Swift.withUnsafeMutablePointer(to: &self.buffer) {
iov_storage.iov_base = UnsafeMutableRawPointer(mutating: $0)
}
iov_storage.iov_len = self.buffer.count
Swift.withUnsafeMutablePointer(to: &self.address) { pointer in
storage.msg_name = UnsafeMutableRawPointer(pointer)
storage.msg_namelen = (try? pointer.pointee.size) ?? 0
Expand All @@ -120,7 +94,7 @@ public final class Message: @unchecked Sendable {
} else {
sockaddr_storage()
}
self.init(address: ss, buffer: buffer, flags: 0)
self.init(address: ss, buffer: buffer, flags: flags)
}

public convenience init(capacity: Int, flags: UInt32 = 0) {
Expand Down

0 comments on commit d5ceaff

Please sign in to comment.