Files
swift-nio/Sources/NIOCore/ByteBuffer-views.swift
T
Cory Benfield f607e5a47c Extract basic data structures to NIOCore. (#1895)
Motivation:

Currently the "core" NIO module (currently called NIO) contains two
fairly distinct kinds of things. The first is the core abstractions and
data structures: the things that all NIO programs must necessarily deal
with. This includes our base abstraction protocols (e.g. `EventLoop`,
`Channel`, and `ChannelHandler`), as well as our base data types (e.g.
`ByteBuffer`, `SocketAddress`, `EventLoopFuture`).

The second thing the core NIO module contains is the default POSIX
sockets event-loop and channels. These are the mainline production loops
for NIO programs, and are supported in the various BSD-like OSes to
varying extents. These can be thought of as a baseline implementation of
the core abstraction protocols that form the first part of NIO.

These two fairly distinct use-cases have no particular reason to be
glued together, and in fact in gluing them together we have done a bit
of a disservice to our adopters. The biggest issue is that it has become
impossible to write a NIO program or library that does not bring along
the POSIX layer of NIO. This has made porting NIO to other platforms
difficult (Windows has been painful, and Webassembly is coming down the
road as well), and has also bloated the size of iOS/macOS applications
that use NIO via NIOTransportServices, as all of these applications must
bring along an event loop and several Channels they will not use.

To that end, we plan to begin a scope of work to split the core NIO
module in two. Specifically, we intend to extract the baseline protocols
and data types into a new module, called `NIOCore`, which will be able
to be the baseline NIO module.

The end goal is that libraries that implement protocols on top of
SwiftNIO should be able to depend solely on `NIOCore`, not `NIO`. This
will allow NIO applications to bring along only the event loops they
want or need, without needing to pay for the POSIX sockets
implementation. This should also make porting easier, as there will be
no pressure to bring the existing `NIO` module to new platforms if it
does not fit well.

Modifications:

This is the first in a series of patches, which extracts some of the
low-hanging fruit. In particular, it extracts:

- `ByteBuffer` (and associated types)
- `RecvByteBufferAllocator`
- `CircularBuffer`

This also adds in the backwards compatibility shim: the `NIO` package
performs an exported import of `NIOCore`. This will allow this change to
avoid breaking API.

However, to make a clean break we intend not to release a new NIO minor
version until we have completed our extraction. If we fail to do this it
won't be the end of the world, we can release subsequent minor versions
that continue to extract new types. However, as much as possible should
go at once.

This patch also duplicates a few methods and files. At the end of the
patch series we need to reconcile this duplication. In many cases the
duplication will no longer be necessary, but in some cases we may have
necessary duplicates.

Result:

The first step along the road to better platform citizenship is taken.
2021-07-14 09:58:25 +01:00

223 lines
8.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// A view into a portion of a `ByteBuffer`.
///
/// A `ByteBufferView` is useful whenever a `Collection where Element == UInt8` representing a portion of a
/// `ByteBuffer` is needed.
public struct ByteBufferView: RandomAccessCollection {
public typealias Element = UInt8
public typealias Index = Int
public typealias SubSequence = ByteBufferView
/* private but usableFromInline */ @usableFromInline var _buffer: ByteBuffer
/* private but usableFromInline */ @usableFromInline var _range: Range<Index>
@inlinable
internal init(buffer: ByteBuffer, range: Range<Index>) {
precondition(range.lowerBound >= 0 && range.upperBound <= buffer.capacity)
self._buffer = buffer
self._range = range
}
/// Creates a `ByteBufferView` from the readable bytes of the given `buffer`.
@inlinable
public init(_ buffer: ByteBuffer) {
self = ByteBufferView(buffer: buffer, range: buffer.readerIndex ..< buffer.writerIndex)
}
@inlinable
public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R {
return try self._buffer.withVeryUnsafeBytes { ptr in
try body(UnsafeRawBufferPointer(start: ptr.baseAddress!.advanced(by: self._range.lowerBound),
count: self._range.count))
}
}
@inlinable
public var startIndex: Index {
return self._range.lowerBound
}
@inlinable
public var endIndex: Index {
return self._range.upperBound
}
@inlinable
public func index(after i: Index) -> Index {
return i + 1
}
@inlinable
public subscript(position: Index) -> UInt8 {
get {
guard position >= self._range.lowerBound && position < self._range.upperBound else {
preconditionFailure("index \(position) out of range")
}
return self._buffer.getInteger(at: position)! // range check above
}
set {
guard position >= self._range.lowerBound && position < self._range.upperBound else {
preconditionFailure("index \(position) out of range")
}
self._buffer.setInteger(newValue, at: position)
}
}
@inlinable
public subscript(range: Range<Index>) -> ByteBufferView {
get {
return ByteBufferView(buffer: self._buffer, range: range)
}
set {
self.replaceSubrange(range, with: newValue)
}
}
@inlinable
public func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<UInt8>) throws -> R) rethrows -> R? {
return try self.withUnsafeBytes { bytes in
return try body(bytes.bindMemory(to: UInt8.self))
}
}
@inlinable
public func _customIndexOfEquatableElement(_ element : Element) -> Index?? {
return .some(self.withUnsafeBytes { ptr -> Index? in
return ptr.firstIndex(of: element).map { $0 + self._range.lowerBound }
})
}
@inlinable
public func _customLastIndexOfEquatableElement(_ element: Element) -> Index?? {
return .some(self.withUnsafeBytes { ptr -> Index? in
return ptr.lastIndex(of: element).map { $0 + self._range.lowerBound }
})
}
}
extension ByteBufferView: MutableCollection {}
extension ByteBufferView: RangeReplaceableCollection {
// required by `RangeReplaceableCollection`
@inlinable
public init() {
self = ByteBufferView(ByteBuffer())
}
@inlinable
public mutating func replaceSubrange<C: Collection>(_ subrange: Range<Index>, with newElements: C) where ByteBufferView.Element == C.Element {
precondition(subrange.startIndex >= self.startIndex && subrange.endIndex <= self.endIndex,
"subrange out of bounds")
if newElements.count == subrange.count {
self._buffer.setBytes(newElements, at: subrange.startIndex)
} else if newElements.count < subrange.count {
// Replace the subrange.
self._buffer.setBytes(newElements, at: subrange.startIndex)
// Remove the unwanted bytes between the newly copied bytes and the end of the subrange.
// try! is fine here: the copied range is within the view and the length can't be negative.
try! self._buffer.copyBytes(at: subrange.endIndex,
to: subrange.startIndex.advanced(by: newElements.count),
length: subrange.endIndex.distance(to: self._buffer.writerIndex))
// Shorten the range.
let removedBytes = subrange.count - newElements.count
self._buffer.moveWriterIndex(to: self._buffer.writerIndex - removedBytes)
self._range = self._range.dropLast(removedBytes)
} else {
// Make space for the new elements.
// try! is fine here: the copied range is within the view and the length can't be negative.
try! self._buffer.copyBytes(at: subrange.endIndex,
to: subrange.startIndex.advanced(by: newElements.count),
length: subrange.endIndex.distance(to: self._buffer.writerIndex))
// Replace the bytes.
self._buffer.setBytes(newElements, at: subrange.startIndex)
// Widen the range.
let additionalByteCount = newElements.count - subrange.count
self._buffer.moveWriterIndex(forwardBy: additionalByteCount)
self._range = self._range.startIndex ..< self._range.endIndex.advanced(by: additionalByteCount)
}
}
}
extension ByteBuffer {
/// A view into the readable bytes of the `ByteBuffer`.
@inlinable
public var readableBytesView: ByteBufferView {
return ByteBufferView(self)
}
/// Returns a view into some portion of the readable bytes of a `ByteBuffer`.
///
/// - parameters:
/// - index: The index the view should start at
/// - length: The length of the view (in bytes)
/// - returns: A view into a portion of a `ByteBuffer` or `nil` if the requested bytes were not readable.
@inlinable
public func viewBytes(at index: Int, length: Int) -> ByteBufferView? {
guard length >= 0 && index >= self.readerIndex && index <= self.writerIndex - length else {
return nil
}
return ByteBufferView(buffer: self, range: index ..< (index + length))
}
/// Create a `ByteBuffer` from the given `ByteBufferView`s range.
///
/// - parameter view: The `ByteBufferView` which you want to get a `ByteBuffer` from.
@inlinable
public init(_ view: ByteBufferView) {
self = view._buffer.getSlice(at: view.startIndex, length: view.count)!
}
}
extension ByteBufferView: Equatable {
/// required by `Equatable`
public static func == (lhs: ByteBufferView, rhs: ByteBufferView) -> Bool {
guard lhs._range.count == rhs._range.count else {
return false
}
// A well-formed ByteBufferView can never have a range that is out-of-bounds of the backing ByteBuffer.
// As a result, these getSlice calls can never fail, and we'd like to know it if they do.
let leftBufferSlice = lhs._buffer.getSlice(at: lhs._range.startIndex, length: lhs._range.count)!
let rightBufferSlice = rhs._buffer.getSlice(at: rhs._range.startIndex, length: rhs._range.count)!
return leftBufferSlice == rightBufferSlice
}
}
extension ByteBufferView: Hashable {
/// required by `Hashable`
public func hash(into hasher: inout Hasher) {
// A well-formed ByteBufferView can never have a range that is out-of-bounds of the backing ByteBuffer.
// As a result, this getSlice call can never fail, and we'd like to know it if it does.
hasher.combine(self._buffer.getSlice(at: self._range.startIndex, length: self._range.count)!)
}
}
extension ByteBufferView: ExpressibleByArrayLiteral {
/// required by `ExpressibleByArrayLiteral`
public init(arrayLiteral elements: Element...) {
self.init(elements)
}
}