mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
move OutputSpan-providing extensions to their own file
- and their tests, too
This commit is contained in:
1 parent
d1bc334b8c
commit
d374c35bdf
4 files changed
+142
-117
No files matched your search
@@ -213,81 +213,3 @@ extension OutputSpan {
|
||||
return .init(start: start, count: initialized)
|
||||
}
|
||||
}
|
||||
|
||||
extension Array {
|
||||
|
||||
public init(
|
||||
capacity: Int,
|
||||
initializingWith initializer: (inout OutputSpan<Element>) throws -> Void
|
||||
) rethrows {
|
||||
try self.init(
|
||||
unsafeUninitializedCapacity: capacity,
|
||||
initializingWith: { (buffer, count) in
|
||||
var output = OutputSpan<Element>(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: buffer.count,
|
||||
owner: buffer
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
assert(initialized.baseAddress == buffer.baseAddress)
|
||||
count = initialized.count
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
|
||||
// also see https://github.com/apple/swift/pull/23050
|
||||
// and `final class __SharedStringStorage`
|
||||
|
||||
@available(macOS 11, *)
|
||||
public init(
|
||||
utf8Capacity capacity: Int,
|
||||
initializingWith initializer: (inout OutputSpan<UInt8>) throws -> Void
|
||||
) rethrows {
|
||||
try self.init(
|
||||
unsafeUninitializedCapacity: capacity,
|
||||
initializingUTF8With: { buffer in
|
||||
var output = OutputSpan(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity,
|
||||
owner: buffer
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
assert(initialized.baseAddress == buffer.baseAddress)
|
||||
return initialized.count
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Data {
|
||||
|
||||
public init(
|
||||
capacity: Int,
|
||||
initializingWith initializer: (inout OutputSpan<UInt8>) throws -> Void
|
||||
) rethrows {
|
||||
self = Data(count: capacity) // initialized with zeroed buffer
|
||||
let count = try self.withUnsafeMutableBytes { rawBuffer in
|
||||
try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
|
||||
buffer.deinitialize()
|
||||
var output = OutputSpan(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity,
|
||||
owner: buffer
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
assert(initialized.baseAddress == buffer.baseAddress)
|
||||
return initialized.count
|
||||
}
|
||||
}
|
||||
assert(count <= self.count)
|
||||
self.replaceSubrange(count..<self.count, with: EmptyCollection())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
//===--- StdlibOutputSpanExtensions.swift ---------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2024 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
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension Array {
|
||||
|
||||
public init(
|
||||
capacity: Int,
|
||||
initializingWith initializer: (inout OutputSpan<Element>) throws -> Void
|
||||
) rethrows {
|
||||
try self.init(
|
||||
unsafeUninitializedCapacity: capacity,
|
||||
initializingWith: { (buffer, count) in
|
||||
var output = OutputSpan<Element>(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: buffer.count,
|
||||
owner: buffer
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
assert(initialized.baseAddress == buffer.baseAddress)
|
||||
count = initialized.count
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
|
||||
// also see https://github.com/apple/swift/pull/23050
|
||||
// and `final class __SharedStringStorage`
|
||||
|
||||
@available(macOS 11, *)
|
||||
public init(
|
||||
utf8Capacity capacity: Int,
|
||||
initializingWith initializer: (inout OutputSpan<UInt8>) throws -> Void
|
||||
) rethrows {
|
||||
try self.init(
|
||||
unsafeUninitializedCapacity: capacity,
|
||||
initializingUTF8With: { buffer in
|
||||
var output = OutputSpan(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity,
|
||||
owner: buffer
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
assert(initialized.baseAddress == buffer.baseAddress)
|
||||
return initialized.count
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Data {
|
||||
|
||||
public init(
|
||||
capacity: Int,
|
||||
initializingWith initializer: (inout OutputSpan<UInt8>) throws -> Void
|
||||
) rethrows {
|
||||
self = Data(count: capacity) // initialized with zeroed buffer
|
||||
let count = try self.withUnsafeMutableBytes { rawBuffer in
|
||||
try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
|
||||
buffer.deinitialize()
|
||||
var output = OutputSpan(
|
||||
initializing: buffer.baseAddress.unsafelyUnwrapped,
|
||||
capacity: capacity,
|
||||
owner: buffer
|
||||
)
|
||||
try initializer(&output)
|
||||
let initialized = output.relinquishBorrowedMemory()
|
||||
assert(initialized.baseAddress == buffer.baseAddress)
|
||||
return initialized.count
|
||||
}
|
||||
}
|
||||
assert(count <= self.count)
|
||||
self.replaceSubrange(count..<self.count, with: EmptyCollection())
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Foundation
|
||||
import Future
|
||||
|
||||
struct Allocation<T>: ~Copyable {
|
||||
@@ -213,41 +212,3 @@ final class OutputBufferTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final class OutputBufferUsageTests: XCTestCase {
|
||||
|
||||
func testArrayInitializationExample() {
|
||||
var array: [UInt8]
|
||||
array = Array(capacity: 32, initializingWith: { output in
|
||||
for i in 0..<(output.capacity/2) {
|
||||
output.appendElement(UInt8(clamping: i))
|
||||
}
|
||||
})
|
||||
XCTAssertEqual(array.count, 16)
|
||||
XCTAssert(array.elementsEqual(0..<16))
|
||||
XCTAssertGreaterThanOrEqual(array.capacity, 32)
|
||||
}
|
||||
|
||||
func testDataInitializationExample() {
|
||||
var data: Data
|
||||
data = Data(capacity: 32, initializingWith: { output in
|
||||
for i in 0..<(output.capacity/2) {
|
||||
output.appendElement(UInt8(clamping: i))
|
||||
}
|
||||
})
|
||||
XCTAssertEqual(data.count, 16)
|
||||
XCTAssert(data.elementsEqual(0..<16))
|
||||
}
|
||||
|
||||
func testStringInitializationExample() {
|
||||
var string: String
|
||||
let c = UInt8(ascii: "A")
|
||||
string = String(utf8Capacity: 32, initializingWith: { output in
|
||||
for i in 0..<(output.capacity/2) {
|
||||
output.appendElement(c + UInt8(clamping: i))
|
||||
}
|
||||
})
|
||||
XCTAssertEqual(string.utf8.count, 16)
|
||||
XCTAssert(string.utf8.elementsEqual(c..<(c+16)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
//===--- StdlibOutputSpanExtensionTests.swift -----------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2024 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
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import XCTest
|
||||
import Foundation
|
||||
import Future
|
||||
|
||||
final class OutputBufferUsageTests: XCTestCase {
|
||||
|
||||
func testArrayInitializationExample() {
|
||||
var array: [UInt8]
|
||||
array = Array(capacity: 32, initializingWith: { output in
|
||||
for i in 0..<(output.capacity/2) {
|
||||
output.appendElement(UInt8(clamping: i))
|
||||
}
|
||||
})
|
||||
XCTAssertEqual(array.count, 16)
|
||||
XCTAssert(array.elementsEqual(0..<16))
|
||||
XCTAssertGreaterThanOrEqual(array.capacity, 32)
|
||||
}
|
||||
|
||||
func testDataInitializationExample() {
|
||||
var data: Data
|
||||
data = Data(capacity: 32, initializingWith: { output in
|
||||
for i in 0..<(output.capacity/2) {
|
||||
output.appendElement(UInt8(clamping: i))
|
||||
}
|
||||
})
|
||||
XCTAssertEqual(data.count, 16)
|
||||
XCTAssert(data.elementsEqual(0..<16))
|
||||
}
|
||||
|
||||
func testStringInitializationExample() {
|
||||
var string: String
|
||||
let c = UInt8(ascii: "A")
|
||||
string = String(utf8Capacity: 32, initializingWith: { output in
|
||||
for i in 0..<(output.capacity/2) {
|
||||
output.appendElement(c + UInt8(clamping: i))
|
||||
}
|
||||
})
|
||||
XCTAssertEqual(string.utf8.count, 16)
|
||||
XCTAssert(string.utf8.elementsEqual(c..<(c+16)))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user