Adopt swift-format (#2794)

* Apply formatting

* Apply no block comments rule

* Apply OmitExplicitReturns

* Apple OnlyOneTrailingClosureArgument

* Apply NoAssignmentInExpressions

* Fix up DontRepeatTypeInStaticProperties lint errors

* Apply `OrderedImports`

* Apply `ReplaceForEachWithForLoop`

* format file

* Enable the formatting pipeline

* Adopt `AmbiguousTrailingClosureOverload`

* Fix license header

* Fix format check

* Fix `EndOfLineComment`

* Fix CI

* Adapt CI script to check if changes when running formatting

* Separate lint and format into to steps

* Fix format

* Adopt `UseEarlyExits`

* Revert "Adopt `UseEarlyExits`"

This reverts commit d1ac5bbe12.
This commit is contained in:
Franz Busch authored and GitHub committed 2024-07-19 11:48:17 +02:00
1 parent 980bd3e630
commit c9756e1083
374 files changed
+23992 -14890

No files matched your search

+45 -34
View File
@@ -14,12 +14,12 @@
import NIOCore
private extension UInt8 {
func isAnyBitSetInMask(_ mask: UInt8) -> Bool {
return self & mask != 0
extension UInt8 {
fileprivate func isAnyBitSetInMask(_ mask: UInt8) -> Bool {
self & mask != 0
}
mutating func changingBitsInMask(_ mask: UInt8, to: Bool) {
fileprivate mutating func changingBitsInMask(_ mask: UInt8, to: Bool) {
if to {
self |= mask
} else {
@@ -42,10 +42,12 @@ public struct WebSocketMaskingKey: Sendable {
return nil
}
self._key = (buffer[buffer.startIndex],
buffer[buffer.index(buffer.startIndex, offsetBy: 1)],
buffer[buffer.index(buffer.startIndex, offsetBy: 2)],
buffer[buffer.index(buffer.startIndex, offsetBy: 3)])
self._key = (
buffer[buffer.startIndex],
buffer[buffer.index(buffer.startIndex, offsetBy: 1)],
buffer[buffer.index(buffer.startIndex, offsetBy: 2)],
buffer[buffer.index(buffer.startIndex, offsetBy: 3)]
)
}
/// Creates a websocket masking key from the network-encoded
@@ -56,10 +58,12 @@ public struct WebSocketMaskingKey: Sendable {
/// masking key.
@usableFromInline
internal init(networkRepresentation integer: UInt32) {
self._key = (UInt8((integer & 0xFF000000) >> 24),
UInt8((integer & 0x00FF0000) >> 16),
UInt8((integer & 0x0000FF00) >> 8),
UInt8(integer & 0x000000FF))
self._key = (
UInt8((integer & 0xFF00_0000) >> 24),
UInt8((integer & 0x00FF_0000) >> 16),
UInt8((integer & 0x0000_FF00) >> 8),
UInt8(integer & 0x0000_00FF)
)
}
}
@@ -68,7 +72,7 @@ extension WebSocketMaskingKey: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: UInt8...) {
precondition(elements.count == 4, "WebSocketMaskingKeys must be exactly 4 bytes long")
self.init(elements)! // length precondition above
self.init(elements)! // length precondition above
}
}
@@ -81,9 +85,9 @@ extension WebSocketMaskingKey {
public static func random<Generator>(
using generator: inout Generator
) -> WebSocketMaskingKey where Generator: RandomNumberGenerator {
return WebSocketMaskingKey(networkRepresentation: .random(in: UInt32.min...UInt32.max, using: &generator))
WebSocketMaskingKey(networkRepresentation: .random(in: UInt32.min...UInt32.max, using: &generator))
}
/// Returns a random masking key, using the `SystemRandomNumberGenerator` as a source for randomness.
/// - Returns: A random masking key
@inlinable
@@ -94,8 +98,8 @@ extension WebSocketMaskingKey {
}
extension WebSocketMaskingKey: Equatable {
public static func ==(lhs: WebSocketMaskingKey, rhs: WebSocketMaskingKey) -> Bool {
return lhs._key == rhs._key
public static func == (lhs: WebSocketMaskingKey, rhs: WebSocketMaskingKey) -> Bool {
lhs._key == rhs._key
}
}
@@ -103,11 +107,11 @@ extension WebSocketMaskingKey: Collection {
public typealias Element = UInt8
public typealias Index = Int
public var startIndex: Int { return 0 }
public var endIndex: Int { return 4 }
public var startIndex: Int { 0 }
public var endIndex: Int { 4 }
public func index(after: Int) -> Int {
return after + 1
after + 1
}
public subscript(index: Int) -> UInt8 {
@@ -127,7 +131,7 @@ extension WebSocketMaskingKey: Collection {
@inlinable
public func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R? {
return try withUnsafeBytes(of: self._key) { ptr in
try withUnsafeBytes(of: self._key) { ptr in
// this is boilerplate necessary to convert from UnsafeRawBufferPointer to UnsafeBufferPointer<UInt8>
// we know ptr is bound since we defined self._key as let
let typedPointer = ptr.baseAddress?.assumingMemoryBound(to: UInt8.self)
@@ -162,7 +166,7 @@ public struct WebSocketFrame {
/// a frame is not fragmented at all.
public var fin: Bool {
get {
return self.firstByte.isAnyBitSetInMask(0x80)
self.firstByte.isAnyBitSetInMask(0x80)
}
set {
self.firstByte.changingBitsInMask(0x80, to: newValue)
@@ -172,7 +176,7 @@ public struct WebSocketFrame {
/// The value of the first reserved bit. Must be `false` unless using an extension that defines its use.
public var rsv1: Bool {
get {
return self.firstByte.isAnyBitSetInMask(0x40)
self.firstByte.isAnyBitSetInMask(0x40)
}
set {
self.firstByte.changingBitsInMask(0x40, to: newValue)
@@ -182,7 +186,7 @@ public struct WebSocketFrame {
/// The value of the second reserved bit. Must be `false` unless using an extension that defines its use.
public var rsv2: Bool {
get {
return self.firstByte.isAnyBitSetInMask(0x20)
self.firstByte.isAnyBitSetInMask(0x20)
}
set {
self.firstByte.changingBitsInMask(0x20, to: newValue)
@@ -192,7 +196,7 @@ public struct WebSocketFrame {
/// The value of the third reserved bit. Must be `false` unless using an extension that defines its use.
public var rsv3: Bool {
get {
return self.firstByte.isAnyBitSetInMask(0x10)
self.firstByte.isAnyBitSetInMask(0x10)
}
set {
self.firstByte.changingBitsInMask(0x10, to: newValue)
@@ -204,7 +208,7 @@ public struct WebSocketFrame {
get {
// this is a public initialiser which only fails if the opcode is invalid. But all opcodes in 0...0xF
// space are valid so this can never fail.
return WebSocketOpcode(encodedWebSocketOpcode: firstByte & 0x0F)!
WebSocketOpcode(encodedWebSocketOpcode: firstByte & 0x0F)!
}
set {
self.firstByte = (self.firstByte & 0xF0) + UInt8(webSocketOpcode: newValue)
@@ -213,7 +217,7 @@ public struct WebSocketFrame {
/// The total length of the data in the frame.
public var length: Int {
return data.readableBytes + (extensionData?.readableBytes ?? 0)
data.readableBytes + (extensionData?.readableBytes ?? 0)
}
/// The application data.
@@ -223,7 +227,7 @@ public struct WebSocketFrame {
/// obtain it, or transform this data directly by calling `data.unmask(maskKey)`.
public var data: ByteBuffer {
get {
return self._storage.data
self._storage.data
}
set {
if !isKnownUniquelyReferenced(&self._storage) {
@@ -240,7 +244,7 @@ public struct WebSocketFrame {
/// obtain it, or transform this data directly by calling `extensionData.unmask(maskKey)`.
public var extensionData: ByteBuffer? {
get {
return self._storage.extensionData
self._storage.extensionData
}
set {
if !isKnownUniquelyReferenced(&self._storage) {
@@ -303,9 +307,16 @@ public struct WebSocketFrame {
/// - maskKey: The masking key for the frame, if any. Defaults to `nil`.
/// - data: The application data for the frame.
/// - extensionData: The extension data for the frame.
public init(fin: Bool = false, rsv1: Bool = false, rsv2: Bool = false, rsv3: Bool = false,
opcode: WebSocketOpcode = .continuation, maskKey: WebSocketMaskingKey? = nil,
data: ByteBuffer, extensionData: ByteBuffer? = nil) {
public init(
fin: Bool = false,
rsv1: Bool = false,
rsv2: Bool = false,
rsv3: Bool = false,
opcode: WebSocketOpcode = .continuation,
maskKey: WebSocketMaskingKey? = nil,
data: ByteBuffer,
extensionData: ByteBuffer? = nil
) {
self._storage = .init(data: data, extensionData: extensionData)
self.fin = fin
self.rsv1 = rsv1
@@ -348,7 +359,7 @@ extension WebSocketFrame {
extension WebSocketFrame._Storage: Sendable {}
extension WebSocketFrame._Storage: Equatable {
static func ==(lhs: WebSocketFrame._Storage, rhs: WebSocketFrame._Storage) -> Bool {
return lhs.data == rhs.data && lhs.extensionData == rhs.extensionData
static func == (lhs: WebSocketFrame._Storage, rhs: WebSocketFrame._Storage) -> Bool {
lhs.data == rhs.data && lhs.extensionData == rhs.extensionData
}
}