Skip to content

Commit ca6353d

Browse files
committed
Update raw version to return a typed buffer
1 parent 8ade724 commit ca6353d

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public struct Unsafe${Mutable}RawBufferPointer
427427
/// `source` and binds the initialized memory to type `T`.
428428
///
429429
/// Returns an iterator to any elements of `source` that didn't fit in the
430-
/// buffer, and an index into the buffer one past the last byte written.
430+
/// buffer, and a typed buffer of the written elements.
431431
///
432432
/// - Precondition: The memory in `startIndex..<(source.count *
433433
/// MemoryLayout<T>.stride)` is uninitialized or initialized to a
@@ -436,16 +436,18 @@ public struct Unsafe${Mutable}RawBufferPointer
436436
/// - Precondition: The buffer must contain sufficient memory to
437437
/// accommodate at least `source.underestimateCount` elements.
438438
///
439-
/// - Postcondition: The memory at `self[startIndex..<returned index]
439+
/// - Postcondition: The memory at `self[startIndex..<source.count *
440+
/// MemoryLayout<T>.stride]
440441
/// is bound to type `T`.
441442
///
442-
/// - Postcondition: The `T` values at `self[startIndex..<returned index]`
443+
/// - Postcondition: The `T` values at `self[startIndex..<source.count *
444+
/// MemoryLayout<T>.stride]`
443445
/// are initialized.
444446
///
445447
// TODO: Optimize where `C` is a `ContiguousArrayBuffer`.
446448
public func initializeMemory<S: Sequence>(
447449
as: S.Iterator.Element.Type, from source: S
448-
) -> (unwritten: S.Iterator, initializedUpTo: Index) {
450+
) -> (unwritten: S.Iterator, initialized: UnsafeMutableBufferPointer<S.Iterator.Element>) {
449451

450452
var it = source.makeIterator()
451453
var idx = startIndex
@@ -456,8 +458,9 @@ public struct Unsafe${Mutable}RawBufferPointer
456458
"insufficient space to accommodate source.underestimatedCount elements")
457459
guard let base = baseAddress else {
458460
// this can be a precondition since only an invalid argument should be costly
459-
_precondition(source.underestimatedCount == 0, "no memory available to initialize from source")
460-
return (it, startIndex)
461+
_precondition(source.underestimatedCount == 0,
462+
"no memory available to initialize from source")
463+
return (it, UnsafeMutableBufferPointer(start: nil, count: 0))
461464
}
462465

463466
for p in stride(from: base,
@@ -472,7 +475,9 @@ public struct Unsafe${Mutable}RawBufferPointer
472475
formIndex(&idx, offsetBy: elementStride)
473476
}
474477

475-
return (it, idx)
478+
return (it, UnsafeMutableBufferPointer(
479+
start: base.assumingMemoryBound(to: S.Iterator.Element.self),
480+
count: idx / elementStride))
476481
}
477482
% end # mutable
478483

test/stdlib/UnsafeRawBufferPointer.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).underflow") {
7979
let buffer = UnsafeMutableRawBufferPointer.allocate(count: 30)
8080
defer { buffer.deallocate() }
8181
let source = stride(from: 5 as Int64, to: 0, by: -1)
82-
var (it,idx) = buffer.initializeMemory(as: Int64.self, from: source)
82+
var (it,bound) = buffer.initializeMemory(as: Int64.self, from: source)
83+
let idx = bound.endIndex * MemoryLayout<Int64>.stride
8384
expectEqual(it.next()!, 2)
8485
expectEqual(idx, 24)
85-
([5, 4, 3] as [Int64]).withUnsafeBytes {
86-
expectEqualSequence($0,buffer[0..<idx])
87-
}
86+
let expected: [Int64] = [5,4,3]
87+
expected.withUnsafeBytes { expectEqualSequence($0,buffer[0..<idx]) }
88+
expectEqualSequence([5, 4, 3],bound)
8889
}
8990

9091
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") {
@@ -94,35 +95,39 @@ UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).overflow") {
9495
if _isDebugAssertConfiguration() {
9596
expectCrashLater()
9697
}
97-
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source)
98+
var (it,bound) = buffer.initializeMemory(as: Int64.self, from: source)
99+
let idx = bound.endIndex * MemoryLayout<Int64>.stride
98100
expectEqual(it.next()!, 2)
99101
expectEqual(idx, 24)
100-
([5, 4, 3] as [Int64]).withUnsafeBytes {
101-
expectEqualSequence($0,buffer[0..<idx])
102-
}
102+
let expected: [Int64] = [5,4,3]
103+
expected.withUnsafeBytes { expectEqualSequence($0,buffer[0..<idx]) }
104+
expectEqualSequence([5, 4, 3],bound)
103105
}
104106

105107
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).exact") {
106108
let buffer = UnsafeMutableRawBufferPointer.allocate(count: 24)
107109
defer { buffer.deallocate() }
108110
let source: [Int64] = [5, 4, 3]
109-
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source)
111+
var (it,bound) = buffer.initializeMemory(as: Int64.self, from: source)
112+
let idx = bound.endIndex * MemoryLayout<Int64>.stride
110113
expectNil(it.next())
111114
expectEqual(idx, buffer.endIndex)
112115
source.withUnsafeBytes { expectEqualSequence($0,buffer) }
116+
expectEqualSequence([5, 4, 3],bound)
113117
}
114118

115119
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).invalidNilPtr") {
116120
let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0)
117121
let source: [Int64] = [5, 4, 3, 2, 1]
118122
expectCrashLater()
119-
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source)
123+
var (it, bound) = buffer.initializeMemory(as: Int64.self, from: source)
120124
}
121125

122126
UnsafeRawBufferPointerTestSuite.test("initializeMemory(as:from:).validNilPtr") {
123127
let buffer = UnsafeMutableRawBufferPointer(start: nil, count: 0)
124128
let source: [Int64] = []
125-
var (it, idx) = buffer.initializeMemory(as: Int64.self, from: source)
129+
var (it, bound) = buffer.initializeMemory(as: Int64.self, from: source)
130+
let idx = bound.endIndex * MemoryLayout<Int64>.stride
126131
expectNil(it.next())
127132
expectEqual(idx, source.endIndex)
128133
}

0 commit comments

Comments
 (0)