diff --git a/Sources/TrailingElementsModule/CMakeLists.txt b/Sources/TrailingElementsModule/CMakeLists.txt index 02a40686..2daa5211 100644 --- a/Sources/TrailingElementsModule/CMakeLists.txt +++ b/Sources/TrailingElementsModule/CMakeLists.txt @@ -20,6 +20,6 @@ else() endif() target_sources(${module_name} PRIVATE - "IntrusiveManagedBuffer.swift" + "TrailingArray.swift" "PaddedStorage.swift" "TrailingElements.swift") diff --git a/Sources/TrailingElementsModule/IntrusiveManagedBuffer.swift b/Sources/TrailingElementsModule/TrailingArray.swift similarity index 93% rename from Sources/TrailingElementsModule/IntrusiveManagedBuffer.swift rename to Sources/TrailingElementsModule/TrailingArray.swift index 9aed5313..3e3d3780 100644 --- a/Sources/TrailingElementsModule/IntrusiveManagedBuffer.swift +++ b/Sources/TrailingElementsModule/TrailingArray.swift @@ -7,7 +7,7 @@ /// the information in the header (via the `trailingCount` property), so that /// it is not stored separately. @frozen -public struct IntrusiveManagedBuffer: ~Copyable +public struct TrailingArray: ~Copyable where Header: ~Copyable { /// The underlying storage. @@ -40,7 +40,7 @@ public struct IntrusiveManagedBuffer: ~Copyable var output = unsafe OutputSpan(buffer: rawElements, initializedCount: 0) try initializer(&output) let initialized = unsafe output.finalize(for: rawElements) - precondition(count == initialized, "IntrusiveManagedBuffer initialization underflow") + precondition(count == initialized, "TrailingArray initialization underflow") } /// Allocate an intrusive managed buffer with the given header and calling @@ -196,7 +196,7 @@ public struct IntrusiveManagedBuffer: ~Copyable } } -extension IntrusiveManagedBuffer where Header: ~Copyable, Header.Element: BitwiseCopyable { +extension TrailingArray where Header: ~Copyable, Header.Element: BitwiseCopyable { /// Allocate an intrusive managed buffer with the given header, but leaving the /// trailing elements uninitialized. @_alwaysEmitIntoClient @@ -206,7 +206,7 @@ extension IntrusiveManagedBuffer where Header: ~Copyable, Header.Element: Bitwis } } -extension IntrusiveManagedBuffer where Header: Copyable { +extension TrailingArray 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 @@ -216,14 +216,14 @@ extension IntrusiveManagedBuffer where Header: Copyable { public static func withTemporaryValue( header: consuming Header, repeating element: Element, - body: (inout IntrusiveManagedBuffer
) throws(E) -> R + body: (inout TrailingArray
) 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) + var managedBuffer = TrailingArray(consuming: storage._pointer) managedBuffer.rawElements.initialize(repeating: element) do throws(E) { @@ -254,14 +254,14 @@ extension IntrusiveManagedBuffer where Header: Copyable { public static func withTemporaryValue( header: consuming Header, initializingTrailingElementsWith initializer: (inout OutputSpan) throws(E) -> Void, - body: (inout IntrusiveManagedBuffer
) throws(E) -> R + body: (inout TrailingArray
) 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) + var managedBuffer = TrailingArray(consuming: storage._pointer) try managedBuffer._initializeTrailingElements(initializer: initializer) do throws(E) { @@ -283,7 +283,7 @@ extension IntrusiveManagedBuffer where Header: Copyable { } } -extension IntrusiveManagedBuffer where Header.Element: BitwiseCopyable { +extension TrailingArray 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 @@ -294,14 +294,14 @@ extension IntrusiveManagedBuffer where Header.Element: BitwiseCopyable { public static func withTemporaryValue( header: consuming Header, uninitializedTrailingElements: (), - body: (inout IntrusiveManagedBuffer
) throws(E) -> R + body: (inout TrailingArray
) 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) + var managedBuffer = TrailingArray(consuming: storage._pointer) do throws(E) { let result = try body(&managedBuffer) diff --git a/Sources/TrailingElementsModule/TrailingElements.swift b/Sources/TrailingElementsModule/TrailingElements.swift index 521fbcf8..6f04daa1 100644 --- a/Sources/TrailingElementsModule/TrailingElements.swift +++ b/Sources/TrailingElementsModule/TrailingElements.swift @@ -1,5 +1,5 @@ /// Describes a type that has some number of elements following it directly -/// in memory. Such types are generally used with the `IntrusiveManagedBuffer` +/// in memory. Such types are generally used with the `TrailingArray` /// 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. diff --git a/Sources/TrailingElementsModule/TrailingElementsModule.docc/TrailingElementsModule.md b/Sources/TrailingElementsModule/TrailingElementsModule.docc/TrailingElementsModule.md index 1fbcaded..cc372875 100644 --- a/Sources/TrailingElementsModule/TrailingElementsModule.docc/TrailingElementsModule.md +++ b/Sources/TrailingElementsModule/TrailingElementsModule.docc/TrailingElementsModule.md @@ -7,7 +7,7 @@ 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: +The ``TrailingArray`` 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 { @@ -21,7 +21,7 @@ struct Coordinates { } ``` -To use ``IntrusiveManagedBuffer``, the `Coordinates` type must conform to the +To use ``TrailingArray``, 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 @@ -34,24 +34,24 @@ extension Coordinates: TrailingElements { 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 +var coords = TrailingArray(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. +Subscripting an ``TrailingArray`` provides access to the elements. One can also use the ``TrailingArray.elements`` or ``TrailingArray.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: +If heap allocation is not acceptable, use the static method ``TrailingArray.withTemporaryValue`` to produce a temporary instance of ``TrailingArray`` 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 +TrailingArray.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) @@ -61,7 +61,7 @@ IntrusiveManagedBuffer.withTemporaryValue(header: Coordinates(numPoints: 3), rep ``` ### 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:)`: +``TrailingArray`` provides facilities for working with the underlying unsafe pointers, which can be useful when interoperating with C APIs. ``TrailingArray`` 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
) @@ -85,7 +85,7 @@ struct coordinates_t { }; ``` -The C `coordinates_t` type can extended to conform to the ``TrailingElements`` protocol, allowing it to be used directly with ``IntrusiveManagedBuffer``: +The C `coordinates_t` type can extended to conform to the ``TrailingElements`` protocol, allowing it to be used directly with ``TrailingArray``: ```swift extension coordinates_t: TrailingElements { @@ -98,7 +98,7 @@ This provides safer access patterns for C flexible array members, introducing sa ### ``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: +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 ``TrailingArray``, 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()) @@ -108,7 +108,7 @@ var padded = PaddedStorage(header: SomeType(), totalSize: getSizeOfSomeTypeWithP ### Structures -- ``IntrusiveManagedBuffer`` +- ``TrailingArray`` - ``PaddedStorage`` ### Protocols diff --git a/Tests/TrailingElementsTests/TrailingElementsTests.swift b/Tests/TrailingElementsTests/TrailingElementsTests.swift index 67a4b3cb..5a09f0ee 100644 --- a/Tests/TrailingElementsTests/TrailingElementsTests.swift +++ b/Tests/TrailingElementsTests/TrailingElementsTests.swift @@ -18,7 +18,7 @@ extension Coordinates: TrailingElements { @Suite("Trailing elements tests") struct TrailingElementsTests { @Test func simpleCoordinates() async throws { - var coords = IntrusiveManagedBuffer(header: Coordinates(numCoordinates: 3)) { outputSpan in + var coords = TrailingArray(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)) @@ -33,7 +33,7 @@ struct TrailingElementsTests { } @Test func repeatingInit() async throws { - var coords = IntrusiveManagedBuffer(header: Coordinates(numCoordinates: 3), repeating: Point(x: 1, y: 1)) + var coords = TrailingArray(header: Coordinates(numCoordinates: 3), repeating: Point(x: 1, y: 1)) #expect(coords[0].x == 1) #expect(coords[1].x == 1) @@ -46,7 +46,7 @@ struct TrailingElementsTests { } @Test func temporaryCoordinates() async throws { - IntrusiveManagedBuffer.withTemporaryValue(header: Coordinates(numCoordinates: 3)) { outputSpan in + TrailingArray.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))