Skip to content

Commit

Permalink
fix some Swift 4.2 warnings (#374)
Browse files Browse the repository at this point in the history
Motivation:

Warnings are bad.

Modifications:

Changed things that still work in 4.0/4.1 but are warnings in 4.2 to
improved versions.

Result:

less warnings on the upcoming 4.2
  • Loading branch information
weissi authored and Lukasa committed May 1, 2018
1 parent 772917f commit 8bf1be0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
7 changes: 3 additions & 4 deletions Sources/NIO/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,9 @@ extension StaticString: Collection {
public var endIndex: Index { return self.utf8CodeUnitCount }
public func index(after i: Index) -> Index { return i + 1 }

public subscript(position: Int) -> StaticString.Element {
get {
return self[position]
}
public subscript(position: Int) -> UInt8 {
precondition(position < self.utf8CodeUnitCount, "index \(position) out of bounds")
return self.utf8Start.advanced(by: position).pointee
}
}

Expand Down
7 changes: 6 additions & 1 deletion Sources/NIO/CircularBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public protocol AppendableCollection: Collection {
/// will automatically expand if more elements than `initialRingCapacity` are stored, it's advantageous to prevent
/// expansions from happening frequently. Expansions will always force an allocation and a copy to happen.
public struct CircularBuffer<E>: CustomStringConvertible, AppendableCollection {
#if swift(>=4.2)
public typealias RangeType = Range
#else
public typealias RangeType = CountableRange
#endif
private var buffer: ContiguousArray<E?>

/// The index into the buffer of the first item
Expand Down Expand Up @@ -168,7 +173,7 @@ public struct CircularBuffer<E>: CustomStringConvertible, AppendableCollection {
}

/// Return all valid indices of the ring.
public var indices: CountableRange<Int> {
public var indices: RangeType<Int> {
return 0..<self.count
}

Expand Down
8 changes: 7 additions & 1 deletion Sources/NIO/MarkedCircularBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
/// writes and mark how far through the buffer the user has flushed, and therefore how far through the buffer is
/// safe to write.
public struct MarkedCircularBuffer<E>: CustomStringConvertible, AppendableCollection {
#if swift(>=4.2)
public typealias RangeType = Range
#else
public typealias RangeType = CountableRange
#endif

private var buffer: CircularBuffer<E>
private var markedIndex: Int = -1 /* negative: nothing marked */

Expand Down Expand Up @@ -71,7 +77,7 @@ public struct MarkedCircularBuffer<E>: CustomStringConvertible, AppendableCollec
}

/// The valid indices into the buffer.
public var indices: CountableRange<Int> {
public var indices: RangeType<Int> {
return self.buffer.indices
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/NIOPriorityQueue/Heap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ internal struct Heap<T: Comparable> {

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
extension Heap: CustomDebugStringConvertible {
@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
public var debugDescription: String {
guard self.storage.count > 0 else {
return "<empty heap>"
Expand Down Expand Up @@ -266,6 +267,7 @@ extension Heap: Sequence {
return self.storage.count
}

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
func makeIterator() -> HeapIterator<T> {
return HeapIterator(heap: self)
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/NIOPriorityQueue/PriorityQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public struct PriorityQueue<Element: Comparable> {

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
extension PriorityQueue: Equatable {
@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
public static func ==(lhs: PriorityQueue, rhs: PriorityQueue) -> Bool {
return lhs.count == rhs.count && lhs.elementsEqual(rhs)
}
Expand All @@ -63,23 +64,29 @@ extension PriorityQueue: Equatable {
extension PriorityQueue: Sequence {
public struct Iterator: IteratorProtocol {

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
private var queue: PriorityQueue<Element>

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
fileprivate init(queue: PriorityQueue<Element>) {
self.queue = queue
}

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
public mutating func next() -> Element? {
return self.queue.pop()
}
}

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
public func makeIterator() -> Iterator {
return Iterator(queue: self)
}
}

@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
public extension PriorityQueue {
@available(*, deprecated, message: "The NIOPriorityQueue module is deprecated and will be removed in the next major release.")
public var count: Int {
return self.heap.count
}
Expand Down
1 change: 1 addition & 0 deletions Tests/NIOTests/ByteBufferTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ extension ByteBufferTest {
("testLargeSliceBeginMoreThan16MBIsOkay", testLargeSliceBeginMoreThan16MBIsOkay),
("testDiscardReadBytesOnConsumedBuffer", testDiscardReadBytesOnConsumedBuffer),
("testDumpBytesFormat", testDumpBytesFormat),
("testStaticStringCategorySubscript", testStaticStringCategorySubscript),
]
}
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/NIOTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,12 @@ class ByteBufferTest: XCTestCase {
"e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff ]"
XCTAssertEqual(expected, actual)
}

func testStaticStringCategorySubscript() throws {
let s: StaticString = "hello"
XCTAssertEqual("h".utf8.first!, s[0])
XCTAssertEqual("o".utf8.first!, s[4])
}
}

private enum AllocationExpectationState: Int {
Expand Down

0 comments on commit 8bf1be0

Please sign in to comment.