Skip to content

Commit 73106dd

Browse files
authored
Rename initialize(with:count:) to initialize(to:count:). (#3601)
As proposed in SE-0107: UnsafeRawPointer. "with" is considered a vacuous preposition. "to" implies direction.
1 parent d82483b commit 73106dd

34 files changed

+94
-88
lines changed

stdlib/private/StdlibUnittest/OpaqueIdentityFunctions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func _stdlib_getPointer(_ x: OpaquePointer) -> OpaquePointer
1515

1616
public func _opaqueIdentity<T>(_ x: T) -> T {
1717
let ptr = UnsafeMutablePointer<T>(allocatingCapacity: 1)
18-
ptr.initialize(with: x)
18+
ptr.initialize(to: x)
1919
let result =
2020
UnsafeMutablePointer<T>(_stdlib_getPointer(OpaquePointer(ptr))).pointee
2121
ptr.deinitialize()

stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct _stdlib_ShardedAtomicCounter {
3535
let count = max(8, hardwareConcurrency * hardwareConcurrency)
3636
let shards = UnsafeMutablePointer<Int>(allocatingCapacity: count)
3737
for i in 0..<count {
38-
(shards + i).initialize(with: 0)
38+
(shards + i).initialize(to: 0)
3939
}
4040
self._shardsPtr = shards
4141
self._shardsCount = count

stdlib/private/SwiftPrivatePthreadExtras/SwiftPrivatePthreadExtras.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal class PthreadBlockContextImpl<Argument, Result>: PthreadBlockContext {
4242

4343
override func run() -> UnsafeMutablePointer<Void> {
4444
let result = UnsafeMutablePointer<Result>(allocatingCapacity: 1)
45-
result.initialize(with: block(arg))
45+
result.initialize(to: block(arg))
4646
return UnsafeMutablePointer(result)
4747
}
4848
}

stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public func reflect(object: AnyObject) {
357357
public func reflect<T>(any: T) {
358358
let any: Any = any
359359
let anyPointer = UnsafeMutablePointer<Any>(allocatingCapacity: sizeof(Any.self))
360-
anyPointer.initialize(with: any)
360+
anyPointer.initialize(to: any)
361361
let anyPointerValue = unsafeBitCast(anyPointer, to: UInt.self)
362362
reflect(instanceAddress: anyPointerValue, kind: .Existential)
363363
anyPointer.deallocateCapacity(sizeof(Any.self))
@@ -421,7 +421,7 @@ struct ThickFunctionParts {
421421
public func reflect(function: () -> ()) {
422422
let fn = UnsafeMutablePointer<ThickFunction0>(
423423
allocatingCapacity: sizeof(ThickFunction0.self))
424-
fn.initialize(with: ThickFunction0(function: function))
424+
fn.initialize(to: ThickFunction0(function: function))
425425

426426
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
427427
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
@@ -436,7 +436,7 @@ public func reflect(function: () -> ()) {
436436
public func reflect(function: (Int) -> ()) {
437437
let fn = UnsafeMutablePointer<ThickFunction1>(
438438
allocatingCapacity: sizeof(ThickFunction1.self))
439-
fn.initialize(with: ThickFunction1(function: function))
439+
fn.initialize(to: ThickFunction1(function: function))
440440

441441
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
442442
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
@@ -451,7 +451,7 @@ public func reflect(function: (Int) -> ()) {
451451
public func reflect(function: (Int, String) -> ()) {
452452
let fn = UnsafeMutablePointer<ThickFunction2>(
453453
allocatingCapacity: sizeof(ThickFunction2.self))
454-
fn.initialize(with: ThickFunction2(function: function))
454+
fn.initialize(to: ThickFunction2(function: function))
455455

456456
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
457457
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
@@ -466,7 +466,7 @@ public func reflect(function: (Int, String) -> ()) {
466466
public func reflect(function: (Int, String, AnyObject?) -> ()) {
467467
let fn = UnsafeMutablePointer<ThickFunction3>(
468468
allocatingCapacity: sizeof(ThickFunction3.self))
469-
fn.initialize(with: ThickFunction3(function: function))
469+
fn.initialize(to: ThickFunction3(function: function))
470470

471471
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
472472
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)

stdlib/public/SDK/Foundation/NSError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public func _stdlib_bridgeNSErrorToError<
282282
T : _ObjectiveCBridgeableError
283283
>(_ error: NSError, out: UnsafeMutablePointer<T>) -> Bool {
284284
if let bridged = T(_bridgedNSError: error) {
285-
out.initialize(with: bridged)
285+
out.initialize(to: bridged)
286286
return true
287287
} else {
288288
return false

stdlib/public/core/ArrayBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ extension _ArrayBuffer {
217217
// Make another pass to retain the copied objects
218218
var result = target
219219
for _ in CountableRange(bounds) {
220-
result.initialize(with: result.pointee)
220+
result.initialize(to: result.pointee)
221221
result += 1
222222
}
223223
return result

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ extension _ArrayBufferProtocol where Index == Int {
163163
}
164164
// Initialize the hole left by sliding the tail forward
165165
for j in oldTailIndex..<newTailIndex {
166-
(elements + j).initialize(with: newValues[i])
166+
(elements + j).initialize(to: newValues[i])
167167
newValues.formIndex(after: &i)
168168
}
169169
_expectEnd(i, newValues)

stdlib/public/core/Arrays.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
10611061
var p: UnsafeMutablePointer<Element>
10621062
(self, p) = ${Self}._allocateUninitialized(count)
10631063
for _ in 0..<count {
1064-
p.initialize(with: repeatedValue)
1064+
p.initialize(to: repeatedValue)
10651065
p += 1
10661066
}
10671067
}
@@ -1252,7 +1252,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
12521252
_sanityCheck(_buffer.capacity >= _buffer.count + 1)
12531253

12541254
_buffer.count = oldCount + 1
1255-
(_buffer.firstElementAddress + oldCount).initialize(with: newElement)
1255+
(_buffer.firstElementAddress + oldCount).initialize(to: newElement)
12561256
}
12571257

12581258
/// Adds a new element at the end of the array.
@@ -1616,7 +1616,7 @@ internal struct _InitializeMemoryFromCollection<
16161616
var p = rawMemory
16171617
var q = newValues.startIndex
16181618
for _ in 0..<count {
1619-
p.initialize(with: newValues[q])
1619+
p.initialize(to: newValues[q])
16201620
newValues.formIndex(after: &q)
16211621
p += 1
16221622
}
@@ -1923,7 +1923,7 @@ internal struct _InitializePointer<T> : _PointerFunction {
19231923
internal func call(_ rawMemory: UnsafeMutablePointer<T>, count: Int) {
19241924
_sanityCheck(count == 1)
19251925
// FIXME: it would be better if we could find a way to move, here
1926-
rawMemory.initialize(with: newValue)
1926+
rawMemory.initialize(to: newValue)
19271927
}
19281928

19291929
@_transparent
@@ -2003,7 +2003,7 @@ internal func _arrayAppendSequence<Buffer, S>(
20032003
let base = buffer.firstElementAddress
20042004

20052005
while (nextItem != nil) && count < capacity {
2006-
(base + count).initialize(with: nextItem!)
2006+
(base + count).initialize(to: nextItem!)
20072007
count += 1
20082008
nextItem = stream.next()
20092009
}

stdlib/public/core/Collection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ extension Sequence
16801680
} else {
16811681
var p = ptr
16821682
for x in self {
1683-
p.initialize(with: x)
1683+
p.initialize(to: x)
16841684
p += 1
16851685
}
16861686
return p

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
145145
let resultPtr = result.baseAddress
146146
let p = __manager._elementPointer
147147
for i in 0..<count {
148-
(resultPtr + i).initialize(with: _bridgeToObjectiveCUnconditional(p[i]))
148+
(resultPtr + i).initialize(to: _bridgeToObjectiveCUnconditional(p[i]))
149149
}
150150
_fixLifetime(__manager)
151151
return result
@@ -239,7 +239,7 @@ struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
239239
let verbatim = false
240240
#endif
241241

242-
__bufferPointer._headerPointer.initialize(with:
242+
__bufferPointer._headerPointer.initialize(to:
243243
_ArrayBody(
244244
count: count,
245245
capacity: capacity,
@@ -608,7 +608,7 @@ internal func _copyCollectionToContiguousArray<
608608
var i = source.startIndex
609609
for _ in 0..<count {
610610
// FIXME(performance): use _copyContents(initializing:).
611-
p.initialize(with: source[i])
611+
p.initialize(to: source[i])
612612
source.formIndex(after: &i)
613613
p += 1
614614
}
@@ -667,7 +667,7 @@ internal struct _UnsafePartiallyInitializedContiguousArrayBuffer<Element> {
667667
"_UnsafePartiallyInitializedContiguousArrayBuffer has no more capacity")
668668
remainingCapacity -= 1
669669

670-
p.initialize(with: element)
670+
p.initialize(to: element)
671671
p += 1
672672
}
673673

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,15 +2719,15 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
27192719
internal func initializeKey(_ k: Key, at i: Int) {
27202720
_sanityCheck(!isInitializedEntry(at: i))
27212721

2722-
(keys + i).initialize(with: k)
2722+
(keys + i).initialize(to: k)
27232723
initializedEntries[i] = true
27242724
_fixLifetime(self)
27252725
}
27262726

27272727
@_transparent
27282728
internal func moveInitializeEntry(from: Storage, at: Int, toEntryAt: Int) {
27292729
_sanityCheck(!isInitializedEntry(at: toEntryAt))
2730-
(keys + toEntryAt).initialize(with: (from.keys + at).move())
2730+
(keys + toEntryAt).initialize(to: (from.keys + at).move())
27312731
from.initializedEntries[at] = false
27322732
initializedEntries[toEntryAt] = true
27332733
}
@@ -2745,17 +2745,17 @@ struct _Native${Self}Storage<${TypeParametersDecl}> :
27452745
internal func initializeKey(_ k: Key, value v: Value, at i: Int) {
27462746
_sanityCheck(!isInitializedEntry(at: i))
27472747

2748-
(keys + i).initialize(with: k)
2749-
(values + i).initialize(with: v)
2748+
(keys + i).initialize(to: k)
2749+
(values + i).initialize(to: v)
27502750
initializedEntries[i] = true
27512751
_fixLifetime(self)
27522752
}
27532753

27542754
@_transparent
27552755
internal func moveInitializeEntry(from: Storage, at: Int, toEntryAt: Int) {
27562756
_sanityCheck(!isInitializedEntry(at: toEntryAt))
2757-
(keys + toEntryAt).initialize(with: (from.keys + at).move())
2758-
(values + toEntryAt).initialize(with: (from.values + at).move())
2757+
(keys + toEntryAt).initialize(to: (from.keys + at).move())
2758+
(values + toEntryAt).initialize(to: (from.values + at).move())
27592759
from.initializedEntries[at] = false
27602760
initializedEntries[toEntryAt] = true
27612761
}
@@ -3093,7 +3093,7 @@ internal struct _BridgedNative${Self}Storage {
30933093
internal func initializeKey(_ k: AnyObject, at i: Int) {
30943094
_sanityCheck(!isInitializedEntry(at: i))
30953095

3096-
(keys + i).initialize(with: k)
3096+
(keys + i).initialize(to: k)
30973097
initializedEntries[i] = true
30983098
_fixLifetime(self)
30993099
}
@@ -3103,8 +3103,8 @@ internal struct _BridgedNative${Self}Storage {
31033103
) {
31043104
_sanityCheck(!isInitializedEntry(at: i))
31053105

3106-
(keys + i).initialize(with: k)
3107-
(values + i).initialize(with: v)
3106+
(keys + i).initialize(to: k)
3107+
(values + i).initialize(to: v)
31083108
initializedEntries[i] = true
31093109
_fixLifetime(self)
31103110
}

stdlib/public/core/HeapBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ struct _HeapBuffer<Value, Element> : Equatable {
172172
size: totalSize,
173173
alignmentMask: alignMask)
174174
self._storage = Builtin.castToNativeObject(object)
175-
self._value.initialize(with: initializer)
175+
self._value.initialize(to: initializer)
176176
}
177177

178178
public // @testable

stdlib/public/core/ManagedBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
195195

196196
// initialize the header field
197197
try withUnsafeMutablePointerToHeader {
198-
$0.initialize(with:
198+
$0.initialize(to:
199199
try initialHeader(
200200
buffer: self.buffer,
201201
capacity: {

stdlib/public/core/Reflection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public protocol _Mirror {
130130
@_silgen_name("swift_getSummary")
131131
public // COMPILER_INTRINSIC
132132
func _getSummary<T>(_ out: UnsafeMutablePointer<String>, x: T) {
133-
out.initialize(with: String(reflecting: x))
133+
out.initialize(to: String(reflecting: x))
134134
}
135135

136136
/// Produce a mirror for any value. The runtime produces a mirror that

stdlib/public/core/Sequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ extension Sequence {
11781178
) -> UnsafeMutablePointer<Iterator.Element> {
11791179
var p = UnsafeMutablePointer<Iterator.Element>(ptr)
11801180
for x in IteratorSequence(self.makeIterator()) {
1181-
p.initialize(with: x)
1181+
p.initialize(to: x)
11821182
p += 1
11831183
}
11841184
return p

stdlib/public/core/String.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ extension String {
744744
start: UnsafeMutablePointer<UTF8.CodeUnit>,
745745
utf8CodeUnitCount: Int
746746
) {
747-
resultStorage.initialize(with:
747+
resultStorage.initialize(to:
748748
String._fromWellFormedCodeUnitSequence(
749749
UTF8.self,
750750
input: UnsafeBufferPointer(start: start, count: utf8CodeUnitCount)))

stdlib/public/core/UnsafeBitMap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct _UnsafeBitMap {
5151

5252
public // @testable
5353
func initializeToZero() {
54-
values.initialize(with: 0, count: numberOfWords)
54+
values.initialize(to: 0, count: numberOfWords)
5555
}
5656

5757
public // @testable

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ public struct ${Self}<Pointee>
172172
///
173173
/// - Postcondition: The pointee is initialized; the value should eventually
174174
/// be destroyed or moved from to avoid leaks.
175-
public func initialize(with newValue: Pointee, count: Int = 1) {
175+
public func initialize(to newValue: Pointee, count: Int = 1) {
176176
// FIXME: add tests (since the `count` has been added)
177177
_debugPrecondition(count >= 0,
178-
"${Self}.initialize(with:): negative count")
178+
"${Self}.initialize(to:): negative count")
179179
// Must not use `initializeFrom` with a `Collection` as that will introduce
180180
// a cycle.
181181
for offset in 0..<count {
@@ -244,7 +244,7 @@ public struct ${Self}<Pointee>
244244
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)
245245
// This builtin is equivalent to:
246246
// for i in 0..<count {
247-
// (self + i).initialize(with: (source + i).move())
247+
// (self + i).initialize(to: (source + i).move())
248248
// }
249249
}
250250
else {
@@ -255,7 +255,7 @@ public struct ${Self}<Pointee>
255255
// var src = source + count
256256
// var dst = self + count
257257
// while dst != self {
258-
// (--dst).initialize(with: (--src).move())
258+
// (--dst).initialize(to: (--src).move())
259259
// }
260260
}
261261
}
@@ -286,7 +286,7 @@ public struct ${Self}<Pointee>
286286
Pointee.self, self._rawValue, source._rawValue, count._builtinWordValue)
287287
// This builtin is equivalent to:
288288
// for i in 0..<count {
289-
// (self + i).initialize(with: source[i])
289+
// (self + i).initialize(to: source[i])
290290
// }
291291
}
292292
@@ -511,11 +511,16 @@ extension ${Self} {
511511
}
512512

513513
% if mutable:
514-
@available(*, unavailable, renamed: "initialize(with:)")
514+
@available(*, unavailable, renamed: "initialize(to:)")
515515
public func initialize(_ newvalue: Pointee) {
516516
Builtin.unreachable()
517517
}
518518

519+
@available(*, unavailable, renamed: "initialize(to:count:)")
520+
public func initialize(with newvalue: Pointee, count: Int = 1) {
521+
Builtin.unreachable()
522+
}
523+
519524
@available(*, unavailable, renamed: "deinitialize(count:)")
520525
public func destroy() {
521526
Builtin.unreachable()

test/1_stdlib/Builtins.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ struct ContainsP { var p: P }
130130
func exerciseArrayValueWitnesses<T>(_ value: T) {
131131
let buf = UnsafeMutablePointer<T>(allocatingCapacity: 5)
132132

133-
(buf + 0).initialize(with: value)
134-
(buf + 1).initialize(with: value)
133+
(buf + 0).initialize(to: value)
134+
(buf + 1).initialize(to: value)
135135

136136
Builtin.copyArray(T.self, (buf + 2)._rawValue, buf._rawValue, 2._builtinWordValue)
137137
Builtin.takeArrayBackToFront(T.self, (buf + 1)._rawValue, buf._rawValue, 4._builtinWordValue)

test/1_stdlib/HeapBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ a.value.name = "DaveA"
2020
a.value.locations.append("Princeton")
2121
a.value.locations.append("San Jose")
2222
for x in 0..<10 {
23-
(a.baseAddress + x).initialize(with: x)
23+
(a.baseAddress + x).initialize(to: x)
2424
}
2525

2626
print("buffer has storage: \(a.storage != nil)")

test/1_stdlib/ManagedBuffer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ final class TestManagedBuffer<T> : ManagedBuffer<CountAndCapacity, T> {
109109

110110
withUnsafeMutablePointerToElements {
111111
(p: UnsafeMutablePointer<T>) -> () in
112-
(p + count).initialize(with: x)
112+
(p + count).initialize(to: x)
113113
}
114114
self.count = count + 2
115115
}

test/1_stdlib/Reflection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ dump(randomUnsafeMutablePointerString)
182182

183183
// CHECK-NEXT: Hello panda
184184
var sanePointerString = UnsafeMutablePointer<String>(allocatingCapacity: 1)
185-
sanePointerString.initialize(with: "Hello panda")
185+
sanePointerString.initialize(to: "Hello panda")
186186
dump(sanePointerString.pointee)
187187
sanePointerString.deinitialize()
188188
sanePointerString.deallocateCapacity(1)

0 commit comments

Comments
 (0)