diff --git a/Package.swift b/Package.swift index cb91a8ba..4c4c5404 100644 --- a/Package.swift +++ b/Package.swift @@ -313,6 +313,15 @@ let targets: [CustomTarget] = [ name: "RopeModuleTests", dependencies: ["_RopeModule", "_CollectionsTestSupport"]), + .target( + kind: .exported, + name: "TrailingElementsModule", + exclude: ["CMakeLists.txt"]), + .target( + kind: .test, + name: "TrailingElementsTests", + dependencies: ["TrailingElementsModule"]), + // These aren't ready for production use yet. // .target( // kind: .exported, @@ -334,6 +343,7 @@ let targets: [CustomTarget] = [ "HeapModule", "OrderedCollections", "_RopeModule", + "TrailingElementsModule", //"SortedCollections", ], exclude: ["CMakeLists.txt"]) diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index e98233cb..7b1972d4 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -62,6 +62,7 @@ if(NOT COLLECTIONS_FOUNDATION_TOOLCHAIN_MODULE) add_subdirectory(BitCollections) add_subdirectory(HashTreeCollections) add_subdirectory(HeapModule) + add_subdirectory(TrailingElementsModule) endif() add_subdirectory(DequeModule) add_subdirectory(OrderedCollections) diff --git a/Sources/TrailingElementsModule/CMakeLists.txt b/Sources/TrailingElementsModule/CMakeLists.txt new file mode 100644 index 00000000..02a40686 --- /dev/null +++ b/Sources/TrailingElementsModule/CMakeLists.txt @@ -0,0 +1,25 @@ +#[[ +This source file is part of the Swift Collections Open Source Project + +Copyright (c) 2021 - 2024 Apple Inc. and the Swift project authors +Licensed under Apache License v2.0 with Runtime Library Exception + +See https://swift.org/LICENSE.txt for license information +#]] + +if(COLLECTIONS_SINGLE_MODULE) + set(module_name ${COLLECTIONS_MODULE_NAME}) +else() + set(module_name TrailingElementsModule) + add_library(TrailingElementsModule) + set_target_properties(TrailingElementsModule PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY}) + + _install_target(TrailingElementsModule) + set_property(GLOBAL APPEND PROPERTY SWIFT_COLLECTIONS_EXPORTS TrailingElementsModule) +endif() + +target_sources(${module_name} PRIVATE + "IntrusiveManagedBuffer.swift" + "PaddedStorage.swift" + "TrailingElements.swift") diff --git a/Sources/TrailingElementsModule/IntrusiveManagedBuffer.swift b/Sources/TrailingElementsModule/IntrusiveManagedBuffer.swift new file mode 100644 index 00000000..99dca9e1 --- /dev/null +++ b/Sources/TrailingElementsModule/IntrusiveManagedBuffer.swift @@ -0,0 +1,323 @@ +/// A value that manages a contiguous block of memory starting with a header +/// value and then followed by a contiguous array of elements. Values of this +/// type own the underlying memory, and are non-copyable to ensure that +/// ownership of that memory is unique. +/// +/// The number of elements stored after the header must be computable based on +/// the information in the header (via the `trailingCount` property), so that +/// it is not stored separately. +@frozen +public struct IntrusiveManagedBuffer: ~Copyable + where Header: ~Copyable +{ + /// The underlying storage. + @usableFromInline + var pointer: UnsafeMutablePointer
+ + /// The element type stored within the buffer. + public typealias Element = Header.Element + + /// Allocate storage and initialize only the header, leaving the trailing + /// elements uninitialized. This is private because it can compromise + /// memory safety. + @_alwaysEmitIntoClient + private init(_headerOnly header: consuming Header) { + pointer = UnsafeMutableRawPointer.allocate( + byteCount: Self.allocationSize( + header: header + ), + alignment: MemoryLayout
.alignment + ).assumingMemoryBound(to: Header.self) + + pointer.initialize(to: header) + } + + @available(SwiftStdlib 5.1, *) + @_alwaysEmitIntoClient + mutating func _initializeTrailingElements( + initializer: (inout OutputSpan) throws(E) -> Void + ) throws(E) { + var output = unsafe OutputSpan(buffer: rawElements, initializedCount: 0) + try initializer(&output) + let initialized = unsafe output.finalize(for: rawElements) + precondition(count == initialized, "IntrusiveManagedBuffer initialization underflow") + } + + /// Allocate an intrusive managed buffer with the given header and calling + /// the initializer to fill in the trailing elements. + @available(SwiftStdlib 5.1, *) + @_alwaysEmitIntoClient + public init( + header: consuming Header, + initializingTrailingElementsWith initializer: (inout OutputSpan) throws(E) -> Void + ) throws(E) { + self.init(_headerOnly: header) + try _initializeTrailingElements(initializer: initializer) + } + + /// Allocate an intrusive managed buffer with the given header and + /// initializing each trailing element with the given `element`. + @_alwaysEmitIntoClient + public init(header: consuming Header, repeating element: Element) { + self.init(_headerOnly: header) + rawElements.initialize(repeating: element) + } + + /// Deinitialize each of the trailing elements, then the header, then + /// deallocate the underlying storage. + @_alwaysEmitIntoClient + deinit { + rawElements.deinitialize() + pointer.deinitialize(count: 1) + pointer.deallocate() + } + + /// Take ownership over a pointer to memory containing the header followed + /// by the trailing elements. + /// + /// Once this managed buffer instance is no longer used, this memory will + /// be freed. + @_alwaysEmitIntoClient + public init(consuming pointer: UnsafeMutablePointer
) { + self.pointer = pointer + } + + /// Return the pointer to the underlying memory, including ownership over + /// that memory. The underlying storage will not be freed by this buffer; + /// it is the responsibility of the caller. + @_alwaysEmitIntoClient + public consuming func takePointer() -> UnsafeMutablePointer
{ + let pointer = self.pointer + discard self + return pointer + } + + /// Access the header portion of the buffer. + @_alwaysEmitIntoClient + public var header: Header { + unsafeAddress { + UnsafePointer(pointer) + } + + unsafeMutableAddress { + pointer + } + } + + /// The number of trailing elements in the value. + @_alwaysEmitIntoClient + public var count: Int { header.trailingCount } + + /// Starting index for accessing the trailing elements. Always 0 + @_alwaysEmitIntoClient + public var startIndex: Int { 0 } + + /// Ending index for accessing the trailing elements. Always `count`. + @_alwaysEmitIntoClient + public var endIndex: Int { count } + + /// Indices covering all of the trailing elements. Always `0.. { 0.. Element { + get { + precondition(index >= 0 && index < count) + return rawElements[index] + } + + set { + precondition(index >= 0 && index < count) + rawElements[index] = newValue + } + } + + /// Access the flexible array elements. + @_alwaysEmitIntoClient + var rawElements: UnsafeMutableBufferPointer { + UnsafeMutableBufferPointer( + start: UnsafeMutableRawPointer(pointer.advanced(by: 1)) + .assumingMemoryBound(to: Element.self), + count: header.trailingCount + ) + } + + /// Accesses the trailing elements following the header. + @available(SwiftStdlib 5.1, *) + @_alwaysEmitIntoClient + public var elements: Span { + @_lifetime(self) + get { + _overrideLifetime(rawElements.span, borrowing: self) + } + } + + /// Accesses the trailing elements following the header, allowing mutation + /// of those elements. + @available(SwiftStdlib 5.1, *) + @_alwaysEmitIntoClient + public var mutableElements: MutableSpan { + @_lifetime(self) + mutating get { + let elements = self.rawElements.mutableSpan + return _overrideLifetime(elements, mutating: &self) + } + } + + /// Execute the given closure, providing it with an unsafe buffer pointer + /// referencing the header. + @_alwaysEmitIntoClient + public mutating func withUnsafeMutablePointerToHeader(_ body: (UnsafeMutablePointer
) throws(E) -> R) throws(E) -> R { + return try body(pointer) + } + + /// Execute the given closure, providing it with an unsafe buffer pointer + /// referencing the trailing elements. + @_alwaysEmitIntoClient + public mutating func withUnsafeMutablePointerToElements(_ body: (UnsafeMutableBufferPointer) throws(E) -> R) throws(E) -> R { + return try body(rawElements) + } + + /// Execute the given closure, providing it with unsafe pointers to the + /// header and trailing elements, respectively. + @_alwaysEmitIntoClient + public mutating func withUnsafeMutablePointers( + _ body: (UnsafeMutablePointer
, UnsafeMutableBufferPointer) throws(E) -> R + ) throws(E) -> R { + return try body(pointer, rawElements) + } + + /// Determine the allocation size needed for the given header value. + @_alwaysEmitIntoClient + public static func allocationSize(header: borrowing Header) -> Int { + MemoryLayout
.size + MemoryLayout.stride * header.trailingCount + } +} + +extension IntrusiveManagedBuffer where Header: ~Copyable, Header.Element: BitwiseCopyable { + /// Allocate an intrusive managed buffer with the given header, but leaving the + /// trailing elements uninitialized. + @_alwaysEmitIntoClient + @unsafe + public init(header: consuming Header, uninitializedTrailingElements: ()) { + self.init(_headerOnly: header) + } +} + +extension IntrusiveManagedBuffer where Header: Copyable { + /// Create a temporary intrusive managed buffer for the given header, whose + /// trailing elements are initialized to copies of `element`. That instance + /// is provided to the given `body` to operate on for the duration of the + /// call. The temporary is allocated on the stack, unless it is very + /// large according to `withUnsafeTemporaryAllocation`. + @_alwaysEmitIntoClient + public static func withTemporaryValue( + header: consuming Header, + repeating element: Element, + body: (inout IntrusiveManagedBuffer
) throws(E) -> R + ) throws(E) -> R { + return try PaddedStorage
.withTemporaryValue( + header: header, + totalSize: allocationSize(header: header) + ) { (storage) throws(E) in + /// Create a managed buffer over that temporary storage. + var managedBuffer = IntrusiveManagedBuffer(consuming: storage.pointer) + managedBuffer.rawElements.initialize(repeating: element) + + do throws(E) { + let result = try body(&managedBuffer) + + // Tell the managed buffer not to free the storage. + let finalPointer = managedBuffer.takePointer() + precondition(finalPointer == storage.pointer) + + return result + } catch { + // Tell the managed buffer not to free the storage. + let finalPointer = managedBuffer.takePointer() + precondition(finalPointer == storage.pointer) + + throw error + } + } + } + + /// Create a temporary intrusive managed buffer for the given header, whose + /// trailing elements are initialized with the given `initializer` function. + /// That instance is provided to the given `body` to operate on for the + /// duration of the call. The temporary is allocated on the stack, unless + /// it is very large according to `withUnsafeTemporaryAllocation`. + @available(SwiftStdlib 5.1, *) + @_alwaysEmitIntoClient + public static func withTemporaryValue( + header: consuming Header, + initializingTrailingElementsWith initializer: (inout OutputSpan) throws(E) -> Void, + body: (inout IntrusiveManagedBuffer
) throws(E) -> R + ) throws(E) -> R { + return try PaddedStorage
.withTemporaryValue( + header: header, + totalSize: allocationSize(header: header) + ) { (storage) throws(E) in + /// Create a managed buffer over that temporary storage. + var managedBuffer = IntrusiveManagedBuffer(consuming: storage.pointer) + try managedBuffer._initializeTrailingElements(initializer: initializer) + + do throws(E) { + let result = try body(&managedBuffer) + + // Tell the managed buffer not to free the storage. + let finalPointer = managedBuffer.takePointer() + precondition(finalPointer == storage.pointer) + + return result + } catch { + // Tell the managed buffer not to free the storage. + let finalPointer = managedBuffer.takePointer() + precondition(finalPointer == storage.pointer) + + throw error + } + } + } +} + +extension IntrusiveManagedBuffer where Header.Element: BitwiseCopyable { + /// Create a temporary intrusive managed buffer for the given header, whose + /// trailing elements are left uninitialized. That instance is provided to + /// the given `body` to operate on for the duration of the + /// call. The temporary is allocated on the stack, unless it is very + /// large according to `withUnsafeTemporaryAllocation`. + @_alwaysEmitIntoClient + @unsafe + public static func withTemporaryValue( + header: consuming Header, + uninitializedTrailingElements: (), + body: (inout IntrusiveManagedBuffer
) throws(E) -> R + ) throws(E) -> R { + return try PaddedStorage
.withTemporaryValue( + header: header, + totalSize: allocationSize(header: header) + ) { (storage) throws(E) in + /// Create a managed buffer over that temporary storage. + var managedBuffer = IntrusiveManagedBuffer(consuming: storage.pointer) + + do throws(E) { + let result = try body(&managedBuffer) + + // Tell the managed buffer not to free the storage. + let finalPointer = managedBuffer.takePointer() + precondition(finalPointer == storage.pointer) + + return result + } catch { + // Tell the managed buffer not to free the storage. + let finalPointer = managedBuffer.takePointer() + precondition(finalPointer == storage.pointer) + + throw error + } + } + } +} diff --git a/Sources/TrailingElementsModule/PaddedStorage.swift b/Sources/TrailingElementsModule/PaddedStorage.swift new file mode 100644 index 00000000..d8474015 --- /dev/null +++ b/Sources/TrailingElementsModule/PaddedStorage.swift @@ -0,0 +1,126 @@ +/// Represents memory containing a header value followed by some extra padding +/// following it. Values of this type own the underlying memory, and are +/// non-copyable to ensure that ownership of that memory is unique. Memory +/// allocated for the storage will be deinitialized and freed when the +/// padded-storage instance is deinitialized. +/// +/// The extra padding referenced by this value is allocated, but is otherwise +/// inaccessible except through unsafe pointer manipulation. This is intended +/// to be used in cases where a common header can have some extra storage +/// following it, but that storage is not describable using the type +/// system or is otherwise opaque to the user. +/// +/// If the storage following the header is contiguous memory of some specific +/// type, whose count is computable from the header, use +/// `IntrusiveManagedPointer` instead. +@frozen +public struct PaddedStorage: ~Copyable { + /// Pointer to the header, followed by the padding. + @usableFromInline + let pointer: UnsafeMutablePointer
+ + /// Create a new instance with the given header and total size. The total + /// size includes the storage for the header itself, so it must be at least + /// as large as the header. + @_alwaysEmitIntoClient + public init(header: consuming Header, totalSize size: Int) { + precondition(size >= MemoryLayout
.size, + "must allocate enough storage for the underlying stored type") + pointer = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: MemoryLayout
.alignment) + .assumingMemoryBound(to: Header.self) + pointer.initialize(to: header) + } + + /// Take ownership over a pointer to memory containing the header followed + /// by the trailing elements. + /// + /// Once this padded storage is no longer used, this memory will be + /// deinitialized and freed. + @_alwaysEmitIntoClient + public init(consuming pointer: UnsafeMutablePointer
) { + self.pointer = pointer + } + + /// Deinitializes the header, then deallocates the underlying memory. + @_alwaysEmitIntoClient + deinit { + pointer.deinitialize(count: 1) + pointer.deallocate() + } + + /// Access the header portion of the value. + @_alwaysEmitIntoClient + public var header: Header { + unsafeAddress { + UnsafePointer(pointer) + } + + unsafeMutableAddress { + pointer + } + } + + /// Executes the given closure with the pointer to the header itself. + @_alwaysEmitIntoClient + public func withUnsafeMutablePointerToHeader(_ body: (UnsafeMutablePointer
) throws(E) -> R) throws(E) -> R { + return try body(pointer) + } + + /// Take ownership over the stored memory, returning its pointer. The + /// underlying storage will not be freed by the `PaddedStorage` instance, + /// as it is the responsibility of the caller. + @_alwaysEmitIntoClient + public consuming func takePointer() -> UnsafeMutablePointer
{ + let pointer = self.pointer + discard self + return pointer + } +} + +extension PaddedStorage: @unchecked Sendable where Header: Sendable, Header: ~Copyable { } + +extension PaddedStorage where Header: Copyable { + /// Create a temporary for the given header with additional storage, + /// providing that instance to the given `body` to operate on for the + /// duration. The temporary is allocated on the stack (unless it is very + /// large), eliminating the need for heap allocation of variable-size + /// values. + @_alwaysEmitIntoClient + public static func withTemporaryValue( + header: Header, + totalSize size: Int, + body: (inout PaddedStorage
) throws(E) -> R + ) throws(E) -> R { + precondition(size >= MemoryLayout
.size, + "must allocate enough storage for the underlying stored type") + + // Allocate temporary storage large enough for the value we need. + let result: Result = withUnsafeTemporaryAllocation( + byteCount: size, + alignment: MemoryLayout
.alignment + ) { buffer in + /// Create a tail-allocated storage over that temporary storage. + let pointer = buffer.baseAddress!.assumingMemoryBound(to: Header.self) + pointer.initialize(to: header) + var tailAllocated = PaddedStorage(consuming: pointer) + + do throws(E) { + let result = try body(&tailAllocated) + + // Tell the tail-allocated buffer not to free the storage. + let finalPointer = tailAllocated.takePointer() + precondition(finalPointer == pointer) + + return .success(result) + } catch { + // Tell the tail-allocated buffer not to free the storage. + let finalPointer = tailAllocated.takePointer() + precondition(finalPointer == pointer) + + return .failure(error) + } + } + + return try result.get() + } +} diff --git a/Sources/TrailingElementsModule/TrailingElements.swift b/Sources/TrailingElementsModule/TrailingElements.swift new file mode 100644 index 00000000..521fbcf8 --- /dev/null +++ b/Sources/TrailingElementsModule/TrailingElements.swift @@ -0,0 +1,10 @@ +/// Describes a type that has some number of elements following it directly +/// in memory. Such types are generally used with the `IntrusiveManagedBuffer` +/// type, which manages storage for the header and its trailing elements. +public protocol TrailingElements: ~Copyable { + /// The element type of the data that follows the header in memory. + associatedtype Element + + /// The number of elements following the header. + var trailingCount: Int { get } +} diff --git a/Sources/TrailingElementsModule/TrailingElementsModule.docc/TrailingElementsModule.md b/Sources/TrailingElementsModule/TrailingElementsModule.docc/TrailingElementsModule.md new file mode 100644 index 00000000..1fbcaded --- /dev/null +++ b/Sources/TrailingElementsModule/TrailingElementsModule.docc/TrailingElementsModule.md @@ -0,0 +1,115 @@ +# ``TrailingElementsModule`` + +## Overview + +This module provides types that manage values consisting of a fixed-size header +plus variable-size storage that follows that header in memory. The types are +similar in design to the [`ManagedBuffer`](https://developer.apple.com/documentation/swift/managedbuffer) type in the standard library, but are non-copyable types that eliminate the need for reference counting and heap allocation. + + +The ``IntrusiveManagedBuffer`` type can be used for types consisting of a header after which there are some (dynamic) number of elements. For example, we could have a `Coordinates` type that is followed by `Point` elements: + +```swift +struct Point { + var x: Int + var y: Int +} + +struct Coordinates { + var numPoints: Int + // Points will follow in memory +} +``` + +To use ``IntrusiveManagedBuffer``, the `Coordinates` type must conform to the +``TrailingElements`` protocol, specifying the type of the trailing elements (`Point`) and the number of trailing elements (`numPoints`), like this: + +```swift +extension Coordinates: TrailingElements { + typealias Element = Point + var trailingCount: Int { numPoints } +} +``` + +Now, we can create an intrusive managed buffer. The easiest way is to do so on the heap: + +```swift +var coords = IntrusiveManagedBuffer(header: Coordinates(numPoints: 3)) { outputSpan in + outputSpan.append(Point(x: 1, y: 2)) + outputSpan.append(Point(x: 2, y: 3)) + outputSpan.append(Point(x: 3, y: 4)) +} +``` + +Subscripting an ``IntrusiveManagedBuffer`` provides access to the elements. One can also use the ``IntrusiveManagedBuffer.elements`` or ``IntrusiveManagedBuffer.mutableElements`` properties to get a [`Span`](https://developer.apple.com/documentation/swift/span) or [`MutableSpan`](https://developer.apple.com/documentation/swift/mutablespan) over the elements, respectively. Once the `coords` value is no longer used, the buffer will be deallocated. + +```swift +print(coords[0]) // displays Point(x: 1, y: 2) +``` + +### Stack allocation +If heap allocation is not acceptable, use the static method ``IntrusiveManagedBuffer.withTemporaryValue`` to produce a temporary instance of ``IntrusiveManagedBuffer`` that can be used within the given body closure, like this: + +```swift +IntrusiveManagedBuffer.withTemporaryValue(header: Coordinates(numPoints: 3), repeating: Point(x: 0, y: 0)) { coords in + coords[0] = Point(x: 1, y: 2) + coords[1] = Point(x: 2, y: 3) + coords[2] = Point(x: 3, y: 4) + + // operate on coords +} +``` + +### Interoperability with unsafe C APIs +``IntrusiveManagedBuffer`` provides facilities for working with the underlying unsafe pointers, which can be useful when interoperating with C APIs. ``IntrusiveManagedBuffer`` can take ownership of a heap-allocated pointer to its `Header` type (such as one might get back from a C API api) using `init(consuming:)`: + +```swift +init(consuming pointer: UnsafeMutablePointer
) +``` + +or give up its pointer to hand off to C with `takePointer`: + +```swift +consuming func takePointer() -> UnsafeMutablePointer
+``` + +Unsafe pointers can be accessed via the `withUnsafeMutablePointer*` family of methods to access a pointer to the header, the trailing elements, or both. + +### Flexible array members +One of the uses for this module is to work with C types that involve [flexible array members](https://en.wikipedia.org/wiki/Flexible_array_member), which are described via an incomplete array at the end of a struct. Here is the C equivalent to the `Coordinates` structure shown above: + +```c +struct coordinates_t { + unsigned num_points; + struct point_t points[]; +}; +``` + +The C `coordinates_t` type can extended to conform to the ``TrailingElements`` protocol, allowing it to be used directly with ``IntrusiveManagedBuffer``: + +```swift +extension coordinates_t: TrailingElements { + typealias Element = point_t + var trailingCount: Int { num_points } +} +``` + +This provides safer access patterns for C flexible array members, introducing safe memory ownership and bounds-safety checking. + +### ``PaddedStorage`` + +This module also provides a lower-level primitive called ``PaddedStorage`` that references heap- or stack-allocated data with a particular type (also called the `Header` type) but whose allocation is larger than the size of the header itself. The API is roughly similar to that of ``IntrusiveManagedBuffer``, but it has no notion of what data might follow the `Header` instance. Instead, its initializer takes a "total size" for the allocation size, leaving the size calculation to the user: + +```swift +var padded = PaddedStorage(header: SomeType(), totalSize: getSizeOfSomeTypeWithPadding()) +``` + +## Topics + +### Structures + +- ``IntrusiveManagedBuffer`` +- ``PaddedStorage`` + +### Protocols +- ``TrailingElements` diff --git a/Tests/TrailingElementsTests/TrailingElementsTests.swift b/Tests/TrailingElementsTests/TrailingElementsTests.swift new file mode 100644 index 00000000..67a4b3cb --- /dev/null +++ b/Tests/TrailingElementsTests/TrailingElementsTests.swift @@ -0,0 +1,59 @@ +import Testing +import TrailingElementsModule + +struct Point { + var x: Int + var y: Int +} + +struct Coordinates { + var numCoordinates: Int +} + +extension Coordinates: TrailingElements { + typealias Element = Point + var trailingCount: Int { numCoordinates } +} + +@Suite("Trailing elements tests") +struct TrailingElementsTests { + @Test func simpleCoordinates() async throws { + var coords = IntrusiveManagedBuffer(header: Coordinates(numCoordinates: 3)) { outputSpan in + outputSpan.append(Point(x: 1, y: 2)) + outputSpan.append(Point(x: 2, y: 3)) + outputSpan.append(Point(x: 3, y: 4)) + } + + #expect(coords[0].x == 1) + #expect(coords[1].x == 2) + #expect(coords[2].x == 3) + + coords[1].y = 17 + #expect(coords[1].y == 17) + } + + @Test func repeatingInit() async throws { + var coords = IntrusiveManagedBuffer(header: Coordinates(numCoordinates: 3), repeating: Point(x: 1, y: 1)) + + #expect(coords[0].x == 1) + #expect(coords[1].x == 1) + #expect(coords[2].x == 1) + + coords[1].x = 17 + #expect(coords[0].x == 1) + #expect(coords[1].x == 17) + #expect(coords[2].x == 1) + } + + @Test func temporaryCoordinates() async throws { + IntrusiveManagedBuffer.withTemporaryValue(header: Coordinates(numCoordinates: 3)) { outputSpan in + outputSpan.append(Point(x: 1, y: 2)) + outputSpan.append(Point(x: 2, y: 3)) + outputSpan.append(Point(x: 3, y: 4)) + } body: { coords in + #expect(coords[0].x == 1) + #expect(coords[1].x == 2) + #expect(coords[2].x == 3) + } + } +}