Skip to content

Commit

Permalink
Add support for typed throws
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Nov 14, 2024
1 parent 3e64960 commit 97c1683
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Sources/Socket/System/CInternetAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal extension CInternetAddress {
internal extension String {

@usableFromInline
init<T: CInternetAddress>(_ cInternetAddress: T) throws {
init<T: CInternetAddress>(_ cInternetAddress: T) throws(Errno) {
let cString = UnsafeMutablePointer<CChar>.allocate(capacity: T.stringLength)
defer { cString.deallocate() }
let success = withUnsafePointer(to: cInternetAddress) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Socket/System/CInterop.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SystemPackage

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
import Glibc
Expand Down Expand Up @@ -65,7 +65,7 @@ public extension CInterop {
/// The C `sockaddr_in6` type
typealias IPv6SocketAddress = sockaddr_in6

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
/// The C `sockaddr_dl` type
typealias LinkLayerAddress = sockaddr_dl
#elseif os(Linux)
Expand Down
18 changes: 9 additions & 9 deletions Sources/Socket/System/Constants.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SystemPackage

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(Android)
import Glibc
Expand Down Expand Up @@ -34,7 +34,7 @@ internal var _O_NONBLOCK: CInt { O_NONBLOCK }
@_alwaysEmitIntoClient
internal var _O_APPEND: CInt { O_APPEND }

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
@_alwaysEmitIntoClient
internal var _O_SHLOCK: CInt { O_SHLOCK }

Expand All @@ -60,7 +60,7 @@ internal var _O_TRUNC: CInt { O_TRUNC }
@_alwaysEmitIntoClient
internal var _O_EXCL: CInt { O_EXCL }

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
@_alwaysEmitIntoClient
internal var _O_EVTONLY: CInt { O_EVTONLY }
#endif
Expand All @@ -74,7 +74,7 @@ internal var _O_NOCTTY: CInt { O_NOCTTY }
internal var _O_DIRECTORY: CInt { O_DIRECTORY }
#endif

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
@_alwaysEmitIntoClient
internal var _O_SYMLINK: CInt { O_SYMLINK }
#endif
Expand All @@ -93,7 +93,7 @@ internal var _SEEK_CUR: CInt { SEEK_CUR }
@_alwaysEmitIntoClient
internal var _SEEK_END: CInt { SEEK_END }

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
@_alwaysEmitIntoClient
internal var _SEEK_HOLE: CInt { SEEK_HOLE }

Expand Down Expand Up @@ -196,7 +196,7 @@ internal var _AF_VSOCK: CInt { AF_VSOCK }
internal var _AF_ISDN: CInt { AF_ISDN }
#endif

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
@_alwaysEmitIntoClient
internal var _AF_IMPLINK: CInt { AF_IMPLINK }

Expand Down Expand Up @@ -341,7 +341,7 @@ internal var _SO_DONTROUTE: CInt { SO_DONTROUTE }
@_alwaysEmitIntoClient
internal var _SO_BROADCAST: CInt { SO_BROADCAST }

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
@_alwaysEmitIntoClient
internal var _SO_USELOOPBACK: CInt { SO_USELOOPBACK }
#endif
Expand Down Expand Up @@ -381,7 +381,7 @@ internal var _MSG_WAITALL: CInt { numericCast(MSG_WAITALL) } /* wait for full re
@_alwaysEmitIntoClient
internal var _MSG_DONTWAIT: CInt { numericCast(MSG_DONTWAIT) } /* this message should be nonblocking */

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
@_alwaysEmitIntoClient
internal var _MSG_EOF: CInt { numericCast(MSG_EOF) } /* data completes connection */

Expand Down Expand Up @@ -416,7 +416,7 @@ internal var _MSG_MORE: CInt { numericCast(MSG_MORE) }

@_alwaysEmitIntoClient
internal var _fd_set_count: Int {
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
// __DARWIN_FD_SETSIZE is number of *bits*, so divide by number bits in each element to get element count
// at present this is 1024 / 32 == 32
return Int(__DARWIN_FD_SETSIZE) / 32
Expand Down
10 changes: 5 additions & 5 deletions Sources/Socket/System/FileChange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public extension SocketDescriptor {
func duplicate(
closeOnExec: Bool,
retryOnInterrupt: Bool = true
) throws -> FileDescriptor {
) throws(Errno) -> FileDescriptor {
let fileDescriptor = try _change(
closeOnExec ? .duplicateCloseOnExec : .duplicate,
self.rawValue,
Expand All @@ -18,7 +18,7 @@ public extension SocketDescriptor {

/// Get Flags
@_alwaysEmitIntoClient
func getFlags(retryOnInterrupt: Bool = true) throws -> FileDescriptor.Flags {
func getFlags(retryOnInterrupt: Bool = true) throws(Errno) -> FileDescriptor.Flags {
let rawValue = try _change(
.getFileDescriptorFlags,
retryOnInterrupt: retryOnInterrupt
Expand All @@ -31,7 +31,7 @@ public extension SocketDescriptor {
func setFlags(
_ newValue: FileDescriptor.Flags,
retryOnInterrupt: Bool = true
) throws {
) throws(Errno) {
let _ = try _change(
.setFileDescriptorFlags,
newValue.rawValue,
Expand All @@ -41,7 +41,7 @@ public extension SocketDescriptor {

/// Get Status
@_alwaysEmitIntoClient
func getStatus(retryOnInterrupt: Bool = true) throws -> FileDescriptor.OpenOptions {
func getStatus(retryOnInterrupt: Bool = true) throws(Errno) -> FileDescriptor.OpenOptions {
let rawValue = try _change(
.getStatusFlags,
retryOnInterrupt: retryOnInterrupt
Expand All @@ -54,7 +54,7 @@ public extension SocketDescriptor {
func setStatus(
_ newValue: FileDescriptor.OpenOptions,
retryOnInterrupt: Bool = true
) throws {
) throws(Errno) {
let _ = try _change(
.setStatusFlags,
newValue.rawValue,
Expand Down
6 changes: 3 additions & 3 deletions Sources/Socket/System/InputOutput/IOOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extension SocketDescriptor {
public func inputOutput<T: IOControlID>(
_ request: T,
retryOnInterrupt: Bool = true
) throws {
) throws(Errno) {
try _inputOutput(request, retryOnInterrupt: true).get()
}

Expand All @@ -27,7 +27,7 @@ extension SocketDescriptor {
public func inputOutput<T: IOControlInteger>(
_ request: T,
retryOnInterrupt: Bool = true
) throws {
) throws(Errno) {
try _inputOutput(request, retryOnInterrupt: retryOnInterrupt).get()
}

Expand All @@ -47,7 +47,7 @@ extension SocketDescriptor {
public func inputOutput<T: IOControlValue>(
_ request: inout T,
retryOnInterrupt: Bool = true
) throws {
) throws(Errno) {
try _inputOutput(&request, retryOnInterrupt: retryOnInterrupt).get()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Socket/System/NetworkInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public struct NetworkInterfaceID: Equatable, Hashable {
public extension NetworkInterfaceID {

static var interfaces: [NetworkInterfaceID] {
get throws {
get throws(Errno) {
// get null terminated list
guard let pointer = system_if_nameindex() else {
throw Errno.current
Expand Down
4 changes: 2 additions & 2 deletions Sources/Socket/System/Poll.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extension SocketDescriptor {
for events: FileEvents,
timeout: Int = 0,
retryOnInterrupt: Bool = true
) throws -> FileEvents {
) throws(Errno) -> FileEvents {
try _poll(
events: events,
timeout: CInt(timeout),
Expand Down Expand Up @@ -128,7 +128,7 @@ extension Array where Element == SocketDescriptor.Poll {
public mutating func poll(
timeout: Int = 0,
retryOnInterrupt: Bool = true
) throws {
) throws(Errno) {
guard isEmpty == false else { return }
try SocketDescriptor._poll(&self, timeout: CInt(timeout), retryOnInterrupt: retryOnInterrupt).get()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Alsey Coleman Miller on 10/1/22.
//

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(Linux)
#if canImport(Darwin) || os(Linux)
import Foundation
import SystemPackage
@_implementationOnly import CSocket
Expand All @@ -15,7 +15,7 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable {

public typealias ProtocolID = LinkLayerProtocol

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
/// Index type
public typealias Index = UInt16

Expand All @@ -34,7 +34,7 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable {
public let address: String

internal init(_ cValue: CSocketAddressType) {
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
let index = cValue.sdl_index
let address = Swift.withUnsafePointer(to: cValue) {
String(cString: system_link_ntoa($0))
Expand All @@ -57,7 +57,7 @@ public struct LinkLayerSocketAddress: SocketAddress, Equatable, Hashable {
) throws -> Result) rethrows -> Result {

var socketAddress = CSocketAddressType()
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
socketAddress.sdl_index = index
self.address.withCString {
system_link_addr($0, &socketAddress)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Socket/System/SocketAddressFamily.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public extension SocketAddressFamily {
}
#endif

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
#if canImport(Darwin)
public extension SocketAddressFamily {

/// NetBIOS protocol
Expand Down
Loading

0 comments on commit 97c1683

Please sign in to comment.