updates to RawSpan

This commit is contained in:
Guillaume Lessard authored and Guillaume Lessard committed 2024-10-06 10:24:55 -07:00
1 parent 7f13d84dd5
commit 3fda314bcd
2 files changed
+108 -108

No files matched your search

+77 -81
View File
@@ -149,7 +149,7 @@ extension RawSpan {
/// - Parameters:
/// - span: An existing `Span<T>`, which will define both this
/// `RawSpan`'s lifetime and the memory it represents.
@inlinable @inline(__always)
@_alwaysEmitIntoClient
public init<T: BitwiseCopyable>(_ span: borrowing Span<T>) {
self.init(
_unchecked: UnsafeRawPointer(span._start),
@@ -208,50 +208,37 @@ extension RawSpan {
//MARK: Bounds Checking
extension RawSpan {
/// Return true if `offset` is a valid offset into this `RawSpan`
/// Return true if `offset` is a valid byte offset into this `RawSpan`
///
/// - Parameters:
/// - position: an index to validate
/// - Returns: true if `offset` is a valid index
/// - position: a byte offset to validate
/// - Returns: true if `offset` is a valid byte offset
@inlinable @inline(__always)
public func boundsContain(_ offset: Int) -> Bool {
0 <= offset && offset < byteCount
}
/// Traps if `offset` is not a valid offset into this `RawSpan`
///
/// - Parameters:
/// - position: an index to validate
@inlinable @inline(__always)
public func boundsPrecondition(_ offset: Int) {
precondition(
boundsContain(offset), "Offset out of bounds"
)
}
/// Return true if `offsets` is a valid range of offsets into this `RawSpan`
///
/// - Parameters:
/// - offsets: a range of indices to validate
/// - Returns: true if `offsets` is a valid range of indices
/// - offsets: a range of byte offsets to validate
/// - Returns: true if `offsets` is a valid range of byte offsets
@inlinable @inline(__always)
public func boundsContain(_ offsets: Range<Int>) -> Bool {
0 <= offsets.lowerBound && offsets.upperBound <= byteCount
}
/// Traps if `offsets` is not a valid range of offsets into this `RawSpan`
/// Return true if `offsets` is a valid range of offsets into this `RawSpan`
///
/// - Parameters:
/// - offsets: a range of indices to validate
/// - offsets: a range of byte offsets to validate
/// - Returns: true if `offsets` is a valid range of byte offsets
@inlinable @inline(__always)
public func boundsPrecondition(_ offsets: Range<Int>) {
precondition(
boundsContain(offsets), "Range of offsets out of bounds"
)
public func boundsContain(_ offsets: ClosedRange<Int>) -> Bool {
0 <= offsets.lowerBound && offsets.upperBound < byteCount
}
}
/*
//MARK: extracting sub-spans
extension RawSpan {
@@ -268,10 +255,15 @@ extension RawSpan {
/// - Returns: A span over the bytes within `bounds`
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func extracting(_ bounds: Range<Int>) -> Self {
boundsPrecondition(bounds)
return extracting(unchecked: bounds)
@_alwaysEmitIntoClient
@usableFromInline func _extracting(_ bounds: Range<Int>) -> Self {
precondition(boundsContain(bounds))
return _extracting(unchecked: bounds)
}
@_alwaysEmitIntoClient
public mutating func _shrink(to bounds: Range<Int>) {
self = _extracting(bounds)
}
/// Constructs a new span over the bytes within the supplied range of
@@ -289,14 +281,19 @@ extension RawSpan {
/// - Returns: A span over the bytes within `bounds`
///
/// - Complexity: O(1)
@inlinable @inline(__always)
public func extracting(unchecked bounds: Range<Int>) -> Self {
@_alwaysEmitIntoClient
@usableFromInline func _extracting(unchecked bounds: Range<Int>) -> Self {
RawSpan(
_unchecked: _pointer?.advanced(by: bounds.lowerBound),
byteCount: bounds.count
)
}
@_alwaysEmitIntoClient
public mutating func _shrink(unchecked bounds: Range<Int>) {
self = _extracting(unchecked: bounds)
}
/// Constructs a new span over the bytes within the supplied range of
/// positions within this span.
///
@@ -311,8 +308,13 @@ extension RawSpan {
///
/// - Complexity: O(1)
@_alwaysEmitIntoClient
public func extracting(_ bounds: some RangeExpression<Int>) -> Self {
extracting(bounds.relative(to: _byteOffsets))
@usableFromInline func _extracting(_ bounds: some RangeExpression<Int>) -> Self {
_extracting(bounds.relative(to: _byteOffsets))
}
@_alwaysEmitIntoClient
public mutating func _shrink(_ bounds: some RangeExpression<Int>) {
self = _extracting(bounds)
}
/// Constructs a new span over the bytes within the supplied range of
@@ -331,10 +333,15 @@ extension RawSpan {
///
/// - Complexity: O(1)
@_alwaysEmitIntoClient
public func extracting(
@usableFromInline func _extracting(
unchecked bounds: some RangeExpression<Int>
) -> Self {
extracting(unchecked: bounds.relative(to: _byteOffsets))
_extracting(unchecked: bounds.relative(to: _byteOffsets))
}
@_alwaysEmitIntoClient
public mutating func _shrink(unchecked bounds: some RangeExpression<Int>) {
self = _extracting(unchecked: bounds)
}
/// Constructs a new span over all the bytes of this span.
@@ -347,37 +354,10 @@ extension RawSpan {
///
/// - Complexity: O(1)
@_alwaysEmitIntoClient
public func extracting(_: UnboundedRange) -> Self {
@usableFromInline func _extracting(_: UnboundedRange) -> Self {
self
}
}
*/
extension RawSpan {
@_alwaysEmitIntoClient
public mutating func _shrink(to bounds: Range<Int>) {
boundsPrecondition(bounds)
_shrink(unchecked: bounds)
}
@_alwaysEmitIntoClient
public mutating func _shrink(unchecked bounds: Range<Int>) {
self = RawSpan(
_unchecked: _pointer?.advanced(by: bounds.lowerBound),
byteCount: bounds.count
)
}
@_alwaysEmitIntoClient
public mutating func _shrink(_ bounds: some RangeExpression<Int>) {
_shrink(to: bounds.relative(to: _byteOffsets))
}
@_alwaysEmitIntoClient
public mutating func _shrink(unchecked bounds: some RangeExpression<Int>) {
_shrink(unchecked: bounds.relative(to: _byteOffsets))
}
}
extension RawSpan {
@@ -452,9 +432,9 @@ extension RawSpan {
public func unsafeLoad<T>(
fromByteOffset offset: Int = 0, as: T.Type
) -> T {
boundsPrecondition(
precondition(boundsContain(
Range(uncheckedBounds: (offset, offset+MemoryLayout<T>.size))
)
))
return unsafeLoad(fromUncheckedByteOffset: offset, as: T.self)
}
@@ -503,9 +483,9 @@ extension RawSpan {
public func unsafeLoadUnaligned<T: BitwiseCopyable>(
fromByteOffset offset: Int = 0, as: T.Type
) -> T {
boundsPrecondition(
precondition(boundsContain(
Range(uncheckedBounds: (offset, offset+MemoryLayout<T>.size))
)
))
return unsafeLoadUnaligned(fromUncheckedByteOffset: offset, as: T.self)
}
@@ -571,7 +551,6 @@ extension RawSpan {
}
//MARK: one-sided slicing operations
/*
extension RawSpan {
/// Returns a span containing the initial bytes of this span,
@@ -589,13 +568,18 @@ extension RawSpan {
/// - Returns: A span with at most `maxLength` bytes.
///
/// - Complexity: O(1)
@inlinable
public func extracting(first maxLength: Int) -> Self {
@_alwaysEmitIntoClient
@usableFromInline func _extracting(first maxLength: Int) -> Self {
precondition(maxLength >= 0, "Can't have a prefix of negative length.")
let newCount = min(maxLength, byteCount)
return Self(_unchecked: _pointer, byteCount: newCount)
}
@_alwaysEmitIntoClient
public mutating func _shrink(toFirst maxLength: Int) {
self = _extracting(first: maxLength)
}
/// Returns a span over all but the given number of trailing bytes.
///
/// If the number of elements to drop exceeds the number of elements in
@@ -610,13 +594,18 @@ extension RawSpan {
/// - Returns: A span leaving off the specified number of bytes at the end.
///
/// - Complexity: O(1)
@inlinable
public func extracting(droppingLast k: Int) -> Self {
@_alwaysEmitIntoClient
@usableFromInline func _extracting(droppingLast k: Int) -> Self {
precondition(k >= 0, "Can't drop a negative number of elements.")
let dc = min(k, byteCount)
return Self(_unchecked: _pointer, byteCount: byteCount&-dc)
}
@_alwaysEmitIntoClient
public mutating func _shrink(droppingLast k: Int) {
self = _extracting(droppingLast: k)
}
/// Returns a span containing the trailing bytes of the span,
/// up to the given maximum length.
///
@@ -632,14 +621,19 @@ extension RawSpan {
/// - Returns: A span with at most `maxLength` bytes.
///
/// - Complexity: O(1)
@inlinable
public func extracting(last maxLength: Int) -> Self {
@_alwaysEmitIntoClient
@usableFromInline func _extracting(last maxLength: Int) -> Self {
precondition(maxLength >= 0, "Can't have a suffix of negative length.")
let newCount = min(maxLength, byteCount)
let newStart = _pointer?.advanced(by: byteCount&-newCount)
return Self(_unchecked: newStart, byteCount: newCount)
}
@_alwaysEmitIntoClient
public mutating func _shrink(toLast maxLength: Int) {
self = _extracting(last: maxLength)
}
/// Returns a span over all but the given number of initial bytes.
///
/// If the number of elements to drop exceeds the number of bytes in
@@ -654,15 +648,19 @@ extension RawSpan {
/// - Returns: A span starting after the specified number of bytes.
///
/// - Complexity: O(1)
@inlinable
public func extracting(droppingFirst k: Int) -> Self {
@_alwaysEmitIntoClient
@usableFromInline func _extracting(droppingFirst k: Int) -> Self {
precondition(k >= 0, "Can't drop a negative number of elements.")
let dc = min(k, byteCount)
let newStart = _pointer?.advanced(by: dc)
return Self(_unchecked: newStart, byteCount: byteCount&-dc)
}
@_alwaysEmitIntoClient
public mutating func _shrink(droppingFirst k: Int) {
self = _extracting(droppingFirst: k)
}
}
*/
/// An error indicating that out-of-bounds access was attempted
@frozen
@@ -679,7 +677,6 @@ public struct OutOfBoundsError: Error {
}
}
/*
extension RawSpan {
/// Parse an instance of `T`, advancing `position`.
@inlinable
@@ -710,7 +707,7 @@ extension RawSpan {
guard end <= length else {
throw OutOfBoundsError(expected: length, has: byteCount&-position)
}
return extracting(position..<end)
return _extracting(position..<end)
}
}
@@ -727,7 +724,7 @@ extension RawSpan {
@inlinable
public init(_ base: RawSpan, in range: Range<Int>) {
base.boundsPrecondition(range)
precondition(base.boundsContain(range))
position = 0
self.base = base
parseRange = range
@@ -758,7 +755,7 @@ extension RawSpan {
/// The bytes that we've parsed so far
@inlinable
public var parsedBytes: RawSpan { base.extracting(..<position) }
public var parsedBytes: RawSpan { base._extracting(..<position) }
/// The number of bytes left to parse
@inlinable
@@ -773,4 +770,3 @@ extension RawSpan {
Cursor(self, in: range)
}
}
*/
+31 -27
View File
@@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//
import XCTest
import Future
@testable import Future
final class RawSpanTests: XCTestCase {
@@ -99,7 +99,9 @@ final class RawSpanTests: XCTestCase {
a.withUnsafeBytes {
let span = RawSpan(_unsafeBytes: $0)
let u0 = span.extracting(droppingFirst: 2).unsafeLoadUnaligned(as: UInt64.self)
var copy = span
copy._shrink(droppingFirst: 2)
let u0 = copy.unsafeLoadUnaligned(as: UInt64.self)
XCTAssertEqual(u0 & 0xff, 2)
XCTAssertEqual(u0.byteSwapped & 0xff, 9)
let u1 = span.unsafeLoadUnaligned(fromByteOffset: 6, as: UInt64.self)
@@ -114,10 +116,10 @@ final class RawSpanTests: XCTestCase {
let b = (0..<capacity).map(Int8.init)
b.withUnsafeBytes {
let span = RawSpan(_unsafeBytes: $0)
let sub1 = span.extracting(0..<2)
let sub2 = span.extracting(..<2)
let sub3 = span.extracting(...)
let sub4 = span.extracting(unchecked: 2...)
let sub1 = span._extracting(0..<2)
let sub2 = span._extracting(..<2)
let sub3 = span._extracting(...)
let sub4 = span._extracting(unchecked: 2...)
XCTAssertTrue(
sub1.unsafeView(as: UInt8.self)._elementsEqual(sub2.unsafeView(as: UInt8.self))
)
@@ -135,8 +137,10 @@ final class RawSpanTests: XCTestCase {
let b = (0..<capacity).map(UInt8.init)
b.withUnsafeBytes {
let span = RawSpan(_unsafeBytes: $0)
let prefix = span.extracting(0..<8)
let beyond = prefix.extracting(unchecked: 16..<24)
var prefix = span
prefix._shrink(to: 0..<8)
var beyond = prefix
beyond._shrink(unchecked: 16..<24)
XCTAssertEqual(beyond.byteCount, 8)
XCTAssertEqual(beyond.unsafeLoad(as: UInt8.self), 16)
}
@@ -182,16 +186,16 @@ final class RawSpanTests: XCTestCase {
a.withUnsafeBytes {
let span = RawSpan(_unsafeBytes: $0)
XCTAssertEqual(span.byteCount, capacity)
XCTAssertEqual(span.extracting(first: 1).unsafeLoad(as: UInt8.self), 0)
XCTAssertEqual(span._extracting(first: 1).unsafeLoad(as: UInt8.self), 0)
XCTAssertEqual(
span.extracting(first: capacity).unsafeLoad(
span._extracting(first: capacity).unsafeLoad(
fromByteOffset: capacity-1, as: UInt8.self
),
UInt8(capacity-1)
)
XCTAssertTrue(span.extracting(droppingLast: capacity).isEmpty)
XCTAssertTrue(span._extracting(droppingLast: capacity).isEmpty)
XCTAssertEqual(
span.extracting(droppingLast: 1).unsafeLoad(
span._extracting(droppingLast: 1).unsafeLoad(
fromByteOffset: capacity-2, as: UInt8.self
),
UInt8(capacity-2)
@@ -202,8 +206,8 @@ final class RawSpanTests: XCTestCase {
let b = UnsafeRawBufferPointer(start: nil, count: 0)
let span = RawSpan(_unsafeBytes: b)
XCTAssertEqual(span.byteCount, b.count)
XCTAssertEqual(span.extracting(first: 1).byteCount, b.count)
XCTAssertEqual(span.extracting(droppingLast: 1).byteCount, b.count)
XCTAssertEqual(span._extracting(first: 1).byteCount, b.count)
XCTAssertEqual(span._extracting(droppingLast: 1).byteCount, b.count)
}
}
@@ -213,19 +217,19 @@ final class RawSpanTests: XCTestCase {
a.withUnsafeBytes {
let span = RawSpan(_unsafeBytes: $0)
XCTAssertEqual(span.byteCount, capacity)
XCTAssertEqual(span.extracting(last: capacity).unsafeLoad(as: UInt8.self), 0)
XCTAssertEqual(span.extracting(last: capacity-1).unsafeLoad(as: UInt8.self), 1)
XCTAssertEqual(span.extracting(last: 1).unsafeLoad(as: UInt8.self), UInt8(capacity-1))
XCTAssertTrue(span.extracting(droppingFirst: capacity).isEmpty)
XCTAssertEqual(span.extracting(droppingFirst: 1).unsafeLoad(as: UInt8.self), 1)
XCTAssertEqual(span._extracting(last: capacity).unsafeLoad(as: UInt8.self), 0)
XCTAssertEqual(span._extracting(last: capacity-1).unsafeLoad(as: UInt8.self), 1)
XCTAssertEqual(span._extracting(last: 1).unsafeLoad(as: UInt8.self), UInt8(capacity-1))
XCTAssertTrue(span._extracting(droppingFirst: capacity).isEmpty)
XCTAssertEqual(span._extracting(droppingFirst: 1).unsafeLoad(as: UInt8.self), 1)
}
do {
let b = UnsafeRawBufferPointer(start: nil, count: 0)
let span = RawSpan(_unsafeBytes: b)
XCTAssertEqual(span.byteCount, b.count)
XCTAssertEqual(span.extracting(last: 1).byteCount, b.count)
XCTAssertEqual(span.extracting(droppingFirst: 1).byteCount, b.count)
XCTAssertEqual(span._extracting(last: 1).byteCount, b.count)
XCTAssertEqual(span._extracting(droppingFirst: 1).byteCount, b.count)
}
}
@@ -234,9 +238,9 @@ final class RawSpanTests: XCTestCase {
let a = Array(0..<capacity)
let span = RawSpan(a.storage)
for o in span._byteOffsets {
span.assertValidity(o)
XCTAssertTrue(span.boundsContain(o))
}
// span.assertValidity(span.count)
XCTAssertFalse(span.boundsContain(span.byteCount))
}
func testContainment() {
@@ -244,8 +248,8 @@ final class RawSpanTests: XCTestCase {
defer { b.deallocate() }
let span = RawSpan(_unsafeBytes: b)
let subSpan = span.extracting(last: 2)
let emptySpan = span.extracting(first: 0)
let subSpan = span._extracting(last: 2)
let emptySpan = span._extracting(first: 0)
let fakeSpan = RawSpan(
_unsafeStart: b.baseAddress!.advanced(by: 8), byteCount: 8
)
@@ -269,8 +273,8 @@ final class RawSpanTests: XCTestCase {
defer { b.deallocate() }
let span = RawSpan(_unsafeBytes: b)
let subSpan = span.extracting(last: 2)
let emptySpan = span.extracting(first: 0)
let subSpan = span._extracting(last: 2)
let emptySpan = span._extracting(first: 0)
let nilSpan = RawSpan(
_unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0)
)