From f3f50c7a016f20ebdd9dc3dd9905f879d88bcf29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20He=C3=9F?= Date: Tue, 30 Jun 2020 23:54:51 +0200 Subject: [PATCH] Drop pre-Swift 5 compat in source Remove if-swift versions, also ties it to NIO2. --- .gitignore | 1 + .travis.yml | 12 +- Package.swift | 4 +- Package@swift-5.swift | 19 --- Sources/NIORedis/RESPChannelHandler.swift | 158 +++++----------------- Sources/NIORedis/RESPParser.swift | 28 +--- Sources/NIORedis/RESPPipelineSetup.swift | 11 +- Sources/NIORedis/RESPValue.swift | 23 +--- 8 files changed, 48 insertions(+), 208 deletions(-) delete mode 100644 Package@swift-5.swift diff --git a/.gitignore b/.gitignore index 028d24c..b6e2135 100644 --- a/.gitignore +++ b/.gitignore @@ -69,4 +69,5 @@ fastlane/test_output *.xcodeproj .build-linux Package.resolved +.swiftpm diff --git a/.travis.yml b/.travis.yml index 5077485..6f8d07e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,16 +7,18 @@ matrix: include: - os: Linux dist: trusty - env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-4.2.3-release/ubuntu1404/swift-4.2.3-RELEASE/swift-4.2.3-RELEASE-ubuntu14.04.tar.gz" + env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.0.2-release/ubuntu1404/swift-5.0.2-RELEASE/swift-5.0.2-RELEASE-ubuntu14.04.tar.gz" sudo: required - os: Linux dist: trusty - env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.0-release/ubuntu1404/swift-5.0-RELEASE/swift-5.0-RELEASE-ubuntu14.04.tar.gz" + env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.1.3-release/ubuntu1404/swift-5.1.3-RELEASE/swift-5.1.3-RELEASE-ubuntu14.04.tar.gz" + sudo: required + - os: Linux + dist: xenial + env: SWIFT_SNAPSHOT_NAME="https://swift.org/builds/swift-5.2-release/ubuntu1604/swift-5.2-RELEASE/swift-5.2-RELEASE-ubuntu16.04.tar.gz" sudo: required - os: osx - osx_image: xcode10.1 - - os: osx - osx_image: xcode10.2 + osx_image: xcode11 before_install: diff --git a/Package.swift b/Package.swift index ccc9c48..c552054 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 import PackageDescription @@ -9,7 +9,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-nio.git", - from: "1.13.2"), + from: "2.18.0") ], targets: [ .target(name: "NIORedis", diff --git a/Package@swift-5.swift b/Package@swift-5.swift deleted file mode 100644 index 3e6e5c9..0000000 --- a/Package@swift-5.swift +++ /dev/null @@ -1,19 +0,0 @@ -// swift-tools-version:5.0 - -import PackageDescription - -let package = Package( - name: "swift-nio-redis", - products: [ - .library(name: "NIORedis", targets: [ "NIORedis" ]), - ], - dependencies: [ - .package(url: "https://github.com/apple/swift-nio.git", - from: "2.0.0") - ], - targets: [ - .target(name: "NIORedis", - dependencies: [ "NIO", "NIOFoundationCompat" ]), - .testTarget(name: "NIORedisTests", dependencies: [ "NIORedis"]) - ] -) diff --git a/Sources/NIORedis/RESPChannelHandler.swift b/Sources/NIORedis/RESPChannelHandler.swift index 00e5f09..b06cd6d 100644 --- a/Sources/NIORedis/RESPChannelHandler.swift +++ b/Sources/NIORedis/RESPChannelHandler.swift @@ -2,7 +2,7 @@ // // This source file is part of the swift-nio-redis open source project // -// Copyright (c) 2018-2019 ZeeZide GmbH. and the swift-nio-redis project authors +// Copyright (c) 2018-2020 ZeeZide GmbH. and the swift-nio-redis project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -16,18 +16,18 @@ import NIO open class RESPChannelHandler : ChannelDuplexHandler { - public typealias InboundErr = RESPParserError + public typealias InboundErr = RESPParserError - public typealias InboundIn = ByteBuffer - public typealias InboundOut = RESPValue + public typealias InboundIn = ByteBuffer + public typealias InboundOut = RESPValue - public typealias OutboundIn = RESPEncodable - public typealias OutboundOut = ByteBuffer + public typealias OutboundIn = RESPEncodable + public typealias OutboundOut = ByteBuffer private final let nilStringBuffer = ConstantBuffers.nilStringBuffer private final let nilArrayBuffer = ConstantBuffers.nilArrayBuffer - public final var parser = RESPParser() + public final var parser = RESPParser() public init() {} @@ -133,27 +133,15 @@ open class RESPChannelHandler : ChannelDuplexHandler { context.write(wrapOutboundOut(out), promise: promise) } - #if swift(>=5) - @inline(__always) - final func encode(simpleString bytes: S, - out: inout ByteBuffer) - where S.Element == UInt8 - { - out.writeInteger(UInt8(43)) // + - out.writeBytes(bytes) - out.writeBytes(eol) - } - #else - @inline(__always) - final func encode(simpleString bytes: S, - out: inout ByteBuffer) - where S.Element == UInt8 - { - out.writeInteger(UInt8(43)) // + - out.writeBytes(bytes) - out.writeBytes(eol) - } - #endif + @inline(__always) + final func encode(simpleString bytes: S, + out: inout ByteBuffer) + where S.Element == UInt8 + { + out.writeInteger(UInt8(43)) // + + out.writeBytes(bytes) + out.writeBytes(eol) + } @inline(__always) final func encode(simpleString bytes: ByteBuffer, out: inout ByteBuffer) { @@ -177,41 +165,22 @@ open class RESPChannelHandler : ChannelDuplexHandler { } } - #if swift(>=5) - @inline(__always) - final func encode(bulkString bytes: S?, - out: inout ByteBuffer) - where S.Element == UInt8 - { - if let s = bytes { - out.writeInteger(UInt8(36)) // $ - out.write(integerAsString : Int(s.count)) - out.writeBytes(eol) - out.writeBytes(s) - out.writeBytes(eol) - } - else { - out.writeBytes(nilString) - } + @inline(__always) + final func encode(bulkString bytes: S?, + out: inout ByteBuffer) + where S.Element == UInt8 + { + if let s = bytes { + out.writeInteger(UInt8(36)) // $ + out.write(integerAsString : Int(s.count)) + out.writeBytes(eol) + out.writeBytes(s) + out.writeBytes(eol) } - #else - @inline(__always) - final func encode(bulkString bytes: S?, - out: inout ByteBuffer) - where S.Element == UInt8 - { - if let s = bytes { - out.writeInteger(UInt8(36)) // $ - out.write(integerAsString : Int(s.count)) - out.writeBytes(eol) - out.writeBytes(s) - out.writeBytes(eol) - } - else { - out.writeBytes(nilString) - } + else { + out.writeBytes(nilString) } - #endif + } @inline(__always) final func encode(integer i: Int, out: inout ByteBuffer) { @@ -270,36 +239,6 @@ open class RESPChannelHandler : ChannelDuplexHandler { } } } - - - #if swift(>=5) // NIO 2 API - default - #else // NIO 1 API Shims - open func channelActive(ctx context: ChannelHandlerContext) { - channelActive(context: context) - } - open func channelInactive(ctx context: ChannelHandlerContext) { - channelInactive(context: context) - } - public func channelRead(ctx context: ChannelHandlerContext, data: NIOAny) { - channelRead(context: context, data: data) - } - open func channelRead(ctx context: ChannelHandlerContext, value: RESPValue) { - channelRead(context: context, value: value) - } - open func errorCaught(ctx context: ChannelHandlerContext, error: Error) { - errorCaught(context: context, error: error) - } - public func write(ctx context: ChannelHandlerContext, data: NIOAny, - promise: EventLoopPromise?) - { - write(context: context, data: data, promise: promise) - } - public final func write(ctx context: ChannelHandlerContext, value: RESPValue, - promise: EventLoopPromise?) - { - write(context: context, value: value, promise: promise) - } - #endif // NIO 1 API Shims } private let eol : ContiguousArray = [ 13, 10 ] // \r\n @@ -310,49 +249,16 @@ fileprivate enum ConstantBuffers { static let nilStringBuffer : ByteBuffer = { let alloc = ByteBufferAllocator() - var bb = alloc.buffer(capacity: 6) + var bb = alloc.buffer(capacity: 6) bb.writeBytes(nilString) return bb }() static let nilArrayBuffer : ByteBuffer = { let alloc = ByteBufferAllocator() - var bb = alloc.buffer(capacity: 6) + var bb = alloc.buffer(capacity: 6) bb.writeBytes(nilArray) return bb }() } -#if swift(>=5) - // NIO 2 -#else -fileprivate extension ByteBuffer { - // NIO 2 API for NIO 1 - - @inline(__always) @discardableResult - mutating func writeString(_ string: String) -> Int { - return self.write(string: string) ?? -1337 // never fails - } - - @inline(__always) @discardableResult - mutating func writeInteger(_ integer: T) -> Int { - return self.write(integer: integer) - } - - @inline(__always) @discardableResult - mutating func writeBuffer(_ buffer: inout ByteBuffer) -> Int { - return self.write(buffer: &buffer) - } - - @inline(__always) @discardableResult - mutating func writeBytes(_ bytes: UnsafeRawBufferPointer) -> Int { - return self.write(bytes: bytes) - } - @inline(__always) @discardableResult - mutating func writeBytes(_ bytes: Bytes) -> Int - where Bytes.Element == UInt8 - { - return self.write(bytes: bytes) - } -} -#endif // swift(<5) diff --git a/Sources/NIORedis/RESPParser.swift b/Sources/NIORedis/RESPParser.swift index 2e481bf..b678826 100644 --- a/Sources/NIORedis/RESPParser.swift +++ b/Sources/NIORedis/RESPParser.swift @@ -2,7 +2,7 @@ // // This source file is part of the swift-nio-redis open source project // -// Copyright (c) 2018-2019 ZeeZide GmbH. and the swift-nio-redis project authors +// Copyright (c) 2018-2020 ZeeZide GmbH. and the swift-nio-redis project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -18,7 +18,7 @@ import struct NIO.ByteBufferAllocator public enum RESPParserError : Error { case UnexpectedStartByte(char: UInt8, buffer: ByteBuffer) case UnexpectedEndByte (char: UInt8, buffer: ByteBuffer) - case TransportError(Swift.Error) + case TransportError (Swift.Error) case ProtocolError case UnexpectedNegativeCount case InternalInconsistency @@ -333,27 +333,3 @@ public struct RESPParser { private var overflowBuffer : ByteBuffer? } - -#if swift(>=5) - // NIO 2 -#else -fileprivate extension ByteBuffer { - // NIO 2 API for NIO 1 - - @inline(__always) @discardableResult - mutating func writeInteger(_ integer: T) -> Int { - return self.write(integer: integer) - } - - @inline(__always) @discardableResult - mutating func writeBytes(_ bytes: UnsafeRawBufferPointer) -> Int { - return self.write(bytes: bytes) - } - @inline(__always) @discardableResult - mutating func writeBytes(_ bytes: Bytes) -> Int - where Bytes.Element == UInt8 - { - return self.write(bytes: bytes) - } -} -#endif // swift(<5) diff --git a/Sources/NIORedis/RESPPipelineSetup.swift b/Sources/NIORedis/RESPPipelineSetup.swift index 00e8579..6529d73 100644 --- a/Sources/NIORedis/RESPPipelineSetup.swift +++ b/Sources/NIORedis/RESPPipelineSetup.swift @@ -2,7 +2,7 @@ // // This source file is part of the swift-nio-redis open source project // -// Copyright (c) 2018-2019 ZeeZide GmbH. and the swift-nio-redis project authors +// Copyright (c) 2018-2020 ZeeZide GmbH. and the swift-nio-redis project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -21,12 +21,7 @@ public extension ChannelPipeline { name : String = "de.zeezide.nio.RESP") -> EventLoopFuture { - #if swift(>=5) - return self.addHandler(RESPChannelHandler(), name: name, - position: first ? .first : .last) - #else - return self.add(name: name, handler: RESPChannelHandler(), first: first) - #endif + return self.addHandler(RESPChannelHandler(), name: name, + position: first ? .first : .last) } - } diff --git a/Sources/NIORedis/RESPValue.swift b/Sources/NIORedis/RESPValue.swift index c6dfe99..9c04b8a 100644 --- a/Sources/NIORedis/RESPValue.swift +++ b/Sources/NIORedis/RESPValue.swift @@ -2,7 +2,7 @@ // // This source file is part of the swift-nio-redis open source project // -// Copyright (c) 2018-2019 ZeeZide GmbH. and the swift-nio-redis project authors +// Copyright (c) 2018-2020 ZeeZide GmbH. and the swift-nio-redis project authors // Licensed under Apache License v2.0 // // See LICENSE.txt for license information @@ -241,7 +241,6 @@ extension RESPValue : CustomStringConvertible { case .error(let e): return "" } } - } @@ -276,24 +275,4 @@ extension String { } } } - } - -#if swift(>=5) - // NIO 2 -#else -fileprivate extension ByteBuffer { - // NIO 2 API for NIO 1 - - @inline(__always) @discardableResult - mutating func writeBytes(_ bytes: UnsafeRawBufferPointer) -> Int { - return self.write(bytes: bytes) - } - @inline(__always) @discardableResult - mutating func writeBytes(_ bytes: Bytes) -> Int - where Bytes.Element == UInt8 - { - return self.write(bytes: bytes) - } -} -#endif // swift(<5)