From 3bf8de6e2b719e65cbc3d04a08703b27623ca648 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Thu, 12 Feb 2026 11:01:30 -0800 Subject: [PATCH] Add new span taking copying initializers to Rigid and UniqueArray --- .../RigidArray/RigidArray+Initializers.swift | 18 +++++++++++++++ .../UniqueArray+Initializers.swift | 17 ++++++++++++++ .../RigidArrayTests.swift | 23 +++++++++++++++++++ .../UniqueArrayTests.swift | 22 ++++++++++++++++++ 4 files changed, 80 insertions(+) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift b/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift index 59b5f8b6..c850ba47 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift @@ -171,6 +171,24 @@ extension RigidArray /*where Element: Copyable*/ { self.init(capacity: capacity ?? contents.count) self.append(copying: contents) } + + /// Creates a new array with the specified capacity, holding a copy + /// of the contents of the given span. + /// + /// - Parameters: + /// - capacity: The storage capacity of the new array, or nil to allocate + /// just enough capacity to store the contents of the span. + /// - span: The span whose contents to copy into the new array. + /// The span must not contain more than `capacity` elements. + @_alwaysEmitIntoClient + @inline(__always) + public init( + capacity: Int? = nil, + copying span: Span + ) { + self.init(capacity: capacity ?? span.count) + self.append(copying: span) + } } // FIXME: Add init(moving:), init(consuming:) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift index fd18c8d1..5706f5e8 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift @@ -139,6 +139,23 @@ extension UniqueArray /*where Element: Copyable*/ { } #endif + /// Creates a new array with the specified capacity, holding a copy + /// of the contents of the given span. + /// + /// - Parameters: + /// - capacity: The storage capacity of the new array, or nil to allocate + /// just enough capacity to store the contents of the span. + /// - span: The span whose contents to copy into the new array. + /// The span must not contain more than `capacity` elements. + @_alwaysEmitIntoClient + @inline(__always) + public init( + capacity: Int? = nil, + copying span: Span + ) { + self.init(capacity: capacity ?? span.count) + self.append(copying: span) + } } #endif diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index bed56314..46a40fbe 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -191,6 +191,29 @@ class RigidArrayTests: CollectionTestCase { #endif #endif + func test_init_copying_Span() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withLifetimeTracking { tracker in + let a = tracker.rigidArray(layout: layout) + let b = RigidArray(capacity: layout.capacity, copying: a.span) + expectEqual(tracker.instances, layout.count) + expectEqual(b.capacity, layout.capacity) + expectEqual(b.count, layout.count) + expectEqual(b.freeCapacity, layout.capacity - layout.count) + expectEqual(b.isEmpty, layout.count == 0) + expectEqual(b.isFull, layout.count == layout.capacity) + for i in 0 ..< layout.count { + expectEqual(b[i].payload, i) + } + expectIterableContents( + b, + equivalentTo: 0 ..< layout.count, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + func test_span() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withLifetimeTracking { tracker in diff --git a/Tests/BasicContainersTests/UniqueArrayTests.swift b/Tests/BasicContainersTests/UniqueArrayTests.swift index 26ca3a1f..65e0752d 100644 --- a/Tests/BasicContainersTests/UniqueArrayTests.swift +++ b/Tests/BasicContainersTests/UniqueArrayTests.swift @@ -227,6 +227,28 @@ class UniqueArrayTests: CollectionTestCase { #endif #endif + func test_init_copying_Span() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withLifetimeTracking { tracker in + let a = tracker.uniqueArray(layout: layout) + let b = UniqueArray(capacity: layout.capacity, copying: a.span) + expectEqual(tracker.instances, layout.count) + expectEqual(b.capacity, layout.capacity) + expectEqual(b.count, layout.count) + expectEqual(b.freeCapacity, layout.capacity - layout.count) + expectEqual(b.isEmpty, layout.count == 0) + for i in 0 ..< layout.count { + expectEqual(b[i].payload, i) + } + expectIterableContents( + b, + equivalentTo: 0 ..< layout.count, + by: { $0.payload == $1 }, + printer: { "\($0.payload)" }) + } + } + } + func test_span() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withLifetimeTracking { tracker in