From c0d02309058aa4018981a968cc6e3d1927cb1f6f Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 16 Jul 2024 08:50:19 -0700 Subject: [PATCH 1/5] add convenience initializers for `RawSpan` --- Sources/Future/RawSpan.swift | 17 ++++++++++++++ Tests/FutureTests/RawSpanTests.swift | 33 ++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index 8f522076..87e1a537 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -58,6 +58,23 @@ extension RawSpan { ) } + /// Unsafely create a `RawSpan` over initialized memory. + /// + /// The memory in `buffer` must be owned by the instance `owner`, + /// meaning that as long as `owner` is alive the memory will remain valid. + /// + /// - Parameters: + /// - buffer: an `UnsafeMutableRawBufferPointer` to initialized memory. + /// - owner: a binding whose lifetime must exceed that of + /// the newly created `RawSpan`. + @_alwaysEmitIntoClient + public init( + unsafeBytes buffer: UnsafeMutableRawBufferPointer, + owner: borrowing Owner + ) { + self.init(unsafeBytes: UnsafeRawBufferPointer(buffer), owner: owner) + } + /// Unsafely create a `RawSpan` over initialized memory. /// /// The memory over `count` bytes starting at diff --git a/Tests/FutureTests/RawSpanTests.swift b/Tests/FutureTests/RawSpanTests.swift index 70cd9b5c..bea88a64 100644 --- a/Tests/FutureTests/RawSpanTests.swift +++ b/Tests/FutureTests/RawSpanTests.swift @@ -43,16 +43,21 @@ final class RawSpanTests: XCTestCase { func testInitWithRawBytes() { let capacity = 4 - let a = Array(0...stride) } + + a.withUnsafeMutableBytes { + let span = RawSpan(unsafeBytes: $0, owner: $0) + XCTAssertEqual(span.byteCount, capacity*MemoryLayout.stride) + } } func testWithRawPointer() { let capacity = 4 - let a = Array(0...stride, owner: a ) - XCTAssertEqual(span.byteCount, capacity*MemoryLayout.stride) + XCTAssertEqual(span.byteCount, $0.count) + } + + a.withUnsafeMutableBytes { + let pointer = $0.baseAddress! + let span = RawSpan( + unsafeStart: pointer, + byteCount: capacity*MemoryLayout.stride, + owner: $0 + ) + XCTAssertEqual(span.byteCount, $0.count) } } @@ -233,13 +248,15 @@ final class RawSpanTests: XCTestCase { let b = UnsafeMutableRawBufferPointer.allocate(byteCount: 8, alignment: 8) defer { b.deallocate() } - let span = RawSpan(unsafeBytes: .init(b), owner: b) + let span = RawSpan(unsafeBytes: b, owner: b) let subSpan = span.extracting(last: 2) let emptySpan = span.extracting(first: 0) let fakeSpan = RawSpan( unsafeStart: b.baseAddress!.advanced(by: 8), byteCount: 8, owner: b ) - let nilSpan = RawSpan(unsafeBytes: .init(start: nil, count: 0), owner: b) + let nilSpan = RawSpan( + unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0), owner: b + ) XCTAssertTrue(span.contains(subSpan)) XCTAssertFalse(subSpan.contains(span)) @@ -256,10 +273,12 @@ final class RawSpanTests: XCTestCase { let b = UnsafeMutableRawBufferPointer.allocate(byteCount: 8, alignment: 8) defer { b.deallocate() } - let span = RawSpan(unsafeBytes: .init(b), owner: b) + let span = RawSpan(unsafeBytes: b, owner: b) let subSpan = span.extracting(last: 2) let emptySpan = span.extracting(first: 0) - let nilSpan = RawSpan(unsafeBytes: .init(start: nil, count: 0), owner: b) + let nilSpan = RawSpan( + unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0), owner: b + ) var bounds: Range bounds = span.offsets(of: subSpan) From fe649d9a4ca841462d9d50ab6a7f1c15d6a507bc Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 16 Jul 2024 08:32:00 -0700 Subject: [PATCH 2/5] add convenience initializers for `Span` --- Sources/Future/Span.swift | 56 ++++++++++++++++++++++++++++++ Tests/FutureTests/SpanTests.swift | 57 +++++++++++++++++++++++++++---- 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index 736d3621..7fb571f4 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -79,6 +79,23 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { self.init(_unchecked: buffer, owner: owner) } + /// Unsafely create a `Span` over initialized memory. + /// + /// The memory in `buffer` must be owned by the instance `owner`, + /// meaning that as long as `owner` is alive the memory will remain valid. + /// + /// - Parameters: + /// - buffer: an `UnsafeMutableBufferPointer` to initialized elements. + /// - owner: a binding whose lifetime must exceed that of + /// the newly created `Span`. + @_alwaysEmitIntoClient + public init( + unsafeElements buffer: UnsafeMutableBufferPointer, + owner: borrowing Owner + ) { + self.init(unsafeElements: UnsafeBufferPointer(buffer), owner: owner) + } + /// Unsafely create a `Span` over initialized memory. /// /// The memory representing `count` instances starting at @@ -124,6 +141,23 @@ extension Span where Element: BitwiseCopyable { self.init(_unchecked: buffer, owner: owner) } + /// Unsafely create a `Span` over initialized memory. + /// + /// The memory in `buffer` must be owned by the instance `owner`, + /// meaning that as long as `owner` is alive the memory will remain valid. + /// + /// - Parameters: + /// - buffer: an `UnsafeMutableBufferPointer` to initialized elements. + /// - owner: a binding whose lifetime must exceed that of + /// the newly created `Span`. + @_alwaysEmitIntoClient + public init( + unsafeElements buffer: UnsafeMutableBufferPointer, + owner: borrowing Owner + ) { + self.init(unsafeElements: UnsafeBufferPointer(buffer), owner: owner) + } + /// Unsafely create a `Span` over initialized memory. /// /// The memory representing `count` instances starting at @@ -175,6 +209,28 @@ extension Span where Element: BitwiseCopyable { ) } + /// Unsafely create a `Span` over initialized memory. + /// + /// The memory in `unsafeBytes` must be owned by the instance `owner` + /// meaning that as long as `owner` is alive the memory will remain valid. + /// + /// `unsafeBytes` must be correctly aligned for accessing + /// an element of type `Element`, and must contain a number of bytes + /// that is an exact multiple of `Element`'s stride. + /// + /// - Parameters: + /// - unsafeBytes: a buffer to initialized elements. + /// - type: the type to use when interpreting the bytes in memory. + /// - owner: a binding whose lifetime must exceed that of + /// the newly created `Span`. + @_alwaysEmitIntoClient + public init( + unsafeBytes buffer: UnsafeMutableRawBufferPointer, + owner: borrowing Owner + ) { + self.init(unsafeBytes: UnsafeRawBufferPointer(buffer), owner: owner) + } + /// Unsafely create a `Span` over initialized memory. /// /// The memory representing `count` instances starting at diff --git a/Tests/FutureTests/SpanTests.swift b/Tests/FutureTests/SpanTests.swift index 42b6541f..8c9d3248 100644 --- a/Tests/FutureTests/SpanTests.swift +++ b/Tests/FutureTests/SpanTests.swift @@ -29,18 +29,42 @@ final class SpanTests: XCTestCase { func testInitWithOrdinaryElement() { let capacity = 4 - let s = (0..(unsafeBytes: $0, owner: $0) XCTAssertEqual(r.count, capacity*MemoryLayout.stride) + + let p = Span( + unsafeStart: $0.baseAddress!, + byteCount: capacity*MemoryLayout.stride, + owner: $0 + ) + XCTAssertEqual(p.count, capacity) + } + + a.withUnsafeMutableBytes { + let b = Span(unsafeBytes: $0, owner: $0) + XCTAssertEqual(b.count, capacity) + + let p = Span( + unsafeStart: $0.baseAddress!, + byteCount: capacity*MemoryLayout.stride, + owner: $0 + ) + XCTAssertEqual(p.count, capacity) } } @@ -328,7 +371,7 @@ final class SpanTests: XCTestCase { unsafeStart: b.baseAddress!.advanced(by: 8), count: 8, owner: b ) let nilSpan = Span( - unsafeElements: .init(start: nil, count: 0), owner: b + unsafeElements: UnsafeBufferPointer(start: nil, count: 0), owner: b ) XCTAssertTrue(span.contains(subSpan)) @@ -351,7 +394,7 @@ final class SpanTests: XCTestCase { let subSpan = span.extracting(last: 2) let emptySpan = span.extracting(first: 0) let nilSpan = Span( - unsafeElements: .init(start: nil, count: 0), owner: b + unsafeElements: UnsafeBufferPointer(start: nil, count: 0), owner: b ) var bounds: Range From c611501b0a6db9591a47af3961b328a498a0abb3 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 16 Jul 2024 09:09:44 -0700 Subject: [PATCH 3/5] mark _indices and _byteOffsets as @_aEIC --- Sources/Future/RawSpan.swift | 2 +- Sources/Future/Span.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index 87e1a537..3028f11f 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -132,7 +132,7 @@ extension RawSpan { /// order. /// /// - Complexity: O(1) - @inlinable @inline(__always) + @_alwaysEmitIntoClient public var _byteOffsets: Range { .init(uncheckedBounds: (0, byteCount)) } diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index 7fb571f4..2d38e2aa 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -349,7 +349,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// order. /// /// - Complexity: O(1) - @inlinable @inline(__always) + @_alwaysEmitIntoClient public var _indices: Range { .init(uncheckedBounds: (0, _count)) } From 296f94c2d84ff76311dd05ba5e4fa49a64618e7e Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 16 Jul 2024 13:30:29 -0700 Subject: [PATCH 4/5] remove undesirable default values --- Sources/Future/RawSpan.swift | 2 +- Sources/Future/Span.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index 3028f11f..8eac660a 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -546,7 +546,7 @@ extension RawSpan { /// /// - Complexity: O(1) @inlinable - public func extracting(droppingFirst k: Int = 1) -> Self { + public 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) diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index 2d38e2aa..f6258044 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -757,7 +757,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { /// /// - Complexity: O(1) @inlinable - public func extracting(droppingFirst k: Int = 1) -> Self { + public func extracting(droppingFirst k: Int) -> Self { precondition(k >= 0, "Can't drop a negative number of elements.") let droppedCount = min(k, count) let newStart = _pointer?.advanced(by: droppedCount) From 93efdb9e4e967a0ad0cb735721b0a67447cd12b2 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Tue, 16 Jul 2024 13:30:51 -0700 Subject: [PATCH 5/5] add doc-comments for the containment utilities --- Sources/Future/RawSpan.swift | 14 ++++++++++++++ Sources/Future/Span.swift | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Sources/Future/RawSpan.swift b/Sources/Future/RawSpan.swift index 8eac660a..4b1058b5 100644 --- a/Sources/Future/RawSpan.swift +++ b/Sources/Future/RawSpan.swift @@ -442,6 +442,12 @@ extension RawSpan { extension RawSpan { + /// Returns true if the memory represented by `span` is a subrange of + /// the memory represented by `self` + /// + /// Parameters: + /// - span: a span of the same type as `self` + /// Returns: whether `span` is a subrange of `self` @inlinable @inline(__always) public func contains(_ span: borrowing Self) -> Bool { if span._count > _count { return false } @@ -450,6 +456,14 @@ extension RawSpan { return span._start.advanced(by: span._count) <= _start.advanced(by: _count) } + /// Returns the offsets where the memory of `span` is located within + /// the memory represented by `self` + /// + /// Note: `span` must be a subrange of `self` + /// + /// Parameters: + /// - span: a subrange of `self` + /// Returns: A range of offsets within `self` @inlinable @inline(__always) public func offsets(of span: borrowing Self) -> Range { precondition(contains(span)) diff --git a/Sources/Future/Span.swift b/Sources/Future/Span.swift index f6258044..7f05fff9 100644 --- a/Sources/Future/Span.swift +++ b/Sources/Future/Span.swift @@ -653,6 +653,12 @@ extension Span where Element: Copyable { extension Span where Element: ~Copyable /*& ~Escapable*/ { + /// Returns true if the memory represented by `span` is a subrange of + /// the memory represented by `self` + /// + /// Parameters: + /// - span: a span of the same type as `self` + /// Returns: whether `span` is a subrange of `self` @inlinable @inline(__always) public func contains(_ span: borrowing Self) -> Bool { if span._count > _count { return false } @@ -661,6 +667,14 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ { return span._start.advanced(by: span._count) <= _start.advanced(by: _count) } + /// Returns the offsets where the memory of `span` is located within + /// the memory represented by `self` + /// + /// Note: `span` must be a subrange of `self` + /// + /// Parameters: + /// - span: a subrange of `self` + /// Returns: A range of offsets within `self` @inlinable @inline(__always) public func offsets(of span: borrowing Self) -> Range { precondition(contains(span))