Drop pre-Swift 5 compat in source

Remove if-swift versions, also ties it to NIO2.
This commit is contained in:
Helge Heß 2020-06-30 23:54:51 +02:00
parent 7ed9d3bb81
commit f3f50c7a01
8 changed files with 48 additions and 208 deletions

1
.gitignore vendored
View File

@ -69,4 +69,5 @@ fastlane/test_output
*.xcodeproj
.build-linux
Package.resolved
.swiftpm

View File

@ -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:

View File

@ -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",

View File

@ -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"])
]
)

View File

@ -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<S: Collection>(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<S: ContiguousCollection>(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<S: Collection>(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<S: Collection>(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<S: Collection>(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<S: ContiguousCollection>(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<Void>?)
{
write(context: context, data: data, promise: promise)
}
public final func write(ctx context: ChannelHandlerContext, value: RESPValue,
promise: EventLoopPromise<Void>?)
{
write(context: context, value: value, promise: promise)
}
#endif // NIO 1 API Shims
}
private let eol : ContiguousArray<UInt8> = [ 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<T: FixedWidthInteger>(_ 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: Sequence>(_ bytes: Bytes) -> Int
where Bytes.Element == UInt8
{
return self.write(bytes: bytes)
}
}
#endif // swift(<5)

View File

@ -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<T: FixedWidthInteger>(_ 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: Sequence>(_ bytes: Bytes) -> Int
where Bytes.Element == UInt8
{
return self.write(bytes: bytes)
}
}
#endif // swift(<5)

View File

@ -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<Void>
{
#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)
}
}

View File

@ -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 "<Error: \(e)>"
}
}
}
@ -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: Sequence>(_ bytes: Bytes) -> Int
where Bytes.Element == UInt8
{
return self.write(bytes: bytes)
}
}
#endif // swift(<5)