Skip to content

Replace almost all public static lets with computed vars #3229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
228 changes: 171 additions & 57 deletions Sources/NIOCore/BSDSocketAPI.swift

Large diffs are not rendered by default.

14 changes: 10 additions & 4 deletions Sources/NIOCore/ByteBuffer-hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,29 @@ extension ByteBuffer {
/// Can be either xxd output compatible, or hexdump compatible.
public struct HexDumpFormat: Hashable, Sendable {

enum Value: Hashable {
@usableFromInline
enum Value: Hashable, Sendable {
case plain(maxBytes: Int? = nil)
case detailed(maxBytes: Int? = nil)
case compact(maxBytes: Int? = nil)
}

@usableFromInline
let value: Value

@inlinable
init(_ value: Value) { self.value = value }

/// A plain hex dump format compatible with `xxd` CLI utility.
public static let plain = Self(.plain(maxBytes: nil))
@inlinable
public static var plain: HexDumpFormat { Self(.plain(maxBytes: nil)) }

/// A hex dump format compatible with `hexdump` command line utility.
public static let detailed = Self(.detailed(maxBytes: nil))
@inlinable
public static var detailed: HexDumpFormat { Self(.detailed(maxBytes: nil)) }

/// A hex dump analog to `plain` format but without whitespaces.
public static let compact = Self(.compact(maxBytes: nil))
public static var compact: HexDumpFormat { Self(.compact(maxBytes: nil)) }

/// A detailed hex dump format compatible with `xxd`, clipped to `maxBytes` bytes dumped.
/// This format will dump first `maxBytes / 2` bytes, and the last `maxBytes / 2` bytes, replacing the rest with " ... ".
Expand Down
7 changes: 5 additions & 2 deletions Sources/NIOCore/EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,11 @@ public struct NIODeadline: Equatable, Hashable, Sendable {
.init(self._uptimeNanoseconds)
}

public static let distantPast = NIODeadline(0)
public static let distantFuture = NIODeadline(.init(Int64.max))
@inlinable
public static var distantPast: NIODeadline { NIODeadline(0) }

@inlinable
public static var distantFuture: NIODeadline { NIODeadline(.init(Int64.max)) }

@inlinable init(_ nanoseconds: Int64) {
precondition(nanoseconds >= 0)
Expand Down
18 changes: 15 additions & 3 deletions Sources/NIOCore/FileHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ extension NIOFileHandle {
public struct Mode: OptionSet, Sendable {
public let rawValue: UInt8

@inlinable
public init(rawValue: UInt8) {
self.rawValue = rawValue
}
Expand All @@ -315,17 +316,28 @@ extension NIOFileHandle {
}

/// Opens file for reading
public static let read = Mode(rawValue: 1 << 0)
@inlinable
public static var read: Mode { Mode(rawValue: 1 << 0) }
/// Opens file for writing
public static let write = Mode(rawValue: 1 << 1)
@inlinable
public static var write: NIOFileHandle.Mode { Mode(rawValue: 1 << 1) }
}

/// `Flags` allows to specify additional flags to `Mode`, such as permission for file creation.
public struct Flags: Sendable {
@usableFromInline
internal var posixMode: NIOPOSIXFileMode

@usableFromInline
internal var posixFlags: CInt

public static let `default` = Flags(posixMode: 0, posixFlags: 0)
@inlinable
internal init(posixMode: NIOPOSIXFileMode, posixFlags: CInt) {
self.posixMode = posixMode
self.posixFlags = posixFlags
}

public static var `default`: Flags { Flags(posixMode: 0, posixFlags: 0) }

#if os(Windows)
public static let defaultPermissions = _S_IREAD | _S_IWRITE
Expand Down
210 changes: 168 additions & 42 deletions Sources/NIOCore/IPProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,89 +36,215 @@ extension NIOIPProtocol {
// Subset of https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml with an RFC
extension NIOIPProtocol {
/// IPv6 Hop-by-Hop Option - [RFC8200]
public static let hopopt = Self(rawValue: 0)
@inlinable
public static var hopopt: NIOIPProtocol {
Self(rawValue: 0)
}
/// Internet Control Message - [RFC792]
public static let icmp = Self(rawValue: 1)
@inlinable
public static var icmp: NIOIPProtocol {
Self(rawValue: 1)
}
/// Internet Group Management - [RFC1112]
public static let igmp = Self(rawValue: 2)
@inlinable
public static var igmp: NIOIPProtocol {
Self(rawValue: 2)
}
/// Gateway-to-Gateway - [RFC823]
public static let ggp = Self(rawValue: 3)
@inlinable
public static var ggp: NIOIPProtocol {
Self(rawValue: 3)
}
/// IPv4 encapsulation - [RFC2003]
public static let ipv4 = Self(rawValue: 4)
@inlinable
public static var ipv4: NIOIPProtocol {
Self(rawValue: 4)
}
/// Stream - [RFC1190][RFC1819]
public static let st = Self(rawValue: 5)
@inlinable
public static var st: NIOIPProtocol {
Self(rawValue: 5)
}
/// Transmission Control - [RFC9293]
public static let tcp = Self(rawValue: 6)
@inlinable
public static var tcp: NIOIPProtocol {
Self(rawValue: 6)
}
/// Exterior Gateway Protocol - [RFC888][David_Mills]
public static let egp = Self(rawValue: 8)
@inlinable
public static var egp: NIOIPProtocol {
Self(rawValue: 8)
}
/// Network Voice Protocol - [RFC741][Steve_Casner]
public static let nvpIi = Self(rawValue: 11)
@inlinable
public static var nvpIi: NIOIPProtocol {
Self(rawValue: 11)
}
/// User Datagram - [RFC768][Jon_Postel]
public static let udp = Self(rawValue: 17)
@inlinable
public static var udp: NIOIPProtocol {
Self(rawValue: 17)
}
/// Host Monitoring - [RFC869][Bob_Hinden]
public static let hmp = Self(rawValue: 20)
@inlinable
public static var hmp: NIOIPProtocol {
Self(rawValue: 20)
}
/// Reliable Data Protocol - [RFC908][Bob_Hinden]
public static let rdp = Self(rawValue: 27)
@inlinable
public static var rdp: NIOIPProtocol {
Self(rawValue: 27)
}
/// Internet Reliable Transaction - [RFC938][Trudy_Miller]
public static let irtp = Self(rawValue: 28)
@inlinable
public static var irtp: NIOIPProtocol {
Self(rawValue: 28)
}
/// ISO Transport Protocol Class 4 - [RFC905][<mystery contact>]
public static let isoTp4 = Self(rawValue: 29)
@inlinable
public static var isoTp4: NIOIPProtocol {
Self(rawValue: 29)
}
/// Bulk Data Transfer Protocol - [RFC969][David_Clark]
public static let netblt = Self(rawValue: 30)
@inlinable
public static var netblt: NIOIPProtocol {
Self(rawValue: 30)
}
/// Datagram Congestion Control Protocol - [RFC4340]
public static let dccp = Self(rawValue: 33)
@inlinable
public static var dccp: NIOIPProtocol {
Self(rawValue: 33)
}
/// IPv6 encapsulation - [RFC2473]
public static let ipv6 = Self(rawValue: 41)
@inlinable
public static var ipv6: NIOIPProtocol {
Self(rawValue: 41)
}
/// Reservation Protocol - [RFC2205][RFC3209][Bob_Braden]
public static let rsvp = Self(rawValue: 46)
@inlinable
public static var rsvp: NIOIPProtocol {
Self(rawValue: 46)
}
/// Generic Routing Encapsulation - [RFC2784][Tony_Li]
public static let gre = Self(rawValue: 47)
@inlinable
public static var gre: NIOIPProtocol {
Self(rawValue: 47)
}
/// Dynamic Source Routing Protocol - [RFC4728]
public static let dsr = Self(rawValue: 48)
@inlinable
public static var dsr: NIOIPProtocol {
Self(rawValue: 48)
}
/// Encap Security Payload - [RFC4303]
public static let esp = Self(rawValue: 50)
@inlinable
public static var esp: NIOIPProtocol {
Self(rawValue: 50)
}
/// Authentication Header - [RFC4302]
public static let ah = Self(rawValue: 51)
@inlinable
public static var ah: NIOIPProtocol {
Self(rawValue: 51)
}
/// NBMA Address Resolution Protocol - [RFC1735]
public static let narp = Self(rawValue: 54)
@inlinable
public static var narp: NIOIPProtocol {
Self(rawValue: 54)
}
/// ICMP for IPv6 - [RFC8200]
public static let ipv6Icmp = Self(rawValue: 58)
@inlinable
public static var ipv6Icmp: NIOIPProtocol {
Self(rawValue: 58)
}
/// No Next Header for IPv6 - [RFC8200]
public static let ipv6Nonxt = Self(rawValue: 59)
@inlinable
public static var ipv6Nonxt: NIOIPProtocol {
Self(rawValue: 59)
}
/// Destination Options for IPv6 - [RFC8200]
public static let ipv6Opts = Self(rawValue: 60)
@inlinable
public static var ipv6Opts: NIOIPProtocol {
Self(rawValue: 60)
}
/// EIGRP - [RFC7868]
public static let eigrp = Self(rawValue: 88)
@inlinable
public static var eigrp: NIOIPProtocol {
Self(rawValue: 88)
}
/// OSPFIGP - [RFC1583][RFC2328][RFC5340][John_Moy]
public static let ospfigp = Self(rawValue: 89)
@inlinable
public static var ospfigp: NIOIPProtocol {
Self(rawValue: 89)
}
/// Ethernet-within-IP Encapsulation - [RFC3378]
public static let etherip = Self(rawValue: 97)
@inlinable
public static var etherip: NIOIPProtocol {
Self(rawValue: 97)
}
/// Encapsulation Header - [RFC1241][Robert_Woodburn]
public static let encap = Self(rawValue: 98)
@inlinable
public static var encap: NIOIPProtocol {
Self(rawValue: 98)
}
/// Protocol Independent Multicast - [RFC7761][Dino_Farinacci]
public static let pim = Self(rawValue: 103)
@inlinable
public static var pim: NIOIPProtocol {
Self(rawValue: 103)
}
/// IP Payload Compression Protocol - [RFC2393]
public static let ipcomp = Self(rawValue: 108)
@inlinable
public static var ipcomp: NIOIPProtocol {
Self(rawValue: 108)
}
/// Virtual Router Redundancy Protocol - [RFC5798]
public static let vrrp = Self(rawValue: 112)
@inlinable
public static var vrrp: NIOIPProtocol {
Self(rawValue: 112)
}
/// Layer Two Tunneling Protocol - [RFC3931][Bernard_Aboba]
public static let l2tp = Self(rawValue: 115)
@inlinable
public static var l2tp: NIOIPProtocol {
Self(rawValue: 115)
}
/// Fibre Channel - [Murali_Rajagopal][RFC6172]
public static let fc = Self(rawValue: 133)
@inlinable
public static var fc: NIOIPProtocol {
Self(rawValue: 133)
}
/// MANET Protocols - [RFC5498]
public static let manet = Self(rawValue: 138)
@inlinable
public static var manet: NIOIPProtocol {
Self(rawValue: 138)
}
/// Host Identity Protocol - [RFC7401]
public static let hip = Self(rawValue: 139)
@inlinable
public static var hip: NIOIPProtocol {
Self(rawValue: 139)
}
/// Shim6 Protocol - [RFC5533]
public static let shim6 = Self(rawValue: 140)
@inlinable
public static var shim6: NIOIPProtocol {
Self(rawValue: 140)
}
/// Wrapped Encapsulating Security Payload - [RFC5840]
public static let wesp = Self(rawValue: 141)
@inlinable
public static var wesp: NIOIPProtocol {
Self(rawValue: 141)
}
/// Robust Header Compression - [RFC5858]
public static let rohc = Self(rawValue: 142)
@inlinable
public static var rohc: NIOIPProtocol {
Self(rawValue: 142)
}
/// Ethernet - [RFC8986]
public static let ethernet = Self(rawValue: 143)
@inlinable
public static var ethernet: NIOIPProtocol {
Self(rawValue: 143)
}
/// AGGFRAG encapsulation payload for ESP - [RFC-ietf-ipsecme-iptfs-19]
public static let aggfrag = Self(rawValue: 144)
@inlinable
public static var aggfrag: NIOIPProtocol {
Self(rawValue: 144)
}
}

extension NIOIPProtocol: CustomStringConvertible {
Expand Down
23 changes: 14 additions & 9 deletions Sources/NIOCore/IntegerTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ struct _UInt24: Sendable {
self._backing = IntegerBitPacking.unpackUInt16UInt8(value)
}

static let bitWidth: Int = 24
@inlinable
static var bitWidth: Int { 24 }

@usableFromInline
static let max: _UInt24 = .init((UInt32(1) << 24) - 1)
@inlinable
static var max: _UInt24 { .init((UInt32(1) << 24) - 1) }

@usableFromInline
static let min: _UInt24 = .init(0)
@inlinable
static var min: _UInt24 { .init(0) }
}

extension UInt32 {
Expand Down Expand Up @@ -74,11 +75,15 @@ struct _UInt56: Sendable {
self._backing = IntegerBitPacking.unpackUInt32UInt16UInt8(value)
}

static let bitWidth: Int = 56
@inlinable
static var bitWidth: Int { 56 }

private static let initializeUInt64: UInt64 = (1 << 56) - 1
static let max: _UInt56 = .init(initializeUInt64)
static let min: _UInt56 = .init(0)
@inlinable
static var initializeUInt64: UInt64 { (1 << 56) - 1 }
@inlinable
static var max: _UInt56 { .init(initializeUInt64) }
@inlinable
static var min: _UInt56 { .init(0) }
}

extension _UInt56 {
Expand Down
Loading
Loading