From a4f5085a7cdf38b89261303211ec1d83a0a473d0 Mon Sep 17 00:00:00 2001 From: hamed Date: Sat, 10 Oct 2026 22:56:07 +0330 Subject: [PATCH 1/2] Copy every source span in replaceSubrange(copying:). The replacement callback took a single span and then required the source count to be exhausted, so a container stored in more than one span trapped. Fixes #751 Co-authored-by: Cursor --- .../Container/RangeReplaceableContainer.swift | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift index b14314cd..d6a886d8 100644 --- a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift @@ -578,9 +578,16 @@ where var c = items.count var i = items.startIndex let range = self.replaceSubrange(subrange, addingCount: c) { target in - let source = items.nextSpan(after: &i, maxCount: target.freeCapacity) - target._append(copying: source) - c -= source.count + // A source container may be split across several spans. Keep copying + // until this output span is full or the source is exhausted; a single + // `nextSpan` leaves the remaining spans uncopied and trips the + // precondition below. + while !target.isFull { + let source = items.nextSpan(after: &i, maxCount: target.freeCapacity) + if source.isEmpty { break } + target._append(copying: source) + c -= source.count + } } precondition(c == 0, "Invalid RangeReplaceableContainer") return range From 35aedf393726e523c846400a58e7b06a29382cd6 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 12 Oct 2026 11:29:41 -0700 Subject: [PATCH 2/2] Remove unnecessary explanation --- .../Protocols/Container/RangeReplaceableContainer.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift index d6a886d8..883ed98a 100644 --- a/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/RangeReplaceableContainer.swift @@ -578,10 +578,6 @@ where var c = items.count var i = items.startIndex let range = self.replaceSubrange(subrange, addingCount: c) { target in - // A source container may be split across several spans. Keep copying - // until this output span is full or the source is exhausted; a single - // `nextSpan` leaves the remaining spans uncopied and trips the - // precondition below. while !target.isFull { let source = items.nextSpan(after: &i, maxCount: target.freeCapacity) if source.isEmpty { break }