Skip to content

Commit 9f4c354

Browse files
author
Russ Bishop
committed
SR-1490: Implement SE-0076
Changes the functions to take immutable UP instead of adding overloads, per comments in bug
1 parent 21413d3 commit 9f4c354

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ public struct ${Self}<Pointee>
207207
///
208208
/// - Precondition: The `Pointee`s at `self..<self + count` and
209209
/// `source..<source + count` are initialized.
210-
public func assignFrom(_ source: ${Self}, count: Int) {
210+
public func assignFrom(_ source: UnsafePointer<Pointee>, count: Int) {
211211
_debugPrecondition(
212212
count >= 0, "${Self}.assignFrom with negative count")
213213
_debugPrecondition(
214-
self < source || self >= source + count,
214+
UnsafePointer(self) < source || UnsafePointer(self) >= source + count,
215215
"assignFrom non-following overlapping range; use assignBackwardFrom")
216216
for i in 0..<count {
217217
self[i] = source[i]
@@ -232,11 +232,11 @@ public struct ${Self}<Pointee>
232232
///
233233
/// - Precondition: The `Pointee`s at `self..<self + count` and
234234
/// `source..<source + count` are initialized.
235-
public func assignBackwardFrom(_ source: ${Self}, count: Int) {
235+
public func assignBackwardFrom(_ source: UnsafePointer<Pointee>, count: Int) {
236236
_debugPrecondition(
237237
count >= 0, "${Self}.assignBackwardFrom with negative count")
238238
_debugPrecondition(
239-
source < self || source >= self + count,
239+
source < UnsafePointer(self) || source >= UnsafePointer(self) + count,
240240
"${Self}.assignBackwardFrom non-preceding overlapping range; use assignFrom instead")
241241
var i = count-1
242242
while i >= 0 {
@@ -324,11 +324,12 @@ public struct ${Self}<Pointee>
324324
///
325325
/// - Postcondition: The `Pointee`s at `self..<self + count` and
326326
/// `source..<source + count` are initialized.
327-
public func initializeFrom(_ source: ${Self}, count: Int) {
327+
public func initializeFrom(_ source: UnsafePointer<Pointee>, count: Int) {
328328
_debugPrecondition(
329329
count >= 0, "${Self}.initializeFrom with negative count")
330330
_debugPrecondition(
331-
self + count <= source || source + count <= self,
331+
UnsafePointer(self) + count <= source ||
332+
source + count <= UnsafePointer(self),
332333
"${Self}.initializeFrom non-following overlapping range")
333334
Builtin.copyArray(
334335
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)

test/1_stdlib/UnsafePointer.swift.gyb

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ class Missile {
188188
}
189189

190190
func checkPointerCorrectness(_ check: Check,
191+
_ withMissiles: Bool = false,
191192
_ f: (UnsafeMutablePointer<Missile>) ->
192-
(UnsafeMutablePointer<Missile>, count: Int) -> Void,
193-
_ withMissiles: Bool = false) {
193+
(UnsafeMutablePointer<Missile>, count: Int) -> Void) {
194194
let ptr = UnsafeMutablePointer<Missile>(allocatingCapacity: 4)
195195
switch check {
196196
case .RightOverlap:
@@ -235,9 +235,23 @@ func checkPointerCorrectness(_ check: Check,
235235
}
236236
}
237237

238-
let checkPtr: (((UnsafeMutablePointer<Missile>) ->
239-
(UnsafeMutablePointer<Missile>, count: Int) -> Void), Bool) -> (Check) -> Void
240-
= { (f, m) in return { checkPointerCorrectness($0, f, m) } }
238+
func checkPtr(
239+
_ f: ((UnsafeMutablePointer<Missile>) -> (UnsafeMutablePointer<Missile>, count: Int) -> Void),
240+
_ m: Bool
241+
) -> (Check) -> Void {
242+
return { checkPointerCorrectness($0, m, f) }
243+
}
244+
245+
func checkPtr(
246+
_ f: ((UnsafeMutablePointer<Missile>) -> (UnsafePointer<Missile>, count: Int) -> Void),
247+
_ m: Bool
248+
) -> (Check) -> Void {
249+
return {
250+
checkPointerCorrectness($0, m) { destPtr in
251+
return { f(destPtr)(UnsafeMutablePointer($0), count: $1) }
252+
}
253+
}
254+
}
241255

242256
UnsafeMutablePointerTestSuite.test("moveInitializeBackwardFrom") {
243257
let check = checkPtr(UnsafeMutablePointer.moveInitializeBackwardFrom, false)
@@ -322,6 +336,41 @@ UnsafeMutablePointerTestSuite.test("initializeFrom.Right") {
322336
}
323337
}
324338

339+
UnsafeMutablePointerTestSuite.test("initializeFrom/immutable") {
340+
var ptr = UnsafeMutablePointer<Missile>(allocatingCapacity: 3)
341+
defer {
342+
ptr.deinitialize(count: 3)
343+
ptr.deallocateCapacity(3)
344+
}
345+
let source = (0..<3).map(Missile.init)
346+
source.withUnsafeBufferPointer { bufferPtr in
347+
ptr.initializeFrom(bufferPtr.baseAddress!, count: 3)
348+
expectEqual(0, ptr[0].number)
349+
expectEqual(1, ptr[1].number)
350+
expectEqual(2, ptr[2].number)
351+
}
352+
}
353+
354+
% for assign in ['assignFrom', 'assignBackwardFrom']:
355+
356+
UnsafeMutablePointerTestSuite.test("${assign}/immutable") {
357+
var ptr = UnsafeMutablePointer<Missile>(allocatingCapacity: 2)
358+
defer {
359+
ptr.deinitialize(count: 2)
360+
ptr.deallocateCapacity(2)
361+
}
362+
ptr.initialize(with: Missile(1))
363+
(ptr + 1).initialize(with: Missile(2))
364+
let source = (3..<5).map(Missile.init)
365+
source.withUnsafeBufferPointer { bufferPtr in
366+
ptr.${assign}(bufferPtr.baseAddress!, count: 2)
367+
expectEqual(3, ptr[0].number)
368+
expectEqual(4, ptr[1].number)
369+
}
370+
}
371+
372+
% end
373+
325374
UnsafePointerTestSuite.test("customMirror") {
326375
// Ensure that the custom mirror works properly, including when the raw value
327376
// is greater than Int.max

0 commit comments

Comments
 (0)