From 8e4f07844183660aaca4add9e4936dc6e21d77b6 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 21 Sep 2026 13:02:13 -0700 Subject: [PATCH 1/2] Producer.generate(into:): Switch to saturating semantics This noticeable simplifies client code, as clients invariably want to fully populate their buffer (when possible). It also is consistent with the semantics of `Iterable.skip(by:)`. --- .../Conformances/UniqueDeque+Container.swift | 8 +-- .../Conformances/UniqueSet+Container.swift | 5 +- .../Extensions/RigidSet+Extras.swift | 4 +- .../Container/DynamicContainer.swift | 4 +- .../MutableContainer+UpdateSubrange.swift | 8 +-- .../Container/RangeReplaceableContainer.swift | 10 ++-- .../Protocols/Drain/Drain+Map.swift | 8 +-- .../Protocols/Drain/Drain.swift | 16 +++--- .../BorrowingIteratorProtocol+Map.swift | 8 +-- .../Protocols/Producer/Producer+Filter.swift | 7 +-- .../Protocols/Producer/Producer+Map.swift | 17 +++--- .../Protocols/Producer/Producer+Reduce.swift | 6 ++- .../Protocols/Producer/Producer+Unfold.swift | 4 +- .../Protocols/Producer/Producer.swift | 53 ++++++++----------- .../BasicContainersTests/RigidSetTests.swift | 41 +++++++------- .../MutableContainer_Update_Tests.swift | 41 +++++++------- .../MinimalTypes/CustomCountedProducer.swift | 8 +-- .../MinimalTypes/CustomProducer.swift | 15 +++--- .../MinimalTypes/PureProducer.swift | 2 +- 19 files changed, 121 insertions(+), 144 deletions(-) diff --git a/Sources/ContainersPreview/Conformances/UniqueDeque+Container.swift b/Sources/ContainersPreview/Conformances/UniqueDeque+Container.swift index fc7f9aa1..f33a3ed7 100644 --- a/Sources/ContainersPreview/Conformances/UniqueDeque+Container.swift +++ b/Sources/ContainersPreview/Conformances/UniqueDeque+Container.swift @@ -94,9 +94,7 @@ extension UniqueDeque where Element: ~Copyable { return try _storage._handle.uncheckedPrepend( addingCount: newItemCount ) { target throws(E) in - while !target.isFull, try producer.generate(into: &target) { - // Do nothing - } + try producer.generate(into: &target) } } @@ -136,9 +134,7 @@ extension UniqueDeque where Element: ~Copyable { precondition(d >= 0, "Invalid Producer") added &+= d } - while !target.isFull, !done { - done = try !producer.generate(into: &target) - } + done = try producer.generate(into: &target) == 0 } } return Range(uncheckedBounds: (0, added)) diff --git a/Sources/ContainersPreview/Conformances/UniqueSet+Container.swift b/Sources/ContainersPreview/Conformances/UniqueSet+Container.swift index 56d5bdf0..599612d3 100644 --- a/Sources/ContainersPreview/Conformances/UniqueSet+Container.swift +++ b/Sources/ContainersPreview/Conformances/UniqueSet+Container.swift @@ -80,9 +80,8 @@ extension UniqueSet where Element: ~Copyable { while !done { _ensureFreeCapacity(Swift.max(producer.underestimatedCount, 1)) try self.insert(addingCount: self.freeCapacity) { target throws(E) in - while !target.isFull, !done { - done = try !producer.generate(into: &target) - } + try producer.generate(into: &target) + done = done || !target.isFull } } } diff --git a/Sources/ContainersPreview/Extensions/RigidSet+Extras.swift b/Sources/ContainersPreview/Extensions/RigidSet+Extras.swift index 073b11c8..96407d77 100644 --- a/Sources/ContainersPreview/Extensions/RigidSet+Extras.swift +++ b/Sources/ContainersPreview/Extensions/RigidSet+Extras.swift @@ -104,9 +104,7 @@ extension RigidSet where Element: ~Copyable { try self.insert( addingCount: newItemCount ?? freeCapacity ) { target throws(E) in - while !target.isFull { - guard try producer.generate(into: &target) else { break } - } + try producer.generate(into: &target) } } diff --git a/Sources/ContainersPreview/Protocols/Container/DynamicContainer.swift b/Sources/ContainersPreview/Protocols/Container/DynamicContainer.swift index b0add86a..8f9718ad 100644 --- a/Sources/ContainersPreview/Protocols/Container/DynamicContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/DynamicContainer.swift @@ -94,9 +94,7 @@ extension DynamicContainer where Self: ~Copyable, Element: ~Copyable { let c = Swift.max(producer.underestimatedCount, self.freeCapacity) if c > 0 { try self.append(addingCount: c) { target throws(E) in - while !target.isFull { - guard try producer.generate(into: &target) else { return } - } + try producer.generate(into: &target) } } // Nudge the container to resize itself, hopefully providing more diff --git a/Sources/ContainersPreview/Protocols/Container/MutableContainer+UpdateSubrange.swift b/Sources/ContainersPreview/Protocols/Container/MutableContainer+UpdateSubrange.swift index 9fdcba09..03f411c7 100644 --- a/Sources/ContainersPreview/Protocols/Container/MutableContainer+UpdateSubrange.swift +++ b/Sources/ContainersPreview/Protocols/Container/MutableContainer+UpdateSubrange.swift @@ -80,8 +80,8 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable } let r = try scratch._append( addingCount: Swift.min(dst.count &- offset, scratch.freeCapacity) - ) { src throws(Failure) in try source.fill(&src) } - guard r else { return } + ) { src throws(Failure) in try source.generate(into: &src) } + guard r > 0 else { return } } index = next offset = 0 @@ -105,7 +105,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable remaining -= target.count target._edit { dst in dst.removeAll() - source.fill(&dst) + source.generate(into: &dst) } } } @@ -134,7 +134,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable // `updateElements(after:from:)` to support the throwing case, but at // the cost of slower operation and a far more unwieldy interface.) dst.removeAll() - source.fill(&dst) + source.generate(into: &dst) precondition(dst.isFull, "Invalid CountedProducer") } } diff --git a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift index 109b68c5..cb87e8c4 100644 --- a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift @@ -176,8 +176,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable consumingWith: { _ in }, addingCount: newItemCount, initializingWith: { target throws(E) in - while !target.isFull, try producer.generate(into: &target) { - } + try producer.generate(into: &target) }) } @@ -350,9 +349,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable where P.Element: ~Copyable { try self.insert(addingCount: newItemCount, at: index) { target throws(E) in - while !target.isFull, try producer.generate(into: &target) { - // Do nothing - } + try producer.generate(into: &target) } } @@ -390,8 +387,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { let c = producer.count let range = try self.insert(addingCount: c, at: index) { target throws(E) in - while !target.isFull, try producer.generate(into: &target) { - } + try producer.generate(into: &target) } try producer._expectEnd("Invalid Container") return range diff --git a/Sources/ContainersPreview/Protocols/Drain/Drain+Map.swift b/Sources/ContainersPreview/Protocols/Drain/Drain+Map.swift index 666a44a6..df29cdc6 100644 --- a/Sources/ContainersPreview/Protocols/Drain/Drain+Map.swift +++ b/Sources/ContainersPreview/Protocols/Drain/Drain+Map.swift @@ -74,17 +74,17 @@ where @_lifetime(self: copy self) public mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { - var success = false + ) throws(Failure) -> Int { + var c = 0 while !target.isFull { var source = _base.drainNext(maxCount: target.freeCapacity) if source.isEmpty { break } - success = true + c += source.count while !source.isEmpty { try target.append(_transform(source.removeFirst())) } } - return success + return c } @inlinable diff --git a/Sources/ContainersPreview/Protocols/Drain/Drain.swift b/Sources/ContainersPreview/Protocols/Drain/Drain.swift index d25d6e9a..12d803e6 100644 --- a/Sources/ContainersPreview/Protocols/Drain/Drain.swift +++ b/Sources/ContainersPreview/Protocols/Drain/Drain.swift @@ -168,13 +168,17 @@ extension Drain where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_lifetime(target: copy target) public mutating func generate( into target: inout OutputSpan - ) throws(Never) -> Bool { - var source = self.drainNext(maxCount: target.freeCapacity) - if source.isEmpty { return false } - target._append(moving: &source) - return true + ) throws(Never) -> Int { + var c = 0 + while !target.isFull { + var source = self.drainNext(maxCount: target.freeCapacity) + guard !source.isEmpty else { break } + c += source.count + target._append(moving: &source) + } + return c } - + /// Skip the given number items in the underlying generative sequence, /// decreasing it by the number of items successfully skipped before hitting /// the end of the sequence or an error. diff --git a/Sources/ContainersPreview/Protocols/Iterable/BorrowingIteratorProtocol+Map.swift b/Sources/ContainersPreview/Protocols/Iterable/BorrowingIteratorProtocol+Map.swift index 425c3fc1..cfe7df3f 100644 --- a/Sources/ContainersPreview/Protocols/Iterable/BorrowingIteratorProtocol+Map.swift +++ b/Sources/ContainersPreview/Protocols/Iterable/BorrowingIteratorProtocol+Map.swift @@ -101,19 +101,19 @@ where @_lifetime(self: copy self) public mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { - var success = false + ) throws(Failure) -> Int { + var c = 0 while !target.isFull { let span = try _it.nextSpan(maxCount: target.freeCapacity) guard !span.isEmpty else { break } - success = true + c += span.count var i = 0 while i < span.count { target.append(try _transform(span[unchecked: i])) i &+= 1 } } - return success + return c } @inlinable diff --git a/Sources/ContainersPreview/Protocols/Producer/Producer+Filter.swift b/Sources/ContainersPreview/Protocols/Producer/Producer+Filter.swift index e801694b..5af9af01 100644 --- a/Sources/ContainersPreview/Protocols/Producer/Producer+Filter.swift +++ b/Sources/ContainersPreview/Protocols/Producer/Producer+Filter.swift @@ -90,16 +90,17 @@ where @_lifetime(self: copy self) public mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { + ) throws(Failure) -> Int { let startCount = target.count repeat { let prevCount = target.count defer { target._remove(from: prevCount, where: { !_isIncluded($0) }) } - guard try _base.generate(into: &target) else { break } + try _base.generate(into: &target) + guard target.isFull else { break } } while target.count == startCount - return target.count > startCount + return target.count - startCount } // Note: We can't implement skip(by:) a better way than the default algorithm. diff --git a/Sources/ContainersPreview/Protocols/Producer/Producer+Map.swift b/Sources/ContainersPreview/Protocols/Producer/Producer+Map.swift index 7d120eff..5aafbdaf 100644 --- a/Sources/ContainersPreview/Protocols/Producer/Producer+Map.swift +++ b/Sources/ContainersPreview/Protocols/Producer/Producer+Map.swift @@ -13,6 +13,7 @@ #if !COLLECTIONS_SINGLE_MODULE import InternalCollectionsUtilities +import SpanPreview #endif #if compiler(>=6.4) && UnstableContainersPreview @@ -87,16 +88,16 @@ extension ConsumingMapProducer: Producer where Base: ~Copyable & ~Escapable { @_lifetime(self: copy self) public mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { + ) throws(Failure) -> Int { let c = Swift.min(target.freeCapacity, _producerBufferSize) - return try _withUnsafeTemporaryAllocation( + return try withTemporaryAllocation( of: Base.Element.self, capacity: c - ) { buffer throws(Failure) in - var outputSpan = OutputSpan(buffer: buffer, initializedCount: 0) - let result = try _base.generate(into: &outputSpan) - let c = outputSpan.finalize(for: buffer) - for i in 0 ..< c { - target.append(try _transform(buffer.moveElement(from: i))) + ) { source throws(Failure) in + let result = try _base.generate(into: &source) + try source._consumeAll { src throws(Failure) in + while !src.isEmpty { + target.append(try _transform(src.removeFirst())) + } } return result } diff --git a/Sources/ContainersPreview/Protocols/Producer/Producer+Reduce.swift b/Sources/ContainersPreview/Protocols/Producer/Producer+Reduce.swift index d9e22a22..9df7af0f 100644 --- a/Sources/ContainersPreview/Protocols/Producer/Producer+Reduce.swift +++ b/Sources/ContainersPreview/Protocols/Producer/Producer+Reduce.swift @@ -34,7 +34,8 @@ extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { var done = false var result = initialResult.take() repeat { - done = try !self.generate(into: &buffer) + try self.generate(into: &buffer) + done = done || !buffer.isFull try buffer._consumeAll { span throws(Failure) in while let next = span.popFirst() { result = try nextPartialResult(result.take()!, next) @@ -59,7 +60,8 @@ extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { var result = initialResult.take()! var done = false repeat { - done = try !self.generate(into: &buffer) + try self.generate(into: &buffer) + done = done || !buffer.isFull try buffer._consumeAll { span throws(Failure) in while let next = span.popFirst() { try updateAccumulatingResult(&result, next) diff --git a/Sources/ContainersPreview/Protocols/Producer/Producer+Unfold.swift b/Sources/ContainersPreview/Protocols/Producer/Producer+Unfold.swift index d4654537..daf55387 100644 --- a/Sources/ContainersPreview/Protocols/Producer/Producer+Unfold.swift +++ b/Sources/ContainersPreview/Protocols/Producer/Producer+Unfold.swift @@ -66,14 +66,14 @@ where @_lifetime(self: copy self) public mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { + ) throws(Failure) -> Int { var c = 0 while !target.isFull { guard let next = try _next(&self._state) else { break } target.append(next) c += 1 } - return c > 0 + return c } @inlinable diff --git a/Sources/ContainersPreview/Protocols/Producer/Producer.swift b/Sources/ContainersPreview/Protocols/Producer/Producer.swift index eed0f57a..77604044 100644 --- a/Sources/ContainersPreview/Protocols/Producer/Producer.swift +++ b/Sources/ContainersPreview/Protocols/Producer/Producer.swift @@ -68,24 +68,24 @@ public protocol Producer: ~Copyable, ~Escapable { /// /// Repeatedly calling this method produces, in order, all the elements of the /// underlying generative sequence. As soon as the sequence has run out of - /// elements, all subsequent calls return false without appending any new - /// items to their target. This method is not guaranteed to fully populate the - /// given output span, but it always appends at least one item until the end - /// of the underlying generative sequence. + /// elements, all subsequent calls return zero without appending any new + /// items to their target. This method is guaranteed to fully populate the + /// given output span unless it reaches the end of the producer, or it + /// encounters an error. /// /// The ownership of all generated elements is transferred to the caller of /// this method -- it can arbitrarily store, mutate, consume or discard them /// as needed, even across invocations of this method, or after the producer /// is destroyed. /// - /// The returned Boolean value can be used to easily determine if the - /// method was able to make progress towards filling `target` without hitting - /// the end of the underlying sequence. + /// The method returns the number of items it successfully appended to the + /// span, making it easy to determine/measure forward progress. /// - /// If `target` is a full span, this method is allowed to unconditionally - /// return true. Passing an empty span is not a reliable way to test if the - /// producer has reached its end. (Some producers may only be able to detect - /// that they are finished while trying to generate the next item.) + /// Passing a `target` with no free capacity is a precondition violation. + /// The operation may result in a runtime error in this case, but it is also + /// allowed to unconditionally return zero in this case, whether or not the + /// producer is at its end. There is no way to test if the producer has + /// reached its end without asking it to generate at least one new item. /// /// ### Error handling /// @@ -111,15 +111,15 @@ public protocol Producer: ~Copyable, ~Escapable { /// the algorithm is solving. /// /// - Parameter target: An output span ready to take newly generated items. - /// - Returns: A boolean value indicating whether the operation was able to - /// append at least one item to the supplied output span without hitting - /// the end of the underlying sequence. + /// On return, `target.isFull` is true unless the producer has reached its + /// end. + /// - Returns: The number of elements that the operation appended to `target`. @discardableResult @_lifetime(target: copy target) @_lifetime(self: copy self) mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool + ) throws(Failure) -> Int /// Skip the given number items in the underlying generative sequence, /// decreasing it by the number of items successfully skipped before hitting @@ -240,9 +240,12 @@ extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { capacity: Swift.min(maxBufferSize, n) ) { buffer throws(Failure) in repeat { - defer { n &-= buffer.count } - guard try self.generate(into: &buffer) else { return } - buffer.removeAll() + defer { + n &-= buffer.count + buffer.removeAll() + } + try self.generate(into: &buffer) + if !buffer.isFull { return } } while n > 0 } } @@ -320,7 +323,7 @@ extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { try withTemporaryAllocation( of: Element.self, capacity: 1 ) { buffer throws(Failure) in - guard try self.generate(into: &buffer) else { return nil } + guard try self.generate(into: &buffer) == 1 else { return nil } return buffer.removeLast() } } @@ -328,18 +331,6 @@ extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { - @_alwaysEmitIntoClient - @discardableResult - public mutating func fill( - _ target: inout OutputSpan - ) throws(Failure) -> Bool { - var result = false - while !target.isFull, try self.generate(into: &target) { - result = true - } - return result - } - /// Triggers a runtime trap if the producer is not at its end, consuming it in /// the process. This is implemented by checking if it is possible to /// skip one item. diff --git a/Tests/BasicContainersTests/RigidSetTests.swift b/Tests/BasicContainersTests/RigidSetTests.swift index 35746338..5f3def8c 100644 --- a/Tests/BasicContainersTests/RigidSetTests.swift +++ b/Tests/BasicContainersTests/RigidSetTests.swift @@ -64,7 +64,7 @@ class RigidSetTests: CollectionTestCase { expectEqual(MemoryLayout>.stride, 6 * word) expectEqual(MemoryLayout?>.stride, 6 * word) } - + func test_empty() { let s = RigidSet() expectEqual(s.count, 0) @@ -398,30 +398,27 @@ class RigidSetTests: CollectionTestCase { func test_insert_producer() { withEvery("capacity", in: [0, 1, 2, 4, 10, 100, 200]) { capacity in withEvery("count", in: 0 ..< capacity) { count in - withEvery("chunkSize", in: [1, 2, 10, 100, Int.max]) { chunkSize in - withLifetimeTracking { tracker in - var s = RigidSet>(capacity: capacity) + withLifetimeTracking { tracker in + var s = RigidSet>(capacity: capacity) - var p = CustomProducer, Never>( - underestimatedCount: 0, - chunkSize: chunkSize - ) { offset in - guard offset < count else { return nil } - return tracker.instance(for: offset) - } - s.insert(from: &p) - expectConsistentSet(s) + var p = CustomProducer, Never>( + underestimatedCount: 0 + ) { offset in + guard offset < count else { return nil } + return tracker.instance(for: offset) + } + s.insert(from: &p) + expectConsistentSet(s) - expectEqual(s.capacity, capacity) - expectEqual(s.count, count) + expectEqual(s.capacity, capacity) + expectEqual(s.count, count) - var seen: Set = [] - var index = s.startIndex - while index != s.endIndex { - let payload = s[index].payload - expectTrue(seen.insert(payload).inserted, "Duplicate item \(payload)") - index = s.index(after: index) - } + var seen: Set = [] + var index = s.startIndex + while index != s.endIndex { + let payload = s[index].payload + expectTrue(seen.insert(payload).inserted, "Duplicate item \(payload)") + index = s.index(after: index) } } } diff --git a/Tests/ContainersTests/MutableContainer_Update_Tests.swift b/Tests/ContainersTests/MutableContainer_Update_Tests.swift index e244c834..03fa6762 100644 --- a/Tests/ContainersTests/MutableContainer_Update_Tests.swift +++ b/Tests/ContainersTests/MutableContainer_Update_Tests.swift @@ -154,30 +154,27 @@ final class MutableContainer_Update_Tests: CollectionTestCase { withEveryFullDeque("layout", ofCapacities: [0, 1, 10]) { layout in withEvery("start", in: 0 ..< layout.count) { start in withEvery("length", in: 0 ..< layout.count - start + 2) { length in - withEvery("chunkSize", in: [1, 2, length] as Set) { chunkSize in - withLifetimeTracking { tracker in - var expected = Array(0 ..< layout.count) - let replacements1 = layout.count ..< (layout.count + length) - let end = Swift.min(start + length, layout.count) - expected.replaceSubrange( - start ..< end, - with: replacements1.prefix(end - start)) + withLifetimeTracking { tracker in + var expected = Array(0 ..< layout.count) + let replacements1 = layout.count ..< (layout.count + length) + let end = Swift.min(start + length, layout.count) + expected.replaceSubrange( + start ..< end, + with: replacements1.prefix(end - start)) - var actual = tracker.rigidDeque(with: layout) - var replacements2 = CustomProducer, Never>( - underestimatedCount: 0, - chunkSize: chunkSize - ) { offset in - guard offset < end - start else { return nil } - return tracker.instance(for: layout.count + offset) - } - - var i = start - actual.deque.updateElements(after: &i, from: &replacements2) - - expectIterablePayloads(actual.deque, equalTo: expected) - expectEqual(i, end) + var actual = tracker.rigidDeque(with: layout) + var replacements2 = CustomProducer, Never>( + underestimatedCount: 0, + ) { offset in + guard offset < end - start else { return nil } + return tracker.instance(for: layout.count + offset) } + + var i = start + actual.deque.updateElements(after: &i, from: &replacements2) + + expectIterablePayloads(actual.deque, equalTo: expected) + expectEqual(i, end) } } } diff --git a/Tests/_CollectionsTestSupport/MinimalTypes/CustomCountedProducer.swift b/Tests/_CollectionsTestSupport/MinimalTypes/CustomCountedProducer.swift index 2e7522e2..66678269 100644 --- a/Tests/_CollectionsTestSupport/MinimalTypes/CustomCountedProducer.swift +++ b/Tests/_CollectionsTestSupport/MinimalTypes/CustomCountedProducer.swift @@ -51,14 +51,14 @@ package struct CustomCountedProducer: ~Copya extension CustomCountedProducer: CountedProducer where Element: ~Copyable { package mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { - var success = false + ) throws(Failure) -> Int { + var c = 0 while !target.isFull, _offset < count { target.append(try _generator(_offset)) _offset += 1 - success = true + c += 1 } - return success + return c } } diff --git a/Tests/_CollectionsTestSupport/MinimalTypes/CustomProducer.swift b/Tests/_CollectionsTestSupport/MinimalTypes/CustomProducer.swift index 83de120f..79633a77 100644 --- a/Tests/_CollectionsTestSupport/MinimalTypes/CustomProducer.swift +++ b/Tests/_CollectionsTestSupport/MinimalTypes/CustomProducer.swift @@ -23,17 +23,14 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) package struct CustomProducer: ~Copyable { package let underestimatedCount: Int - package let _chunkSize: Int package let _generator: (Int) throws(Failure) -> Element? package var _offset: Int package init( underestimatedCount: Int = 0, - chunkSize: Int = Int.max, generatingWith generator: borrowing @escaping (Int) throws(Failure) -> Element? ) { self.underestimatedCount = underestimatedCount - self._chunkSize = chunkSize self._generator = copy generator self._offset = 0 } @@ -45,15 +42,15 @@ package struct CustomProducer: ~Copyable { extension CustomProducer: Producer where Element: ~Copyable { package mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { - var i = 0 - while !target.isFull, i < _chunkSize { - guard let next = try _generator(_offset) else { return i > 0 } + ) throws(Failure) -> Int { + var c = 0 + while !target.isFull { + guard let next = try _generator(_offset) else { return c } target.append(next) _offset += 1 - i += 1 + c += 1 } - return i > 0 + return c } } diff --git a/Tests/_CollectionsTestSupport/MinimalTypes/PureProducer.swift b/Tests/_CollectionsTestSupport/MinimalTypes/PureProducer.swift index 2f03e901..f2f29407 100644 --- a/Tests/_CollectionsTestSupport/MinimalTypes/PureProducer.swift +++ b/Tests/_CollectionsTestSupport/MinimalTypes/PureProducer.swift @@ -69,7 +69,7 @@ where Wrapped: ~Copyable & ~Escapable, Wrapped.Element: ~Copyable { @_lifetime(target: copy target) package mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool { + ) throws(Failure) -> Int { try _base.generate(into: &target) } From 5d0faff6c79092531c5bf37144ba94fef19a60fc Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 21 Sep 2026 13:02:31 -0700 Subject: [PATCH 2/2] Update container design document --- Documentation/Container-design.md | 317 ++++++++++++------------------ 1 file changed, 128 insertions(+), 189 deletions(-) diff --git a/Documentation/Container-design.md b/Documentation/Container-design.md index 76345d45..a464a250 100644 --- a/Documentation/Container-design.md +++ b/Documentation/Container-design.md @@ -1,12 +1,6 @@ # An Ownership-Aware Container Model for Swift * Author: [Karoy Lorentey](https://github.com/lorentey) -* Version history: - - 0.1 (2026-07-21): Initial draft, describing `Container`. - - 0.2 (2026-07-22): Second draft, completing the chapter on the read-only container model. - - 0.3 (2026-07-28): `PermutableContainer` and `MutableContainer` - - 0.4 (2026-08-06): `Producer` - - 0.5 (2026-08-18): Add `Overview of Container Protocols` section. Spin off `DrainableContainer` as a separate protocol. ## Table of Contents @@ -41,6 +35,7 @@ * [Iterating Over a Producer](#iterating-over-a-producer) * [Creating a Producer](#creating-a-producer) * [Default Implementations on Producer](#default-implementations-on-producer) + * [Counted Producers](#counted-producers) * [In\-Place Consumption](#in-place-consumption) * [struct InputSpan](#struct-inputspan) * [protocol Drain](#protocol-drain) @@ -53,7 +48,11 @@ * [Adding Elements to a Container](#adding-elements-to-a-container) * [Replacing Subranges](#replacing-subranges) * [Dynamic Containers](#dynamic-containers) + * [Sample Conformances](#sample-conformances) + * [UniqueDeque](#uniquedeque) + * [LinkedList](#linkedlist) * [Rejected Directions](#rejected-directions) + * [Non\-Comparable Indices](#non-comparable-indices) * [Cursors](#cursors) * [Index Rounding Operations](#index-rounding-operations) * [protocol ContiguousContainer](#protocol-contiguouscontainer) @@ -396,7 +395,7 @@ As `Container` refines `Iterable`, it provides a `BorrowingIterator`, and we can ```swift protocol Container: ... { ... - associatedtype Index: Equatable, Hashable + associatedtype Index: Equatable, Comparable, Hashable ... } ``` @@ -418,11 +417,9 @@ Over the years, Swift's indices have proven to be extremely successful abstracti Every one of the standard container types we've introduced so far (from [`Span`][Span] to [`UniqueArray`][UniqueArray] and beyond) already defines an index that fits this model. -`Container` indices are required to be `Equatable`, but unlike `Collection`, they do not need to be `Comparable`. Linked lists are commonly used in systems programming, and this choice allows some linked list implementations to usefully conform to `Container`. The lack of an inherent ordering does complicate the default implementations of some index operations -- [see below](#distance-from-to). +`Container` indices are required to be `Equatable` and `Comparable`, like `Collection` indices. In addition, `Container` also requires its indices to be `Hashable`. This does not put an undue burden on container implementations, but it does allow indices to be collected in sets or used as dictionary keys, enabling use cases such as dynamic filtering or out-of-band storage of data associated with container elements. (Not guaranteeing hashable indices is a long-standing annoyance with `Collection`.) -In exchange, `Container` requires its indices to be `Hashable`. This does not put an undue burden on container implementations, but it does allow indices to be collected in sets or used as dictionary keys, enabling use cases such as dynamic filtering or out-of-band storage of data associated with container elements. (Not guaranteeing hashable indices is a long-standing annoyance with `Collection`.) - -To acknowledge our performance goals, **we require that equating and hashing `Container` indices must be performed with O(1) complexity**. If a container's index does happen to be `Comparable`, then it is required that `<` has O(1) complexity as well. +To acknowledge our performance goals, **we require that equating, comparing and hashing `Container` indices must all be performed with O(1) complexity**. Now that we have an `Index` type, we can define all the familiar operations that allow us to navigate within a container: @@ -446,19 +443,15 @@ protocol Container: ... { As `Container` models a forward-only container, its `index(_:offsetBy:)` operation requires a nonnegative offset. (We'll shortly introduce a `BidirectionalContainer` refinement that will relax this constraint.) - +However, as indices are required to be `Comparable`, the `distance(from:to:)` method works whether or not its `start` argument precedes `end`, even if we can only iterate forward. -As indices aren't required to be `Comparable`, the default implementation of `distance(from:to:)` cannot quickly validate that its `start` argument precedes `end`. To avoid having to run all the way to the end of the container to detect misuse, the algorithm instead iterates forward from both arguments, until it finds the other. This makes the default implementation twice as slow, but it allows it to correctly calculate negative distances. In the common case when `Index` is `Comparable`, we also provide a refined default algorithm that avoids this overhead. - - - -Notably, we replace `Collection`'s classic `index(_:offsetBy:limitedBy:)` requirement with an improved variant ([first introduced on `UniqueArray`][formIndex-offsetBy-limitedBy]) that reports the number of steps it was unable to take when reaching the limit. This avoids having to figure this out with a separate `distance(from:to:)` invocation, enabling more efficient use of this operation in situations that need this data; for example, it allows easy single-pass offsetting across concatenated containers. `Collection`'s original `index(_:offsetBy:limitedBy:)` operation is still available, as a standard algorithm based on the new requirement. +Notably, we replace `Collection`'s classic `index(_:offsetBy:limitedBy:)` requirement with an improved variant ([first introduced on `RigidArray`/`UniqueArray`][formIndex-offsetBy-limitedBy]) that reports the number of steps it was unable to take when reaching the limit. This avoids having to figure this out with a separate `distance(from:to:)` invocation, enabling more efficient use of this operation in situations that need this data; for example, it allows easy single-pass offsetting across concatenated containers. `Collection`'s original `index(_:offsetBy:limitedBy:)` operation is still available, as a standard algorithm based on the new requirement. The `limit` argument here (and also elsewhere throughout the `Container` interface surface) means a **limiting index**, intended to cause the operation to stop if it encounters the limit during its execution. A limit of this sort only triggers a stop if the operation needs to actively iterate over it. A limit that the operation never needs to visit has no effect, whether it happens to address a position before or after the visited range. For example, if we're trying to find the index by offsetting position 10 by 5 places forward, a limiting index at position 3 has no effect -- to offset the original position, we only need to visit positions 10, 11, 12, 13, 14, and 15. -These curious semantics allow types that cannot provide comparable indices to still correctly conform to the protocol. (For example, linked lists usually cannot determine relative ordering between their indices without actively iterating through the list in linear time. If `limit` was required to be observed even if it lied behind the starting position, then linked lists would need to painstakingly figure out if this was the case, by iterating through items we wouldn't expect the operation to ever visit.) +These curious semantics come from `Collection`, and they are a legacy of an early collection design where indices were not required to be comparable. We adopt them for `Container` for two reasons: first, it is preferable to avoid diverging from `Collection` behavior where there isn't a strong reason to do otherwise; second, while `Container.Index` is required to be comparable in this version of this document, we are not yet fully committed to keep it. @@ -573,90 +566,6 @@ Containers often group their contents into piecewise contiguous storage chunks: While bulk iteration is often a performance boost, not all container types benefit from it. Some containers allocate a separate node for each of their elements, so their storage chunks all have just one item each. (Linked lists and binary search trees are two well-known examples.) These container types are still able to implement bulk iteration interfaces, but they become far less effective, as they are reduced to a somewhat overcomplicated form of elementwise iteration. Accordingly, in the worst case the `index(_:offsetBy:)` implementation above may need to invoke the `nextSpan` operation `n` times -- it has the same linear worst-case complexity as `Collection`'s elementwise implementation. -The default implementation of `distance(from:to:)` poses an interesting problem: `Container` only allows forward iteration, so to measure the distance between two indices, we have to start iterating from the one addressing the earlier one. But `Container` does not require its `Index` to be `Comparable`, so we cannot easily decide which index goes first! We have three options to resolve this: - -1. Only provide a default `distance` algorithm for containers with comparable indices. - - ```swift - extension Container - where - Self: ~Copyable & ~Escapable, - Element: ~Copyable, - Index: Comparable - { - func distance(from start: Index, to end: Index) -> Int { - var (i, j, forward): (Index, Index, Bool) = (start <= end - ? (start, end, true) - : (end, start, false)) - var d = 0 - while i < j { - let span = self.nextSpan(after: &i, maxCount: .max, limitedBy: j) - precondition(span.count > 0, "Invalid Container") - d += span.count - } - return forward ? d : -d - } - } - ``` - - This visits exactly as many storage chunks as exist between the indices, so it is the most efficient way to calculate the distance. (Without knowing more about the internals of the container.) - -2. Require `start` to precede `end`, but with no easy way to validate this other than letting iteration go all the way to the end of the container. - - ```swift - extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable { - func distance(from start: Index, to end: Index) -> Int { - var i = start - var d = 0 - while true { - let c = self.nextSpan(after: &i, maxCount: .max, limitedBy: end).count - d += c - if i == end { break } - precondition(c > 0, "Invalid Container or 'start' does not precede 'end'") - } - return d - } - } - ``` - - This option is equivalent to the previous one if `start` happens to come before `end`, but it runs all the way to the end of the container and then traps if they are in the wrong order. `Collection`'s original `distance` had no trouble returning negative values, so choosing this option could invite accidental misuse. - -3. Allow `start` and `end` to be in any order, but iterate forward from both ends until we find the other: - - ```swift - extension Container where Self: ~Copyable & ~Escapable, Element: ~Copyable { - func distance(from start: Index, to end: Index) -> Int { - // This variant allows start to follow end, but as indices aren't - // comparable, we have to measure distances from both ends. - var d1 = 0 - var d2 = 0 - var i1 = start - var i2 = end - var forward = true - var backward = true - while forward || backward { - if forward { - let c = self.nextSpan(after: &i1, limitedBy: end).count - d1 += c - if i1 == end { return d1 } - if c == 0 { forward = false } - } - if backward { - let c = self.nextSpan(after: &i2, limitedBy: start).count - d2 -= c - if i2 == start { return d2 } - if c == 0 { backward = false } - } - } - fatalError("Invalid Container") - } - } - ``` - - This option has to visit (at worst) twice as many items as the previous two in their regular execution, as the `limitedBy:` arguments [have no effect if the specified `limit` lies behind the start position](#limiting-index-semantics). - -To preserve continuity/interoperability with `Collection` (whose `distance` operation allows any ordering), `Container` implements option 3, while also providing the simpler/faster implementation from option 1, for the common case when `Index` is `Comparable`. - ### Subscripting Bulk iteration is nice, but sometimes we do just want to access an individual element addressed by a specific index. For these cases, `Container` provides an indexing subscript with a [`borrow` accessor][BorrowAndMutate]: @@ -918,7 +827,7 @@ Our `Container` model follows in the footsteps of `Collection`, so we model back ```swift protocol BidirectionalContainer: Container, ~Copyable, ~Escapable -where Element: ~Copyable, Index: Comparable +where Element: ~Copyable { func index(before i: Index) -> Index func formIndex(before i: inout Index) @@ -1046,7 +955,7 @@ Protocol `RandomAccessContainer` refines `BidirectionalContainer`, with minimal ``` protocol RandomAccessContainer : BidirectionalContainer, ~Copyable, ~Escapable -where Element: ~Copyable, Index: Comparable { +where Element: ~Copyable { @_nonoverride func index(_ index: Index, offsetBy n: Int) -> Index @_nonoverride func formIndex(_ index: inout Index, offsetBy n: inout Int, limitedBy limit: Index) @_nonoverride func distance(from start: Index, to end: Index) -> Int @@ -1079,8 +988,6 @@ However, the protocol adds strong semantic complexity requirements across all op | `_customIndexOfEquatableElement(_:)` | O(1) | | `_customLastIndexOfEquatableElement(_:)` | O(1) | -Random-access containers must have a `Comparable` index type. This is mostly a consequence of an O(1) `distance(from:to:)`: if we can measure distances between indices in constant time, then we can simply use the sign of the resulting distance to decide their ordering. Requiring indices to be `Comparable` only take a little bit of additional effort, in that it requires that we can do this based on just the indices themselves, without consulting the container. - Random-access container (and collection) indices are largely isomorphic to integer offsets, and we are often able to increment/decrement them independent of their container. Indeed, well-designed random-access containers often use simple `Int` values as their index; [`Span`][Span], [`InlineArray`][InlineArray], [`UniqueArray`][UniqueArray] all use integer indices, and so do [`RigidArray`][RigidArray], [`RigidDeque`][RigidDeque] and [`UniqueDeque`][UniqueDeque] in swift-collections. To exploit this, and to simplify creating conforming types, `RandomAccessContainer` provides efficient default implementations for most indexing operations if `Index` is `Strideable`: ```swift @@ -1354,7 +1261,7 @@ protocol Producer: ~Copyable, ~Escapable { @discardableResult mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool + ) throws(Failure) -> Int // Requirements with default implementations: @@ -1374,14 +1281,14 @@ The `generate(into:)` member requirement forms the core producer operation: it m @discardableResult mutating func generate( into target: inout OutputSpan - ) throws(Failure) -> Bool + ) throws(Failure) -> Int ``` -This operation generates the next batch of items into the supplied output span instance, which should have room for at least one new element. Repeatedly calling this method produces, in order, all the elements of the underlying sequence. The operation is not guaranteed to fully populate the given output span, but it always appends at least one item until it hits the end of the producer. The returned Boolean indicates the end condition: as soon as the sequence has run out of elements, all subsequent calls return `false` without appending any new items to their target. +This operation generates the next batch of items into the supplied output span instance, which should have room for at least one new element. Repeatedly calling this method produces, in order, all the elements of the underlying sequence. The operation fully populates the given output span, unless it hits the end of the producer or encounters a failure. The returned integer is the number of items generated: as soon as the sequence has run out of elements, all subsequent calls return `0` without appending any new items to their target.
Click to expand footnote -(If a client passes `generate(into:)` a `target` output span that's already full, this operation is allowed to unconditionally return true without doing anything. Some producers are only able to detect that they are finished while trying to actually generate the next item, so we cannot test if the producer has reached its end by passing a zero-capacity span to it.) +(Passing a `target` with no free capacity is a precondition violation. The operation may result in a runtime error in this case, but it is also allowed to unconditionally return zero in this case, whether or not the producer is at its end. There is no way to test if the producer has reached its end without asking it to generate at least one new item.)
@@ -1415,12 +1322,11 @@ extension UniqueArray where Element: ~Copyable { repeat { let c = Swift.max(1, Swift.max(producer.underestimatedCount, self.freeCapacity)) try self.append(addingCount: c) { target throws(E) in - while !target.isFull { - guard try producer.generate(into: &target) else { - done = true - return - } - } + try producer.generate(into: &target) + if !target.isFull { + done = true + return + } } } while !done } @@ -1459,7 +1365,7 @@ extension UniqueArray where Element: ~Copyable { struct SubrangeConsumer: Producer, ~Copyable, ~Escapable { ... } @_lifetime(&self) - mutating func consume(_ subrange: Range) -> SubrangeConsumer { + mutating func consumeSubrange(_ subrange: Range) -> SubrangeConsumer { ... } } @@ -1467,12 +1373,12 @@ extension UniqueArray where Element: ~Copyable { `UniqueArray.SubrangeConsumer` conforms to `Producer` by moving the specified elements out of the array and into the supplied output spans. The subrange consumer extends the duration of the mutation of the array that produced it -- the array is in an inconsistent state while it is being consumed from. When the `SubrangeConsumer` is destroyed, its deinitializer restores the array's invariants by closing the resulting gap in array storage and updating its count to reflect the removals. -(Later on, we'll introduce the [`RangeRemovableContainer` protocol](#range-removable-containers) to standardize the `SubrangeConsumer` shape, allowing us to supply a small family of useful generic algorithms built around it.) +(Later on, we'll introduce the [`DrainableContainer` protocol](#drainable-containers) to standardize the `SubrangeConsumer` shape, allowing us to express in-place consumption and to supply a small family of useful generic algorithms built around it.) -Note how the `Producer` protocol defines an iterator shape, not a sequence. It is the "taking" analogue of `BorrowingIterator`, without a separate `Iterable` analogue. +Note how the `Producer` protocol defines an iterator shape, not a sequence. It is the "taking" analogue of `BorrowingIterator`. We do not provide a producer analogue of the `Iterable` protocol. -`Iterable` provides a uniform way to create a borrowing iterator, but there is no way to _(usefully)_ unify producer creation like that. For example, the `borrowing func map(_:)` and `mutating func consume(_:)` operations above are two prominent instances of `Producer` creation. Neither their ownership modifiers nor their parameter lists lend themselves to easy unification: the parameters are naturally unique, and the modifiers can only be unified by requiring them both to be `consuming` -- which would be unacceptable. +`Iterable` provides a uniform way to create a borrowing iterator, but there is no way to _(usefully)_ unify producer creation like that. For example, the `borrowing func map(_:)` and `mutating func consume(_:)` operations above are two prominent instances of `Producer` creation. Neither their ownership modifiers nor their parameter lists lend themselves to easy unification: the parameters are naturally unique, and the modifiers can only be unified by requiring them both to be `consuming` -- which would be unacceptable. (We can't have a borrowing map or a partial consumption consume the entire container they're invoked on!) Additionally, many producers are inherently nonrepeatable: for instance, a `SubrangeConsumer` can only move the subrange of elements out of its underlying container a single time: once it has transferred their ownership to the client, it cannot do so again. @@ -1491,46 +1397,6 @@ Of course, the issue is that `makeProducer` would be defined as a _consuming_ me ### Default Implementations on `Producer` -``` - /// Return the nearest valid index in this container less than or equal to - /// the given index value, which must be valid in at least one view of self. - /// - /// This operation is important for container types that provide multiple - /// alternative projections (or "views") over the same underlying - /// representation, with each view conforming to `Container`, and sharing - /// the same `Index`. (Like `String` does with its UTF-8, UTF-16, - /// Unicode scalar and character views in the `Collection` world.) - /// This rounding operation enables clients to convert/normalize valid index - /// values in one container view into valid indices in another, allowing them - /// to (easily) decide whether two (potentially misaligned) index values - /// address the same element. - /// - /// The default implementation of this operation simply returns `index`. - /// - /// - Complexity: Recommended to be O(1). Conforming types must clearly - /// document deviations from this expectation. - func index(alignedDown index: Index) -> Index - - /// Return the nearest valid index in this container greater than or equal to - /// the given index value, which must be valid in at least one view of self. - /// - /// This operation is important for container types that provide multiple - /// alternative projections (or "views") over the same underlying - /// representation, with each view conforming to `Container`, and sharing - /// the same `Index`. (Like `String` does with its UTF-8, UTF-16, - /// Unicode scalar and character views in the `Collection` world.) - /// This rounding operation enables clients to convert/normalize valid index - /// values in one container view into valid indices in another, allowing them - /// to (easily) decide whether two (potentially misaligned) index values - /// address the same element. - /// - /// The default implementation of this operation simply returns `index`. - /// - /// - Complexity: Recommended to be O(1). Conforming types must clearly - /// document deviations from this expectation. - func index(alignedUp index: Index) -> Index -``` - ```swift extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { var underestimatedCount: Int { 0 } @@ -1543,9 +1409,12 @@ extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { capacity: Swift.min(maxBufferSize, n) ) { buffer throws(Failure) in repeat { - defer { n &-= buffer.count } - guard try self.generate(into: &buffer) else { return } - buffer.removeAll() + defer { + n &-= buffer.count + buffer.removeAll() + } + try self.generate(into: &buffer) + if !buffer.isFull { return } } while n > 0 } } @@ -1567,6 +1436,23 @@ extension Producer where Self: ~Copyable & ~Escapable, Element: ~Copyable { } ``` +## Counted Producers + +```swift +/// A producer with an exact count. +public protocol CountedProducer: ~Copyable, ~Escapable, Producer +where Element: ~Copyable +{ + var count: Int { get } +} + +extension CountedProducer where Self: ~Copyable & ~Escapable, Element: ~Copyable { + var underestimatedCount: Int { + count + } +} +``` + ## In-Place Consumption ### `struct InputSpan` @@ -1698,11 +1584,15 @@ extension Drain where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_lifetime(target: copy target) mutating func generate( into target: inout OutputSpan - ) throws(Never) -> Bool { - var source = self.drainNext(maxCount: target.freeCapacity) - if source.isEmpty { return false } - target._append(moving: &source) - return true + ) throws(Never) -> Int { + var c = 0 + while !target.isFull { + var source = self.drainNext(maxCount: target.freeCapacity) + guard !source.isEmpty else { break } + c += source.count + target._append(moving: &source) + } + return c } @_lifetime(self: copy self) @@ -1741,28 +1631,61 @@ extension PartialRangeThrough: RangeExpression2 {...} ### Drainable Containers +```swift +public protocol ContainerDrain: Drain, ~Copyable, ~Escapable +where Element: ~Copyable +{ + associatedtype Index + consuming func finalize() -> Index +} +``` + ```swift protocol DrainableContainer : Container, ~Copyable, ~Escapable -where - Element: ~Copyable, - Index: Comparable // For `Range` -{ +where Element: ~Copyable { // MARK: Core requirements - associatedtype SubrangeConsumer: Drain & ~Copyable & ~Escapable + associatedtype SubrangeConsumer: + ContainerDrain & ~Copyable & ~Escapable + where SubrangeConsumer.Index == Index @_lifetime(&self) - mutating func consume(_ subrange: Range) -> SubrangeConsumer + mutating func consumeSubrange(_ subrange: Range) -> SubrangeConsumer + + // FIXME: `consumeAll(where:)` + // FIXME: `removeAll(where:)` + // These should ideally be using SubrangeConsumer, but that requires Drain + // to support partial consumption -- a large complication. // MARK: Requirements with default implementations - mutating func remove(at index: Index) -> Element - mutating func removeSubrange(_ bounds: Range) + /// Removes and returns the element at the specified position. + /// + /// - Parameter index: The position of the element to remove. `index` must be + /// a valid index that is not equal to the end index. + /// On return, `index` is updated to address the position following the + /// removed element. + /// - Returns: The removed element. + @discardableResult + mutating func remove(at index: inout Index) -> Element + + @discardableResult + mutating func consumeSubrange( + _ bounds: Range, + consumingWith consumer: (inout InputSpan) -> Void + ) -> Index + + @discardableResult + mutating func removeSubrange(_ bounds: Range) -> Index + mutating func removeAll() - mutating func removeFirst() -> Element mutating func removeFirst(_ n: Int) mutating func _customRemoveLast() -> Element? + + @_lifetime(&self) + mutating func _customConsumeLast(_ n: Int) -> SubrangeConsumer? + mutating func _customRemoveLast(_ n: Int) -> Bool } ``` @@ -1777,38 +1700,46 @@ where ```swift protocol RangeReplaceableContainer -: RangeRemovableContainer, ~Copyable, ~Escapable -where - Element: ~Copyable, - Index: Comparable // For `Range` +: DrainableContainer, ~Copyable, ~Escapable +where Element: ~Copyable { // MARK: Core requirements var freeCapacity: Int { get } - mutating func replace( - removing subrange: Range, + /// - Returns: A valid index range addressing the newly inserted items. + @discardableResult + mutating func replaceSubrange( + _ subrange: Range, consumingWith consumer: (inout InputSpan) -> Void, addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void - ) throws(E) + ) throws(E) -> Range // MARK: Requirements with default implementations + /// - Returns: A valid index range addressing the newly inserted items. + @discardableResult mutating func insert( addingCount newItemCount: Int, at index: Index, initializingWith initializer: (inout OutputSpan) throws(E) -> Void - ) throws(E) + ) throws(E) -> Range - mutating func insert(_ item: consuming Element, at index: Index) + /// - Returns: A valid index addressing the newly inserted item. + @discardableResult + mutating func insert(_ item: consuming Element, at index: Index) -> Index + /// - Returns: A valid index range addressing the newly inserted items. + @discardableResult mutating func append( addingCount newItemCount: Int, initializingWith initializer: (inout OutputSpan) throws(E) -> Void - ) throws(E) + ) throws(E) -> Range - mutating func append(_ item: consuming Element) + /// - Returns: A valid index addressing the newly inserted item. + @discardableResult + mutating func append(_ item: consuming Element) -> Index } ``` @@ -1821,7 +1752,7 @@ where ### Dynamic Containers ```swift -public protocol DynamicContainer: RangeReplaceableContainer, ~Copyable +protocol DynamicContainer: RangeReplaceableContainer, ~Copyable where Element: ~Copyable { init() @@ -1851,8 +1782,16 @@ where Element: ~Copyable (TODO: Describe the standard algorithms.) +## Sample Conformances + +### `UniqueDeque` + +### `LinkedList` + ## Rejected Directions +### Non-Comparable Indices + ### Cursors ### Index Rounding Operations