mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
OutputSpan modernization
This commit is contained in:
1 parent
28b93832bc
commit
ffebc71d54
3 files changed
+385
-132
No files matched your search
+327
-117
@@ -10,54 +10,214 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public struct OutputSpan<Element: ~Copyable /*& ~Escapable*/>: ~Copyable, ~Escapable {
|
||||
@usableFromInline let _start: UnsafeMutablePointer<Element>
|
||||
// OutputSpan<Element> represents a span of memory which contains
|
||||
// a variable number of `Element` instances, followed by uninitialized memory.
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@frozen
|
||||
public struct OutputSpan<Element: ~Copyable>: ~Copyable, ~Escapable {
|
||||
@usableFromInline let _pointer: UnsafeMutableRawPointer?
|
||||
|
||||
public let capacity: Int
|
||||
public private(set) var initialized: Int = 0
|
||||
|
||||
@usableFromInline
|
||||
var _initialized: Int = 0
|
||||
|
||||
@usableFromInline @inline(__always)
|
||||
var _start: UnsafeMutableRawPointer { _pointer.unsafelyUnwrapped }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public var available: Int { capacity &- _initialized }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public var count: Int { _initialized }
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public var isEmpty: Bool { _initialized == 0 }
|
||||
|
||||
deinit {
|
||||
// `self` always borrows memory, and it shouldn't have gotten here.
|
||||
// Failing to use `relinquishBorrowedMemory()` is an error.
|
||||
if initialized > 0 {
|
||||
if _initialized > 0 {
|
||||
#if false
|
||||
_start.withMemoryRebound(to: Element.self, capacity: _initialized) {
|
||||
$0.deinitialize(count: _initialized)
|
||||
}
|
||||
#else
|
||||
fatalError()
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
initializing pointer: UnsafeMutablePointer<Element>,
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@usableFromInline @inline(__always)
|
||||
init(
|
||||
_unchecked start: UnsafeMutableRawPointer?,
|
||||
capacity: Int,
|
||||
initialized: Int = 0,
|
||||
owner: borrowing Owner
|
||||
) -> dependsOn(owner) Self {
|
||||
self._start = pointer
|
||||
initialized: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
_pointer = start
|
||||
self.capacity = capacity
|
||||
self.initialized = initialized
|
||||
_initialized = initialized
|
||||
}
|
||||
}
|
||||
|
||||
extension OutputSpan where Element: ~Copyable /*& ~Escapable*/ {
|
||||
public mutating func appendElement(_ value: consuming Element) {
|
||||
precondition(initialized < capacity, "Output buffer overflow")
|
||||
_start.advanced(by: initialized).initialize(to: value)
|
||||
initialized &+= 1
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@available(*, unavailable)
|
||||
extension OutputSpan: Sendable {}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@usableFromInline @inline(__always)
|
||||
init(
|
||||
_unchecked buffer: UnsafeMutableBufferPointer<Element>,
|
||||
initialized: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
_pointer = .init(buffer.baseAddress)
|
||||
capacity = buffer.count
|
||||
_initialized = initialized
|
||||
}
|
||||
|
||||
public mutating func deinitializeLastElement() -> Element? {
|
||||
guard initialized > 0 else { return nil }
|
||||
initialized &-= 1
|
||||
return _start.advanced(by: initialized).move()
|
||||
}
|
||||
}
|
||||
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
public mutating func deinitialize() {
|
||||
let b = UnsafeMutableBufferPointer(start: _start, count: initialized)
|
||||
b.deinitialize()
|
||||
initialized = 0
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_initializing buffer: UnsafeMutableBufferPointer<Element>,
|
||||
initialized: Int = 0
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(
|
||||
((Int(bitPattern: buffer.baseAddress) &
|
||||
(MemoryLayout<Element>.alignment&-1)) == 0),
|
||||
"baseAddress must be properly aligned to access Element"
|
||||
)
|
||||
self.init(_unchecked: buffer, initialized: initialized)
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_initializing pointer: UnsafeMutablePointer<Element>,
|
||||
capacity: Int,
|
||||
initialized: Int = 0
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(capacity >= 0, "Capacity must be 0 or greater")
|
||||
self.init(
|
||||
_initializing: .init(start: pointer, count: capacity),
|
||||
initialized: initialized
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan {
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_initializing buffer: Slice<UnsafeMutableBufferPointer<Element>>,
|
||||
initialized: Int = 0
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_initializing: .init(rebasing: buffer), initialized: initialized)
|
||||
}
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan where Element: BitwiseCopyable {
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_initializing bytes: UnsafeMutableRawBufferPointer,
|
||||
initialized: Int = 0
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(
|
||||
((Int(bitPattern: bytes.baseAddress) &
|
||||
(MemoryLayout<Element>.alignment&-1)) == 0),
|
||||
"baseAddress must be properly aligned to access Element"
|
||||
)
|
||||
let (byteCount, stride) = (bytes.count, MemoryLayout<Element>.stride)
|
||||
let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride)
|
||||
precondition(remainder == 0, "Span must contain a whole number of elements")
|
||||
self.init(
|
||||
_unchecked: bytes.baseAddress, capacity: count, initialized: initialized
|
||||
)
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_initializing pointer: UnsafeMutableRawPointer,
|
||||
capacity: Int,
|
||||
initialized: Int = 0
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(capacity >= 0, "Capacity must be 0 or greater")
|
||||
self.init(
|
||||
_initializing: .init(start: pointer, count: capacity),
|
||||
initialized: initialized
|
||||
)
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_initializing buffer: Slice<UnsafeMutableRawBufferPointer>,
|
||||
initialized: Int = 0
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(
|
||||
_initializing: UnsafeMutableRawBufferPointer(rebasing: buffer),
|
||||
initialized: initialized
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func appendElement(_ value: consuming Element) {
|
||||
precondition(_initialized < capacity, "Output buffer overflow")
|
||||
let p = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
p.initializeMemory(as: Element.self, to: value)
|
||||
_initialized &+= 1
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func deinitializeLastElement() -> Element? {
|
||||
guard _initialized > 0 else { return nil }
|
||||
_initialized &-= 1
|
||||
let p = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
return p.withMemoryRebound(to: Element.self, capacity: 1, { $0.move() })
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func deinitialize() {
|
||||
_ = _start.withMemoryRebound(to: Element.self, capacity: _initialized) {
|
||||
$0.deinitialize(count: _initialized)
|
||||
}
|
||||
_initialized = 0
|
||||
}
|
||||
}
|
||||
|
||||
//MARK: bulk-update functions
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan {
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append(repeating repeatedValue: Element, count: Int) {
|
||||
let available = capacity &- _initialized
|
||||
precondition(
|
||||
count <= available,
|
||||
"destination span cannot contain number of elements requested."
|
||||
)
|
||||
let offset = _initialized&*MemoryLayout<Element>.stride
|
||||
let p = _start.advanced(by: offset)
|
||||
p.withMemoryRebound(to: Element.self, capacity: count) {
|
||||
$0.initialize(repeating: repeatedValue, count: count)
|
||||
}
|
||||
_initialized &+= count
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append<S>(
|
||||
from elements: S
|
||||
) -> S.Iterator where S: Sequence, S.Element == Element {
|
||||
@@ -66,85 +226,138 @@ extension OutputSpan {
|
||||
return iterator
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append(
|
||||
from elements: inout some IteratorProtocol<Element>
|
||||
) {
|
||||
while initialized < capacity {
|
||||
while _initialized < capacity {
|
||||
guard let element = elements.next() else { break }
|
||||
_start.advanced(by: initialized).initialize(to: element)
|
||||
initialized &+= 1
|
||||
let p = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
p.initializeMemory(as: Element.self, to: element)
|
||||
_initialized &+= 1
|
||||
}
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append(
|
||||
fromContentsOf source: some Collection<Element>
|
||||
) {
|
||||
let count = source.withContiguousStorageIfAvailable {
|
||||
guard let sourceAddress = $0.baseAddress, !$0.isEmpty else {
|
||||
return 0
|
||||
}
|
||||
let available = capacity &- initialized
|
||||
precondition(
|
||||
$0.count <= available,
|
||||
"buffer cannot contain every element from source."
|
||||
)
|
||||
let tail = _start.advanced(by: initialized)
|
||||
tail.initialize(from: sourceAddress, count: $0.count)
|
||||
return $0.count
|
||||
let void: Void? = source.withContiguousStorageIfAvailable {
|
||||
#if false
|
||||
append(fromContentsOf: Span(_unsafeElements: $0))
|
||||
#else //FIXME: remove once rdar://136838539 & rdar://136849171 are fixed
|
||||
append(fromContentsOf: $0)
|
||||
#endif
|
||||
}
|
||||
if let count {
|
||||
initialized &+= count
|
||||
if void != nil {
|
||||
return
|
||||
}
|
||||
|
||||
let available = capacity &- initialized
|
||||
let tail = _start.advanced(by: initialized)
|
||||
let suffix = UnsafeMutableBufferPointer(start: tail, count: available)
|
||||
var (iterator, copied) = source._copyContents(initializing: suffix)
|
||||
let available = capacity &- _initialized
|
||||
let tail = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
var (iterator, copied) =
|
||||
tail.withMemoryRebound(to: Element.self, capacity: available) {
|
||||
let suffix = UnsafeMutableBufferPointer(start: $0, count: available)
|
||||
return source._copyContents(initializing: suffix)
|
||||
}
|
||||
precondition(
|
||||
iterator.next() == nil,
|
||||
"buffer cannot contain every element from source."
|
||||
"destination span cannot contain every element from source."
|
||||
)
|
||||
assert(initialized + copied <= capacity)
|
||||
initialized &+= copied
|
||||
assert(_initialized + copied <= capacity) // invariant check
|
||||
_initialized &+= copied
|
||||
}
|
||||
|
||||
//FIXME: remove once rdar://136838539 & rdar://136849171 are fixed
|
||||
public mutating func append(
|
||||
fromContentsOf source: UnsafeBufferPointer<Element>
|
||||
) {
|
||||
guard !source.isEmpty else { return }
|
||||
precondition(
|
||||
source.count <= available,
|
||||
"destination span cannot contain every element from source."
|
||||
)
|
||||
let tail = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
source.baseAddress!.withMemoryRebound(to: Element.self, capacity: source.count) {
|
||||
_ = tail.initializeMemory(as: Element.self, from: $0, count: source.count)
|
||||
}
|
||||
_initialized += source.count
|
||||
}
|
||||
|
||||
//FIXME: rdar://136838539 & rdar://136849171
|
||||
public mutating func append(fromContentsOf source: Span<Element>) {
|
||||
let available = capacity &- initialized
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append(
|
||||
fromContentsOf source: Span<Element>
|
||||
) {
|
||||
guard !source.isEmpty else { return }
|
||||
precondition(
|
||||
source.count <= available,
|
||||
"buffer cannot contain every element from source."
|
||||
"destination span cannot contain every element from source."
|
||||
)
|
||||
source.withUnsafeBufferPointer {
|
||||
let tail = _start.advanced(by: initialized)
|
||||
tail.initialize(from: $0.baseAddress!, count: $0.count)
|
||||
let tail = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
source._start.withMemoryRebound(to: Element.self, capacity: source.count) {
|
||||
_ = tail.initializeMemory(as: Element.self, from: $0, count: source.count)
|
||||
}
|
||||
initialized &+= source.count
|
||||
_initialized += source.count
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func append(fromContentsOf source: borrowing MutableSpan<Element>) {
|
||||
source.withUnsafeBufferPointer { append(fromContentsOf: $0) }
|
||||
}
|
||||
}
|
||||
|
||||
extension OutputSpan where Element: ~Copyable /*& ~Escapable*/ {
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func moveAppend(
|
||||
fromContentsOf source: UnsafeMutableBufferPointer<Element>
|
||||
fromContentsOf source: consuming Self
|
||||
) {
|
||||
guard let sourceAddress = source.baseAddress, !source.isEmpty else {
|
||||
return
|
||||
}
|
||||
let available = capacity &- initialized
|
||||
guard !source.isEmpty else { return }
|
||||
precondition(
|
||||
source.count <= available,
|
||||
"buffer cannot contain every element from source."
|
||||
)
|
||||
let tail = _start.advanced(by: initialized)
|
||||
tail.moveInitialize(from: sourceAddress, count: source.count)
|
||||
initialized &+= source.count
|
||||
let buffer = source.relinquishBorrowedMemory()
|
||||
// we must now deinitialize the returned UMBP
|
||||
let tail = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
tail.moveInitializeMemory(
|
||||
as: Element.self, from: buffer.baseAddress!, count: buffer.count
|
||||
)
|
||||
_initialized &+= buffer.count
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func moveAppend(
|
||||
fromContentsOf source: UnsafeMutableBufferPointer<Element>
|
||||
) {
|
||||
#if false //FIXME: rdar://136838539 & rdar://136849171
|
||||
let source = OutputSpan(_initializing: source, initialized: source.count)
|
||||
moveAppend(fromContentsOf: source)
|
||||
#else
|
||||
guard !source.isEmpty else { return }
|
||||
precondition(
|
||||
source.count <= available,
|
||||
"buffer cannot contain every element from source."
|
||||
)
|
||||
let tail = _start.advanced(by: _initialized&*MemoryLayout<Element>.stride)
|
||||
tail.moveInitializeMemory(
|
||||
as: Element.self, from: source.baseAddress!, count: source.count
|
||||
)
|
||||
_initialized &+= source.count
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
extension OutputSpan {
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func moveAppend(
|
||||
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
|
||||
) {
|
||||
@@ -152,64 +365,61 @@ extension OutputSpan {
|
||||
}
|
||||
}
|
||||
|
||||
//extension OutputSpan where Element: ~Copyable /*& ~Escapable*/ {
|
||||
// public mutating func initializeSuffixOutOfOrder<R, E>(
|
||||
// _ count: Int,
|
||||
// body: (inout RandomAccessOutputSpan<Element>) throws(E) -> R
|
||||
// ) throws(E) -> R {
|
||||
// precondition(initialized + count < capacity)
|
||||
// var out = RandomAccessOutputSpan<Element>(
|
||||
// initializing: _start.advanced(by: initialized), capacity: count, owner: self
|
||||
// )
|
||||
// let result = try body(&out)
|
||||
// let buffer = out.relinquishBorrowedMemory()
|
||||
// assert(
|
||||
// buffer.baseAddress == _start.advanced(by: initialized) &&
|
||||
// initialized + buffer.count < capacity
|
||||
// )
|
||||
// initialized &+= buffer.count
|
||||
// return result
|
||||
// }
|
||||
//}
|
||||
extension OutputSpan where Element: BitwiseCopyable {
|
||||
|
||||
extension OutputSpan {
|
||||
}
|
||||
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public var initializedPrefix: Span<Element> {
|
||||
get { Span(_unsafeStart: _start, count: initialized) }
|
||||
get { Span(_unchecked: _pointer, count: _initialized) }
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public func withSpan<E: Error, R: ~Copyable>(
|
||||
_ body: (Span<Element>) throws(E) -> R
|
||||
) throws(E) -> R {
|
||||
try body(initializedPrefix)
|
||||
}
|
||||
}
|
||||
|
||||
// public var mutatingInitializedPrefix: /*inout*/ MutableBufferView<Element> {
|
||||
// mutating /* _read */ get /* mutating(self) */ {
|
||||
// /* yield */ MutableBufferView(
|
||||
// unsafeMutablePointer: _start,
|
||||
// count: initialized,
|
||||
// dependsOn: /* self */ _start
|
||||
// )
|
||||
// }
|
||||
// }
|
||||
|
||||
// public mutating func withMutableBufferView<R>(
|
||||
// _ body: (inout MutableBufferView<Element>) throws -> R
|
||||
// ) rethrows -> R {
|
||||
// var view = MutableBufferView<Element>(
|
||||
// unsafeMutablePointer: _start,
|
||||
// count: initialized,
|
||||
// dependsOn: /* self */ _start
|
||||
// )
|
||||
// return try body(&view)
|
||||
// }
|
||||
|
||||
extension OutputSpan {
|
||||
|
||||
public consuming func relinquishBorrowedMemory() -> UnsafeMutableBufferPointer<Element> {
|
||||
let (start, initialized) = (self._start, self.initialized)
|
||||
discard self
|
||||
return .init(start: start, count: initialized)
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
@_alwaysEmitIntoClient
|
||||
public mutating func withMutableSpan<E: Error, Result: ~Copyable>(
|
||||
_ body: (inout MutableSpan<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
var span = MutableSpan<Element>(_unchecked: _pointer, count: _initialized)
|
||||
defer {
|
||||
precondition(
|
||||
span.count == _initialized && span._pointer == _start,
|
||||
"Substituting the MutableSpan is unsound and unsafe."
|
||||
)
|
||||
}
|
||||
return try body(&span)
|
||||
}
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan where Element: ~Copyable {
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public consuming func relinquishBorrowedMemory() -> UnsafeMutableBufferPointer<Element> {
|
||||
let (start, count) = (self._pointer, self._initialized)
|
||||
discard self
|
||||
let typed = start?.bindMemory(to: Element.self, capacity: count)
|
||||
return .init(start: typed, count: count)
|
||||
}
|
||||
}
|
||||
|
||||
@_disallowFeatureSuppression(NonescapableTypes)
|
||||
extension OutputSpan where Element: BitwiseCopyable {
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
public consuming func relinquishBorrowedBytes() -> UnsafeMutableRawBufferPointer {
|
||||
let (start, count) = (self._pointer, self._initialized)
|
||||
discard self
|
||||
return .init(start: start, count: count&*MemoryLayout<Element>.stride)
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,8 @@ extension Array {
|
||||
unsafeUninitializedCapacity: capacity,
|
||||
initializingWith: { (buffer, count) in
|
||||
var output = OutputSpan<Element>(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: buffer.count,
|
||||
owner: buffer
|
||||
_initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: buffer.count
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
@@ -47,9 +46,8 @@ extension String {
|
||||
unsafeUninitializedCapacity: capacity,
|
||||
initializingUTF8With: { buffer in
|
||||
var output = OutputSpan(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity,
|
||||
owner: buffer
|
||||
_initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
@@ -73,9 +71,8 @@ extension Data {
|
||||
try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
|
||||
buffer.deinitialize()
|
||||
var output = OutputSpan(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity,
|
||||
owner: buffer
|
||||
_initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
|
||||
@@ -31,7 +31,7 @@ struct Allocation<T>: ~Copyable {
|
||||
) rethrows {
|
||||
if count != nil { fatalError() }
|
||||
var outputBuffer = OutputSpan<T>(
|
||||
initializing: allocation, capacity: capacity, owner: self
|
||||
_initializing: allocation, capacity: capacity
|
||||
)
|
||||
do {
|
||||
try body(&outputBuffer)
|
||||
@@ -85,7 +85,7 @@ final class OutputBufferTests: XCTestCase {
|
||||
let allocation = UnsafeMutablePointer<UInt8>.allocate(capacity: c)
|
||||
defer { allocation.deallocate() }
|
||||
|
||||
let ob = OutputSpan(initializing: allocation, capacity: c, owner: allocation)
|
||||
let ob = OutputSpan(_initializing: allocation, capacity: c)
|
||||
let initialized = ob.relinquishBorrowedMemory()
|
||||
XCTAssertNotNil(initialized.baseAddress)
|
||||
XCTAssertEqual(initialized.count, 0)
|
||||
@@ -110,6 +110,24 @@ final class OutputBufferTests: XCTestCase {
|
||||
XCTAssert(span._elementsEqual(0..<c))
|
||||
}
|
||||
|
||||
func testInitializeBufferByAppendingRepeatedElements() {
|
||||
var a = Allocation(of: 48, Int.self)
|
||||
let c = 10
|
||||
a.initialize {
|
||||
$0.append(repeating: c, count: c)
|
||||
let oops = $0.deinitializeLastElement()
|
||||
XCTAssertEqual(oops, c)
|
||||
XCTAssertEqual($0.count, c-1)
|
||||
}
|
||||
a.examine {
|
||||
XCTAssertEqual($0.count, c-1)
|
||||
XCTAssert($0._elementsEqual(Array(repeating: c, count: c-1)))
|
||||
}
|
||||
let span = a.initializedPrefix
|
||||
XCTAssertEqual(span.count, c-1)
|
||||
XCTAssert(span._elementsEqual(Array(repeating: c, count: c-1)))
|
||||
}
|
||||
|
||||
func testInitializeBufferFromSequence() {
|
||||
var a = Allocation(of: 48, Int.self)
|
||||
a.initialize {
|
||||
@@ -154,9 +172,11 @@ final class OutputBufferTests: XCTestCase {
|
||||
a.initialize {
|
||||
let array = Array(0..<c)
|
||||
#if false
|
||||
// let storage = array.storage
|
||||
let storage = Span<Int>(_unsafeElements: UnsafeBufferPointer<Int>(start: nil, count: 0))
|
||||
$0.append(fromContentsOf: storage)
|
||||
array.withUnsafeBufferPointer {
|
||||
// let storage = Span(_unsafeElements: $0)
|
||||
let storage = Span<Int>(_unsafeElements: .init(start: nil, count: 0))
|
||||
$0.append(fromContentsOf: storage)
|
||||
}
|
||||
#else
|
||||
$0.append(fromContentsOf: array)
|
||||
#endif
|
||||
@@ -203,7 +223,7 @@ final class OutputBufferTests: XCTestCase {
|
||||
try a.initialize {
|
||||
$0.appendElement(0)
|
||||
$0.appendElement(1)
|
||||
XCTAssertTrue($0.initialized > 0)
|
||||
XCTAssertTrue($0.count > 0)
|
||||
throw MyTestError.error
|
||||
}
|
||||
}
|
||||
@@ -211,4 +231,30 @@ final class OutputBufferTests: XCTestCase {
|
||||
XCTAssertEqual(a.isInitialized, false)
|
||||
}
|
||||
}
|
||||
|
||||
func testMutateOutputSpan() throws {
|
||||
let b = UnsafeMutableBufferPointer<Int>.allocate(capacity: 10)
|
||||
defer { b.deallocate() }
|
||||
|
||||
var span = OutputSpan(_initializing: b)
|
||||
XCTAssertEqual(span.count, 0)
|
||||
span.append(fromContentsOf: 0..<10)
|
||||
XCTAssertEqual(span.count, 10)
|
||||
|
||||
span.withMutableSpan {
|
||||
#if false
|
||||
let b = UnsafeMutableBufferPointer<Int>.allocate(capacity: 8)
|
||||
b.initialize(repeating: .max)
|
||||
$0 = MutableSpan(_unsafeElements: b)
|
||||
#else
|
||||
for i in 0..<$0.count {
|
||||
$0[i] *= 2
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
let r = span.relinquishBorrowedMemory()
|
||||
print(Array(r))
|
||||
r.deinitialize()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user