Skip to content
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

CircularBuffer: use vendored swift-collections/Deque instead #1792

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ This product contains a derivation of Fabian Fett's 'Base64.swift'.
* https://github.com/fabianfett/swift-base64-kit/blob/master/LICENSE
* HOMEPAGE:
* https://github.com/fabianfett/swift-base64-kit

---

This product contains the `DequeModule` from Swift Collections.

* LICENSE (Apache License 2.0):
* https://github.com/apple/swift-collections/blob/main/LICENSE.txt
* HOMEPAGE:
* https://github.com/apple/swift-collections
340 changes: 34 additions & 306 deletions Sources/NIO/CircularBuffer.swift

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Sources/NIO/Codec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ extension B2MDBuffer {
case .ready where self.buffers.count > 0:
var buffer = self.buffers.removeFirst()
buffer.writeBuffers(self.buffers)
self.buffers.removeAll(keepingCapacity: self.buffers.capacity < 16) // don't grow too much
self.buffers.removeAll(keepingCapacity: self.buffers.count < 16) // don't grow too much
if buffer.readableBytes > 0 || allowEmptyBuffer {
self.state = .processingInProgress
return .available(buffer)
Expand Down Expand Up @@ -316,7 +316,7 @@ extension B2MDBuffer {
} else {
buffer.discardReadBytes()
buffer.writeBuffers(self.buffers)
self.buffers.removeAll(keepingCapacity: self.buffers.capacity < 16) // don't grow too much
self.buffers.removeAll(keepingCapacity: self.buffers.count < 16) // don't grow too much
self.buffers.append(buffer)
}
}
Expand Down
55 changes: 55 additions & 0 deletions Sources/NIO/VendoredDeque/Deque+Codable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Changes for SwiftNIO
- renamed NIODeque to NIONIODeque (to prevent future clashes)
- made (NIO)NIODeque internal (not public)

DO NOT CHANGE THESE FILES, THEY ARE VENDORED FROM Swift Collections.
*/
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension NIODeque: Encodable where Element: Encodable {
/// Encodes the elements of this deque into the given encoder in an unkeyed
/// container.
///
/// This function throws an error if any values are invalid for the given
/// encoder's format.
///
/// - Parameter encoder: The encoder to write data to.
/* was @inlinable */
@usableFromInline internal /*was public */ func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
for element in self {
try container.encode(element)
}
}
}

extension NIODeque: Decodable where Element: Decodable {
/// Creates a new deque by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the data read is corrupted or otherwise invalid.
///
/// - Parameter decoder: The decoder to read data from.
/* was @inlinable */
@usableFromInline internal /*was public */ init(from decoder: Decoder) throws {
self.init()

var container = try decoder.unkeyedContainer()
if let count = container.count {
self.reserveCapacity(count)
}
while !container.isAtEnd {
let element = try container.decode(Element.self)
self.append(element)
}
}
}
34 changes: 34 additions & 0 deletions Sources/NIO/VendoredDeque/Deque+CustomDebugStringConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Changes for SwiftNIO
- renamed NIODeque to NIONIODeque (to prevent future clashes)
- made (NIO)NIODeque internal (not public)

DO NOT CHANGE THESE FILES, THEY ARE VENDORED FROM Swift Collections.
*/
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension NIODeque: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
@usableFromInline internal /*was public */ var debugDescription: String {
var result = "NIODeque<\(Element.self)>(["
var first = true
for item in self {
if first {
first = false
} else {
result += ", "
}
debugPrint(item, terminator: "", to: &result)
}
result += "])"
return result
}
}
23 changes: 23 additions & 0 deletions Sources/NIO/VendoredDeque/Deque+CustomReflectable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Changes for SwiftNIO
- renamed NIODeque to NIONIODeque (to prevent future clashes)
- made (NIO)NIODeque internal (not public)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something here is prefixing NIO again.


DO NOT CHANGE THESE FILES, THEY ARE VENDORED FROM Swift Collections.
*/
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension NIODeque: CustomReflectable {
/// The custom mirror for this instance.
@usableFromInline internal /*was public */ var customMirror: Mirror {
Mirror(self, unlabeledChildren: self, displayStyle: .collection)
}
}
34 changes: 34 additions & 0 deletions Sources/NIO/VendoredDeque/Deque+CustomStringConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Changes for SwiftNIO
- renamed NIODeque to NIONIODeque (to prevent future clashes)
- made (NIO)NIODeque internal (not public)

DO NOT CHANGE THESE FILES, THEY ARE VENDORED FROM Swift Collections.
*/
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension NIODeque: CustomStringConvertible {
/// A textual representation of this instance.
@usableFromInline internal /*was public */ var description: String {
var result = "["
var first = true
for item in self {
if first {
first = false
} else {
result += ", "
}
print(item, terminator: "", to: &result)
}
result += "]"
return result
}
}
28 changes: 28 additions & 0 deletions Sources/NIO/VendoredDeque/Deque+Equatable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Changes for SwiftNIO
- renamed NIODeque to NIONIODeque (to prevent future clashes)
- made (NIO)NIODeque internal (not public)

DO NOT CHANGE THESE FILES, THEY ARE VENDORED FROM Swift Collections.
*/
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension NIODeque: Equatable where Element: Equatable {
/// Returns a Boolean value indicating whether two values are equal. Two
/// deques are considered equal if they contain the same elements in the same
/// order.
///
/// - Complexity: O(`min(left.count, right.count)`)
/* was @inlinable */
@usableFromInline internal /*was public */ static func ==(left: Self, right: Self) -> Bool {
return left.elementsEqual(right)
}
}
33 changes: 33 additions & 0 deletions Sources/NIO/VendoredDeque/Deque+ExpressibleByArrayLiteral.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Changes for SwiftNIO
- renamed NIODeque to NIONIODeque (to prevent future clashes)
- made (NIO)NIODeque internal (not public)

DO NOT CHANGE THESE FILES, THEY ARE VENDORED FROM Swift Collections.
*/
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

extension NIODeque: ExpressibleByArrayLiteral {
/// Creates a new deque from the contents of an array literal.
///
/// Do not call this initializer directly. It is used by the compiler when
/// you use an array literal. Instead, create a new set using an array
/// literal as its value by enclosing a comma-separated list of values in
/// square brackets. You can use an array literal anywhere a set is expected
/// by the type context.
///
/// - Parameter elements: A variadic list of elements of the new set.
/* was @inlinable */
@inline(__always)
@usableFromInline internal /*was public */ init(arrayLiteral elements: Element...) {
self.init(elements)
}
}
Loading