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

[probably close in favour of #709] prefix ContiguousCollection with NIO #708

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/NIO/ByteBuffer-aux.swift
Expand Up @@ -260,7 +260,7 @@ extension ByteBuffer {
/// - returns: The number of bytes written or `bytes.count`.
@discardableResult
@inlinable
public mutating func write<Bytes: ContiguousCollection>(bytes: Bytes) -> Int where Bytes.Element == UInt8 {
public mutating func write<Bytes: NIOContiguousCollection>(bytes: Bytes) -> Int where Bytes.Element == UInt8 {
let written = set(bytes: bytes, at: self.writerIndex)
self._moveWriterIndex(forwardBy: written)
return written
Expand Down
8 changes: 4 additions & 4 deletions Sources/NIO/ByteBuffer-core.swift
Expand Up @@ -395,7 +395,7 @@ public struct ByteBuffer {
}

@inlinable
mutating func _set<Bytes: ContiguousCollection>(bytes: Bytes, at index: _Index) -> _Capacity where Bytes.Element == UInt8 {
mutating func _set<Bytes: NIOContiguousCollection>(bytes: Bytes, at index: _Index) -> _Capacity where Bytes.Element == UInt8 {
let bytesCount = bytes.count
let newEndIndex: _Index = index + _toIndex(Int(bytesCount))
if !isKnownUniquelyReferenced(&self._storage) {
Expand All @@ -405,9 +405,9 @@ public struct ByteBuffer {

self._ensureAvailableCapacity(_Capacity(bytesCount), at: index)
let targetPtr = UnsafeMutableRawBufferPointer(rebasing: self._slicedStorageBuffer.dropFirst(Int(index)))
bytes.withUnsafeBytes { srcPtr in
bytes.withUnsafeBytesNIO { srcPtr in
precondition(srcPtr.count >= bytesCount,
"collection \(bytes) claims count \(bytesCount) but withUnsafeBytes only offers \(srcPtr.count) bytes")
"collection \(bytes) claims count \(bytesCount) but withUnsafeBytesNIO only offers \(srcPtr.count) bytes")
targetPtr.copyMemory(from: UnsafeRawBufferPointer(rebasing: srcPtr.prefix(Int(bytesCount))))
}
return _toCapacity(Int(bytesCount))
Expand Down Expand Up @@ -730,7 +730,7 @@ extension ByteBuffer {
/// Copy the collection of `bytes` into the `ByteBuffer` at `index`.
@discardableResult
@inlinable
public mutating func set<Bytes: ContiguousCollection>(bytes: Bytes, at index: Int) -> Int where Bytes.Element == UInt8 {
public mutating func set<Bytes: NIOContiguousCollection>(bytes: Bytes, at index: Int) -> Int where Bytes.Element == UInt8 {
return Int(self._set(bytes: bytes, at: _toIndex(index)))
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/NIO/ByteBuffer-views.swift
Expand Up @@ -16,7 +16,7 @@
///
/// A `ByteBufferView` is useful whenever a `Collection where Element == UInt8` representing a portion of a
/// `ByteBuffer` is needed.
public struct ByteBufferView: ContiguousCollection, RandomAccessCollection {
public struct ByteBufferView: NIOContiguousCollection, RandomAccessCollection {
public typealias Element = UInt8
public typealias Index = Int
public typealias SubSequence = ByteBufferView
Expand All @@ -30,7 +30,7 @@ public struct ByteBufferView: ContiguousCollection, RandomAccessCollection {
self.range = range
}

public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.buffer.withVeryUnsafeBytes { ptr in
try body(UnsafeRawBufferPointer.init(start: ptr.baseAddress!.advanced(by: self.range.lowerBound),
count: self.range.count))
Expand Down
Expand Up @@ -13,9 +13,9 @@
//===----------------------------------------------------------------------===//

/// A `Collection` that is contiguously laid out in memory and can therefore be duplicated using `memcpy`.
public protocol ContiguousCollection: Collection {
public protocol NIOContiguousCollection: Collection {
@inlinable
func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
}

extension StaticString: Collection {
Expand All @@ -33,28 +33,28 @@ extension StaticString: Collection {
return self.utf8Start.advanced(by: position).pointee
}
}
extension UnsafeRawBufferPointer: ContiguousCollection {
extension UnsafeRawBufferPointer: NIOContiguousCollection {
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try body(self)
}
}
extension UnsafeMutableRawBufferPointer: ContiguousCollection {
extension UnsafeMutableRawBufferPointer: NIOContiguousCollection {
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try body(UnsafeRawBufferPointer(self))
}
}

#if swift(>=4.1)
extension Slice: ContiguousCollection where Base: ContiguousCollection {
extension Slice: NIOContiguousCollection where Base: NIOContiguousCollection {
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
// this is rather compicated because of SR-8580 (can't have two Slice extensions, even if non-overlapping)
let byteDistanceFromBaseToSelf = self.base.distance(from: self.base.startIndex,
to: self.startIndex) * MemoryLayout<Base.Element>.stride
let numberOfBytesInSelf = self.count * MemoryLayout<Base.Element>.stride
return try self.base.withUnsafeBytes { (pointerToBaseCollection: UnsafeRawBufferPointer) -> R in
return try self.base.withUnsafeBytesNIO { (pointerToBaseCollection: UnsafeRawBufferPointer) -> R in
let start = pointerToBaseCollection.startIndex + byteDistanceFromBaseToSelf
let end = start + numberOfBytesInSelf
let range = start..<end
Expand All @@ -66,27 +66,41 @@ extension Slice: ContiguousCollection where Base: ContiguousCollection {
}
#endif

extension Array: ContiguousCollection {}
extension ArraySlice: ContiguousCollection {}
extension ContiguousArray: ContiguousCollection {}
extension Array: NIOContiguousCollection {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be @inlinable, no?

return try self.withUnsafeBytes(body)
}
}

extension ArraySlice: NIOContiguousCollection {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.withUnsafeBytes(body)
}
}

extension ContiguousArray: NIOContiguousCollection {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.withUnsafeBytes(body)
}
}
// ContiguousArray's slice is ArraySlice

extension StaticString: ContiguousCollection {
extension StaticString: NIOContiguousCollection {
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try body(UnsafeRawBufferPointer(start: self.utf8Start, count: self.utf8CodeUnitCount))
}
}

extension UnsafeBufferPointer: ContiguousCollection {
extension UnsafeBufferPointer: NIOContiguousCollection {
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try body(UnsafeRawBufferPointer(self))
}
}
extension UnsafeMutableBufferPointer: ContiguousCollection {
extension UnsafeMutableBufferPointer: NIOContiguousCollection {
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try body(UnsafeRawBufferPointer(self))
}
}
4 changes: 2 additions & 2 deletions Sources/NIOFoundationCompat/ByteBuffer-foundation.swift
Expand Up @@ -37,9 +37,9 @@ public enum ByteBufferFoundationError: Error {
* the platforms OpenSSL in which might cause problems.
*/

extension Data: ContiguousCollection {
extension Data: NIOContiguousCollection {
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
public func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> R in
try body(UnsafeRawBufferPointer(start: ptr, count: self.count))
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/NIOTests/ByteBufferTest.swift
Expand Up @@ -1154,7 +1154,7 @@ class ByteBufferTest: XCTestCase {
}

func testWeUseFastWriteForContiguousCollections() throws {
struct WrongCollection: ContiguousCollection {
struct WrongCollection: NIOContiguousCollection {
let storage: [UInt8] = [1, 2, 3]
typealias Element = UInt8
typealias Index = Array<UInt8>.Index
Expand Down Expand Up @@ -1185,7 +1185,7 @@ class ByteBufferTest: XCTestCase {
return self.storage.index(after: i)
}

func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self.storage.withUnsafeBytes(body)
}
}
Expand Down Expand Up @@ -1453,7 +1453,7 @@ class ByteBufferTest: XCTestCase {

func testWeDontWriteTooMuchForUnderreportingContiguousCollection() throws {
// this is an illegal contiguous collection but we should still be able to deal with this
struct UnderreportingContiguousCollection: ContiguousCollection {
struct UnderreportingContiguousCollection: NIOContiguousCollection {
let storage: [UInt8] = Array(repeating: 0xff, count: 4096)
typealias Element = UInt8
typealias Index = Array<UInt8>.Index
Expand Down Expand Up @@ -1491,7 +1491,7 @@ class ByteBufferTest: XCTestCase {
return self.storage.index(after: i)
}

func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
func withUnsafeBytesNIO<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
// we're giving access to 4096 elements despite the fact we claim to only have 3 available
return try self.storage.withUnsafeBytes(body)
}
Expand Down