From 637518426db7d83f12e592c18cedea7be22572bf Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Thu, 20 Jun 2024 18:10:45 -0700 Subject: [PATCH 1/4] a few corrections --- Sources/Future/RawSpan.swift | 12 ++++++------ Sources/Future/Span.swift | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index 5dcf499b..ef5cd4d1 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -35,7 +35,7 @@ extension RawSpan: Sendable {} extension RawSpan { - //FIXME: make failable once Optional can be non-escapable + //FIXME: make properly non-failable /// Unsafely create a `RawSpan` over initialized memory. /// /// The memory in `buffer` must be owned by the instance `owner`, @@ -160,7 +160,7 @@ extension RawSpan { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `RawSpan`. /// - /// - Returns: A `Span` over the bytes within `bounds` + /// - Returns: A span over the bytes within `bounds` /// /// - Complexity: O(1) @inlinable @inline(__always) @@ -181,7 +181,7 @@ extension RawSpan { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `RawSpan`. /// - /// - Returns: A `Span` over the bytes within `bounds` + /// - Returns: A span over the bytes within `bounds` /// /// - Complexity: O(1) @inlinable @inline(__always) @@ -203,7 +203,7 @@ extension RawSpan { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `RawSpan`. /// - /// - Returns: A `Span` over the bytes within `bounds` + /// - Returns: A span over the bytes within `bounds` /// /// - Complexity: O(1) @_alwaysEmitIntoClient @@ -223,7 +223,7 @@ extension RawSpan { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `RawSpan`. /// - /// - Returns: A `Span` over the bytes within `bounds` + /// - Returns: A span over the bytes within `bounds` /// /// - Complexity: O(1) @_alwaysEmitIntoClient @@ -239,7 +239,7 @@ extension RawSpan { /// slices, extracted spans do not generally share their indices with the /// span from which they are extracted. /// - /// - Returns: A `RawSpan` over all the items of this span. + /// - Returns: A span over all the bytes of this span. /// /// - Complexity: O(1) @_alwaysEmitIntoClient diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index b5efcdb8..80b983d3 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -43,7 +43,7 @@ extension UnsafePointer where Pointee: ~Copyable /*& ~Escapable*/ { extension Span where Element: ~Copyable /*& ~Escapable*/ { - //FIXME: make failable once Optional can be non-escapable + //FIXME: make properly non-failable /// Unsafely create a `Span` over initialized memory. /// /// The memory in `buffer` must be owned by the instance `owner`, @@ -90,7 +90,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { extension Span where Element: BitwiseCopyable { - //FIXME: make failable once Optional can be non-escapable + //FIXME: make properly non-failable /// Unsafely create a `Span` over initialized memory. /// /// The memory in `buffer` must be owned by the instance `owner`, @@ -130,7 +130,7 @@ extension Span where Element: BitwiseCopyable { self.init(_unchecked: start, count: count, owner: owner) } - //FIXME: make failable once Optional can be non-escapable + //FIXME: make properly non-failable /// Unsafely create a `Span` over initialized memory. /// /// The memory in `unsafeBytes` must be owned by the instance `owner` @@ -399,7 +399,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `Span`. /// - /// - Returns: A `Span` over the items at `bounds` + /// - Returns: A `Span` over the items within `bounds` /// /// - Complexity: O(1) @inlinable @inline(__always) @@ -420,7 +420,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `Span`. /// - /// - Returns: A `Span` over the items at `bounds` + /// - Returns: A `Span` over the items within `bounds` /// /// - Complexity: O(1) @inlinable @inline(__always) @@ -442,7 +442,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `Span`. /// - /// - Returns: A `Span` over the items at `bounds` + /// - Returns: A `Span` over the items within `bounds` /// /// - Complexity: O(1) @_alwaysEmitIntoClient @@ -462,7 +462,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Parameter bounds: A valid range of positions. Every position in /// this range must be within the bounds of this `Span`. /// - /// - Returns: A `Span` over the items at `bounds` + /// - Returns: A `Span` over the items within `bounds` /// /// - Complexity: O(1) @_alwaysEmitIntoClient From 4a20e6c3c9ba96eb5c79226bdb16e26c68e231a6 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Fri, 21 Jun 2024 12:50:42 -0700 Subject: [PATCH 2/4] remove some keywords that are inferred by default --- Sources/Future/RawSpan.swift | 22 +++++++++---------- Sources/Future/Span.swift | 42 ++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index ef5cd4d1..fd8be4a7 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -24,7 +24,7 @@ public struct RawSpan: Copyable, ~Escapable { _unchecked start: UnsafeRawPointer, byteCount: Int, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { self._start = start self._count = byteCount } @@ -265,11 +265,9 @@ extension RawSpan { /// The closure's parameter is valid only for the duration of /// its execution. /// - Returns: The return value of the `body` closure parameter. - borrowing public func withUnsafeBytes< - E: Error, Result: ~Copyable & ~Escapable - >( - _ body: (_ buffer: borrowing UnsafeRawBufferPointer) throws(E) -> Result - ) throws(E) -> dependsOn(self) Result { + public func withUnsafeBytes( + _ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result + ) throws(E) -> Result { try body(.init(start: (byteCount==0) ? nil : _start, count: byteCount)) } } @@ -279,9 +277,9 @@ extension RawSpan { /// /// - Parameter type: The type as which we should view /// - Returns: A typed span viewing these bytes as T - borrowing public func view( + public func view( as type: T.Type - ) -> dependsOn(self) Span { + ) -> Span { Span(unsafeStart: _start, byteCount: byteCount, owner: self) } } @@ -388,7 +386,7 @@ extension RawSpan { /// - Returns: A span with at most `maxLength` bytes. /// /// - Complexity: O(1) - borrowing public func extracting(first maxLength: Int) -> Self { + public func extracting(first maxLength: Int) -> Self { precondition(maxLength >= 0, "Can't have a prefix of negative length.") let nc = maxLength < byteCount ? maxLength : byteCount return Self(_unchecked: _start, byteCount: nc, owner: self) @@ -404,7 +402,7 @@ extension RawSpan { /// - Returns: A span leaving off the specified number of bytes at the end. /// /// - Complexity: O(1) - borrowing public func extracting(droppingLast k: Int) -> Self { + public func extracting(droppingLast k: Int) -> Self { precondition(k >= 0, "Can't drop a negative number of elements.") let nc = k < byteCount ? byteCount&-k : 0 return Self(_unchecked: _start, byteCount: nc, owner: self) @@ -421,7 +419,7 @@ extension RawSpan { /// - Returns: A span with at most `maxLength` bytes. /// /// - Complexity: O(1) - borrowing public func extracting(last maxLength: Int) -> Self { + public func extracting(last maxLength: Int) -> Self { precondition(maxLength >= 0, "Can't have a suffix of negative length.") let nc = maxLength < byteCount ? maxLength : byteCount let newStart = _start.advanced(by: byteCount&-nc) @@ -438,7 +436,7 @@ extension RawSpan { /// - Returns: A span starting after the specified number of bytes. /// /// - Complexity: O(1) - borrowing public func extracting(droppingFirst k: Int = 1) -> Self { + public func extracting(droppingFirst k: Int = 1) -> Self { precondition(k >= 0, "Can't drop a negative number of elements.") let dc = k < byteCount ? k : byteCount let newStart = _start.advanced(by: dc) diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index 80b983d3..9df6baa9 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -24,7 +24,7 @@ public struct Span: Copyable, ~Escapable { _unchecked start: UnsafePointer, count: Int, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { self._start = start self._count = count } @@ -56,7 +56,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { public init( unsafeElements buffer: UnsafeBufferPointer, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { guard let baseAddress = buffer.baseAddress else { fatalError("Span requires a non-nil base address") } @@ -78,7 +78,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { unsafeStart start: UnsafePointer, count: Int, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { precondition(count >= 0, "Count must not be negative") precondition( start.isAligned, @@ -103,7 +103,7 @@ extension Span where Element: BitwiseCopyable { public init( unsafeElements buffer: UnsafeBufferPointer, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { guard let baseAddress = buffer.baseAddress else { fatalError("Span requires a non-nil base address") } @@ -125,7 +125,7 @@ extension Span where Element: BitwiseCopyable { unsafeStart start: UnsafePointer, count: Int, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { precondition(count >= 0, "Count must not be negative") self.init(_unchecked: start, count: count, owner: owner) } @@ -148,7 +148,7 @@ extension Span where Element: BitwiseCopyable { public init( unsafeBytes buffer: UnsafeRawBufferPointer, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { guard let baseAddress = buffer.baseAddress else { fatalError("Span requires a non-nil base address") } @@ -177,7 +177,7 @@ extension Span where Element: BitwiseCopyable { unsafeStart pointer: UnsafeRawPointer, byteCount: Int, owner: borrowing Owner - ) -> dependsOn(owner) Self { + ) { let stride = MemoryLayout.stride let (q, r) = byteCount.quotientAndRemainder(dividingBy: stride) precondition(r == 0) @@ -332,7 +332,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Complexity: O(1) @inlinable @inline(__always) public subscript(_ position: Int) -> Element { - borrowing _read { + _read { boundsCheckPrecondition(position) yield self[unchecked: position] } @@ -348,7 +348,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Complexity: O(1) @inlinable @inline(__always) public subscript(unchecked position: Int) -> Element { - borrowing _read { + _read { yield _start.advanced(by: position).pointee } } @@ -488,7 +488,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { } //MARK: withUnsafePointer, etc. -extension Span where Element: ~Copyable { +extension Span where Element: ~Copyable /*& ~Escapable*/ { //FIXME: mark closure parameter as non-escaping /// Calls a closure with a pointer to the viewed contiguous storage. @@ -503,11 +503,9 @@ extension Span where Element: ~Copyable { /// for the `withUnsafeBufferPointer(_:)` method. The closure's /// parameter is valid only for the duration of its execution. /// - Returns: The return value of the `body` closure parameter. - borrowing public func withUnsafeBufferPointer< - E: Error, Result: ~Copyable & ~Escapable - >( - _ body: (_ buffer: borrowing UnsafeBufferPointer) throws(E) -> Result - ) throws(E) -> dependsOn(self) Result { + public func withUnsafeBufferPointer( + _ body: (_ buffer: UnsafeBufferPointer) throws(E) -> Result + ) throws(E) -> Result { try body(.init(start: (count==0) ? nil : _start, count: count)) } } @@ -529,10 +527,8 @@ extension Span where Element: BitwiseCopyable { /// The closure's parameter is valid only for the duration of /// its execution. /// - Returns: The return value of the `body` closure parameter. - borrowing public func withUnsafeBytes< - E: Error, Result: ~Copyable & ~Escapable - >( - _ body: (_ buffer: borrowing UnsafeRawBufferPointer) throws(E) -> Result + public func withUnsafeBytes( + _ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result ) throws(E) -> Result { try RawSpan(self).withUnsafeBytes(body) } @@ -577,7 +573,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Returns: A span with at most `maxLength` elements. /// /// - Complexity: O(1) - borrowing public func extracting(first maxLength: Int) -> Self { + public func extracting(first maxLength: Int) -> Self { precondition(maxLength >= 0, "Can't have a prefix of negative length.") let nc = maxLength < count ? maxLength : count return Self(_unchecked: _start, count: nc, owner: self) @@ -593,7 +589,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Returns: A span leaving off the specified number of elements at the end. /// /// - Complexity: O(1) - borrowing public func extracting(droppingLast k: Int) -> Self { + public func extracting(droppingLast k: Int) -> Self { precondition(k >= 0, "Can't drop a negative number of elements.") let nc = k < count ? count&-k : 0 return Self(_unchecked: _start, count: nc, owner: self) @@ -610,7 +606,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Returns: A span with at most `maxLength` elements. /// /// - Complexity: O(1) - borrowing public func extracting(last maxLength: Int) -> Self { + public func extracting(last maxLength: Int) -> Self { precondition(maxLength >= 0, "Can't have a suffix of negative length.") let nc = maxLength < count ? maxLength : count let newStart = _start.advanced(by: count&-nc) @@ -627,7 +623,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Returns: A span starting after the specified number of elements. /// /// - Complexity: O(1) - borrowing public func extracting(droppingFirst k: Int = 1) -> Self { + public func extracting(droppingFirst k: Int = 1) -> Self { precondition(k >= 0, "Can't drop a negative number of elements.") let dc = k < count ? k : count let newStart = _start.advanced(by: dc) From 683e1cfb985afc4dd566bf4ba0a32e9207e4b930 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Fri, 21 Jun 2024 12:51:36 -0700 Subject: [PATCH 3/4] make `withUnsafe*` functions be inlined --- Sources/Future/RawSpan.swift | 1 + Sources/Future/Span.swift | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index fd8be4a7..5463b795 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -265,6 +265,7 @@ extension RawSpan { /// The closure's parameter is valid only for the duration of /// its execution. /// - Returns: The return value of the `body` closure parameter. + @_alwaysEmitIntoClient public func withUnsafeBytes( _ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result ) throws(E) -> Result { diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index 9df6baa9..9e339019 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -503,6 +503,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// for the `withUnsafeBufferPointer(_:)` method. The closure's /// parameter is valid only for the duration of its execution. /// - Returns: The return value of the `body` closure parameter. + @_alwaysEmitIntoClient public func withUnsafeBufferPointer( _ body: (_ buffer: UnsafeBufferPointer) throws(E) -> Result ) throws(E) -> Result { @@ -527,6 +528,7 @@ extension Span where Element: BitwiseCopyable { /// The closure's parameter is valid only for the duration of /// its execution. /// - Returns: The return value of the `body` closure parameter. + @_alwaysEmitIntoClient public func withUnsafeBytes( _ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result ) throws(E) -> Result { From ce574378f856d15eec794157225005947534a6ca Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 25 Jun 2024 10:27:59 -0700 Subject: [PATCH 4/4] improve bounds-checking and bounds-validation api --- Sources/Future/RawSpan.swift | 44 ++++++++++++++++++++-------- Sources/Future/Span.swift | 38 +++++++++++++++++------- Tests/FutureTests/RawSpanTests.swift | 4 +-- 3 files changed, 61 insertions(+), 25 deletions(-) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index 5463b795..905a5533 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -122,27 +122,45 @@ extension RawSpan { //MARK: Bounds Checking extension RawSpan { + /// Return true if `offset` is a valid offset into this `RawSpan` + /// + /// - Parameters: + /// - position: an index to validate + /// - Returns: true if `offset` is a valid index + @inlinable @inline(__always) + public func validateBounds(_ offset: Int) -> Bool { + 0 <= offset && offset < byteCount + } + /// Traps if `offset` is not a valid offset into this `RawSpan` /// /// - Parameters: - /// - position: an offset to validate + /// - position: an index to validate @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offset: Int) { + public func assertValidity(_ offset: Int) { precondition( - 0 <= offset && offset < byteCount, - "Offset out of bounds" + validateBounds(offset), "Offset out of bounds" ) } - /// Traps if `bounds` 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 offsets to validate + /// - offsets: a range of indices to validate + /// - Returns: true if `offsets` is a valid range of indices @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offsets: Range) { + public func validateBounds(_ offsets: Range) -> Bool { + 0 <= offsets.lowerBound && offsets.upperBound <= byteCount + } + + /// Traps if `offsets` is not a valid range of offsets into this `RawSpan` + /// + /// - Parameters: + /// - offsets: a range of indices to validate + @inlinable @inline(__always) + public func assertValidity(_ offsets: Range) { precondition( - 0 <= offsets.lowerBound && offsets.upperBound <= byteCount, - "Range of offsets out of bounds" + validateBounds(offsets), "Range of offsets out of bounds" ) } } @@ -165,7 +183,7 @@ extension RawSpan { /// - Complexity: O(1) @inlinable @inline(__always) public func extracting(_ bounds: Range) -> Self { - boundsCheckPrecondition(bounds) + assertValidity(bounds) return extracting(uncheckedBounds: bounds) } @@ -305,7 +323,7 @@ extension RawSpan { public func load( fromByteOffset offset: Int = 0, as: T.Type ) -> T { - boundsCheckPrecondition( + assertValidity( Range(uncheckedBounds: (offset, offset+MemoryLayout.size)) ) return load(fromUncheckedByteOffset: offset, as: T.self) @@ -347,7 +365,7 @@ extension RawSpan { public func loadUnaligned( fromByteOffset offset: Int = 0, as: T.Type ) -> T { - boundsCheckPrecondition( + assertValidity( Range(uncheckedBounds: (offset, offset+MemoryLayout.size)) ) return loadUnaligned(fromUncheckedByteOffset: offset, as: T.self) @@ -507,7 +525,7 @@ extension RawSpan { @inlinable public init(_ base: RawSpan, in range: Range) { - base.boundsCheckPrecondition(range) + base.assertValidity(range) position = 0 self.base = base parseRange = range diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index 9e339019..f3dc0326 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -287,27 +287,45 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { //MARK: Bounds Checking extension Span where Element: ~Copyable /*& ~Escapable*/ { + /// Return true if `offset` is a valid offset into this `Span` + /// + /// - Parameters: + /// - position: an index to validate + /// - Returns: true if `offset` is a valid index + @inlinable @inline(__always) + public func validateBounds(_ offset: Int) -> Bool { + 0 <= offset && offset < count + } + /// Traps if `offset` is not a valid offset into this `Span` /// /// - Parameters: - /// - position: an Index to validate + /// - position: an index to validate @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offset: Int) { + public func assertValidity(_ offset: Int) { precondition( - 0 <= offset && offset < count, - "Offset out of bounds" + validateBounds(offset), "Offset out of bounds" ) } + /// Return true if `offsets` is a valid range of offsets into this `Span` + /// + /// - Parameters: + /// - offsets: a range of indices to validate + /// - Returns: true if `offsets` is a valid range of indices + @inlinable @inline(__always) + public func validateBounds(_ offsets: Range) -> Bool { + 0 <= offsets.lowerBound && offsets.upperBound <= count + } + /// Traps if `offsets` is not a valid range of offsets into this `Span` /// /// - Parameters: /// - offsets: a range of indices to validate @inlinable @inline(__always) - public func boundsCheckPrecondition(_ offsets: Range) { + public func assertValidity(_ offsets: Range) { precondition( - 0 <= offsets.lowerBound && offsets.upperBound <= count, - "Range of offsets out of bounds" + validateBounds(offsets), "Range of offsets out of bounds" ) } } @@ -333,7 +351,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { @inlinable @inline(__always) public subscript(_ position: Int) -> Element { _read { - boundsCheckPrecondition(position) + assertValidity(position) yield self[unchecked: position] } } @@ -365,7 +383,7 @@ extension Span where Element: BitwiseCopyable { @inlinable @inline(__always) public subscript(_ position: Int) -> Element { get { - boundsCheckPrecondition(position) + assertValidity(position) return self[unchecked: position] } } @@ -404,7 +422,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// - Complexity: O(1) @inlinable @inline(__always) public func extracting(_ bounds: Range) -> Self { - boundsCheckPrecondition(bounds) + assertValidity(bounds) return extracting(uncheckedBounds: bounds) } diff --git a/Tests/FutureTests/RawSpanTests.swift b/Tests/FutureTests/RawSpanTests.swift index d8e193a6..2a4e93a7 100644 --- a/Tests/FutureTests/RawSpanTests.swift +++ b/Tests/FutureTests/RawSpanTests.swift @@ -208,8 +208,8 @@ final class RawSpanTests: XCTestCase { let a = Array(0..