mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
[BasicContainers] Fix UniqueArray.append(moving:) dropping and leaking every element
Signed-off-by: Aniket <aniketkomailkro@gmail.com>
This commit is contained in:
1 parent
b905c889ff
commit
80d8052b4b
2 files changed
+27
-1
No files matched your search
@@ -160,8 +160,9 @@ extension UniqueArray where Element: ~Copyable {
|
||||
_ensureFreeCapacity(items.count)
|
||||
return items.edit { source in
|
||||
unsafe source.withUnsafeMutableBufferPointer { buffer, count in
|
||||
let extracted = unsafe buffer._extracting(first: count)
|
||||
count = 0
|
||||
return unsafe _storage.append(moving: buffer._extracting(first: count))
|
||||
return unsafe _storage.append(moving: extracted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -781,6 +781,31 @@ class UniqueArrayTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_append_moving_RigidArray() {
|
||||
withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in
|
||||
withEvery("additions", in: [0, 1, 10, 100]) { additions in
|
||||
withLifetimeTracking { tracker in
|
||||
var a = tracker.uniqueArray(layout: layout)
|
||||
var b = RigidArray(capacity: additions) { span in
|
||||
for i in 0 ..< additions {
|
||||
span.append(tracker.instance(for: layout.count + i))
|
||||
}
|
||||
}
|
||||
let range = a.append(moving: &b)
|
||||
let c = layout.count + additions
|
||||
expectEqual(range, layout.count ..< c)
|
||||
expectEqual(b.count, 0)
|
||||
expectUniqueArrayContents(
|
||||
a,
|
||||
equivalentTo: 0 ..< c,
|
||||
by: { $0.payload == $1 },
|
||||
printer: { "\($0.payload)" })
|
||||
expectEqual(tracker.instances, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if UnstableContainersPreview
|
||||
#if false // FIXME: Update
|
||||
func test_append_copying_Container() {
|
||||
|
||||
Reference in new issue
Block a user