Files
swift-collections/Sources/SpanPreview/MutableSpan+InputSpanHelpers.swift

92 lines
3.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
#if compiler(>=6.4) && UnstableContainersPreview
@available(SwiftStdlib 5.0, *)
extension MutableSpan where Element: ~Copyable {
// FIXME: Replace with stdlib implementation when it becomes available
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
@_lifetime(source: copy source)
package mutating func _updateSubrange(
_ subrange: Range<Index>,
moving source: inout InputSpan<Element>
) {
precondition(
subrange.lowerBound >= 0 && subrange.upperBound <= count,
"Index range out of bounds")
precondition(
subrange.count == source.count,
"updateSubrange source count doesn't match target")
guard !subrange.isEmpty else { return }
self.withUnsafeMutableBufferPointer { dst in
unsafe source.withUnsafeMutableBufferPointer { src, c in
// FIXME: Make sure this calls memcpy when Element is bitwise movable.
#if false // FIXME: UMBP.moveUpdate(fromContentsOf:) is broken as of 2026-09-17 (rdar://187733648)
let i = unsafe dst
._extracting(unchecked: subrange)
.moveUpdate(fromContentsOf: src._extracting(last: c))
precondition(i == subrange.count)
#else
var d = unsafe dst._ptr(at: subrange.lowerBound)
var s = unsafe src._ptr(at: src.count - c)
for i in 0 ..< c {
unsafe d.pointee = s.move()
unsafe d += 1
unsafe s += 1
}
#endif
c = 0
}
}
}
// FIXME: Replace with stdlib implementation when it becomes available
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
@_lifetime(source: copy source)
package mutating func _updateAll(
moving source: inout InputSpan<Element>
) {
let count = self.count
precondition(
source.count == count,
"updateSubrange source count doesn't match target")
guard !self.isEmpty else { return }
self.withUnsafeMutableBufferPointer { dst in
unsafe source.withUnsafeMutableBufferPointer { src, c in
// FIXME: Make sure this calls memcpy when Element is bitwise movable.
#if false // FIXME: UMBP.moveUpdate(fromContentsOf:) is broken as of 2026-09-17 (rdar://187733648)
let i = unsafe dst.moveUpdate(fromContentsOf: src._extracting(last: c))
assert(i == count)
#else
var d = unsafe dst._ptr(at: 0)
var s = unsafe src._ptr(at: src.count - c)
for i in 0 ..< c {
unsafe d.pointee = s.move()
unsafe d += 1
unsafe s += 1
}
#endif
c = 0
}
}
}
}
#endif