Skip to content

Commit dc16444

Browse files
authored
Remove _specializingCast from Foundation (#1938)
1 parent 3ab2e47 commit dc16444

9 files changed

Lines changed: 37 additions & 43 deletions

Sources/FoundationEssentials/AttributedString/AttributedString+CharacterView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ extension AttributedString.CharacterView: RangeReplaceableCollection {
314314
let subrange = _guts.characterRange(roundingDown: subrange._bstringRange)
315315

316316
// Prevent the BigString mutation below from falling back to Character-by-Character loops.
317-
if let newElements = _specializingCast(newElements, to: Self.self) {
317+
if let newElements = _specialize(newElements, for: Self.self) {
318318
_replaceSubrange(subrange, with: newElements._characters)
319-
} else if let newElements = _specializingCast(newElements, to: Slice<Self>.self) {
319+
} else if let newElements = _specialize(newElements, for: Slice<Self>.self) {
320320
_replaceSubrange(subrange, with: newElements._rebased._characters)
321321
} else {
322322
_replaceSubrange(subrange, with: newElements)
@@ -333,7 +333,7 @@ extension AttributedString.CharacterView: RangeReplaceableCollection {
333333
// don't need to touch string storage, but we still want to update attributes as if it was
334334
// a full edit.
335335
var hasStringChanges = true
336-
if let newElements = _specializingCast(newElements, to: BigSubstring.self),
336+
if let newElements = _specialize(newElements, for: BigSubstring.self),
337337
newElements.isIdentical(to: _characters[subrange]) {
338338
hasStringChanges = false
339339
}

Sources/FoundationEssentials/AttributedString/AttributedString+UnicodeScalarView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,9 @@ extension AttributedString.UnicodeScalarView: RangeReplaceableCollection {
297297
let subrange = _guts.unicodeScalarRange(roundingDown: subrange._bstringRange)
298298

299299
// Prevent the BigString mutation below from falling back to Character-by-Character loops.
300-
if let newElements = _specializingCast(newElements, to: Self.self) {
300+
if let newElements = _specialize(newElements, for: Self.self) {
301301
_replaceSubrange(subrange, with: newElements._unicodeScalars)
302-
} else if let newElements = _specializingCast(newElements, to: Slice<Self>.self) {
302+
} else if let newElements = _specialize(newElements, for: Slice<Self>.self) {
303303
_replaceSubrange(subrange, with: newElements._rebased._unicodeScalars)
304304
} else {
305305
_replaceSubrange(subrange, with: newElements)
@@ -316,7 +316,7 @@ extension AttributedString.UnicodeScalarView: RangeReplaceableCollection {
316316
// don't need to touch string storage, but we still want to update attributes as if it was
317317
// a full edit.
318318
var hasStringChanges = true
319-
if let newElements = _specializingCast(newElements, to: BigSubstring.UnicodeScalarView.self),
319+
if let newElements = _specialize(newElements, for: BigSubstring.UnicodeScalarView.self),
320320
newElements.isIdentical(to: _unicodeScalars[subrange]) {
321321
hasStringChanges = false
322322
}

Sources/FoundationEssentials/AttributedString/AttributedString.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ extension AttributedString {
143143
}
144144

145145
internal init(_ s: some AttributedStringProtocol) {
146-
if let s = _specializingCast(s, to: AttributedString.self) {
146+
if let s = _specialize(s, for: AttributedString.self) {
147147
self = s
148-
} else if let s = _specializingCast(s, to: AttributedSubstring.self) {
148+
} else if let s = _specialize(s, for: AttributedSubstring.self) {
149149
self = AttributedString(s)
150150
} else {
151151
// !!!: We don't expect or want this to happen.
@@ -251,17 +251,17 @@ extension AttributedString {
251251
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
252252
extension AttributedString {
253253
internal static func _bstring<S: Sequence<Character>>(from elements: S) -> BigString {
254-
if let elements = _specializingCast(elements, to: String.self) {
254+
if let elements = _specialize(elements, for: String.self) {
255255
return BigString(elements)
256256
}
257-
if let elements = _specializingCast(elements, to: Substring.self) {
257+
if let elements = _specialize(elements, for: Substring.self) {
258258
return BigString(elements)
259259
}
260-
if let elements = _specializingCast(elements, to: AttributedString.CharacterView.self) {
260+
if let elements = _specialize(elements, for: AttributedString.CharacterView.self) {
261261
return BigString(elements._characters)
262262
}
263-
if let elements = _specializingCast(
264-
elements, to: Slice<AttributedString.CharacterView>.self
263+
if let elements = _specialize(
264+
elements, for: Slice<AttributedString.CharacterView>.self
265265
) {
266266
return BigString(elements._characters)
267267
}

Sources/FoundationEssentials/AttributedString/AttributedStringProtocol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ extension AttributedStringProtocol {
162162
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
163163
extension AttributedStringProtocol {
164164
internal var __guts: AttributedString.Guts {
165-
if let s = _specializingCast(self, to: AttributedString.self) {
165+
if let s = _specialize(self, for: AttributedString.self) {
166166
return s._guts
167-
} else if let s = _specializingCast(self, to: AttributedSubstring.self) {
167+
} else if let s = _specialize(self, for: AttributedSubstring.self) {
168168
return s._guts
169169
} else {
170170
return self.characters._guts

Sources/FoundationEssentials/AttributedString/Collection Stdlib Defaults.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,3 @@ extension BidirectionalCollection {
113113
return i
114114
}
115115
}
116-
117-
@inline(__always)
118-
internal func _specializingCast<Input, Output>(_ value: Input, to type: Output.Type) -> Output? {
119-
guard Input.self == Output.self else { return nil }
120-
return _identityCast(value, to: type)
121-
}

Sources/FoundationEssentials/JSON/JSONEncoder.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,35 +1293,35 @@ private extension __JSONEncoder {
12931293
}
12941294

12951295
func _asDirectArrayEncodable<T: Encodable>(_ value: T) -> _JSONDirectArrayEncodable? {
1296-
return if let array = _specializingCast(value, to: [Int8].self) {
1296+
return if let array = _specialize(value, for: [Int8].self) {
12971297
array
1298-
} else if let array = _specializingCast(value, to: [Int16].self) {
1298+
} else if let array = _specialize(value, for: [Int16].self) {
12991299
array
1300-
} else if let array = _specializingCast(value, to: [Int32].self) {
1300+
} else if let array = _specialize(value, for: [Int32].self) {
13011301
array
1302-
} else if let array = _specializingCast(value, to: [Int64].self) {
1302+
} else if let array = _specialize(value, for: [Int64].self) {
13031303
array
1304-
} else if let array = _specializingCast(value, to: [Int128].self) {
1304+
} else if let array = _specialize(value, for: [Int128].self) {
13051305
array
1306-
} else if let array = _specializingCast(value, to: [Int].self) {
1306+
} else if let array = _specialize(value, for: [Int].self) {
13071307
array
1308-
} else if let array = _specializingCast(value, to: [UInt8].self) {
1308+
} else if let array = _specialize(value, for: [UInt8].self) {
13091309
array
1310-
} else if let array = _specializingCast(value, to: [UInt16].self) {
1310+
} else if let array = _specialize(value, for: [UInt16].self) {
13111311
array
1312-
} else if let array = _specializingCast(value, to: [UInt32].self) {
1312+
} else if let array = _specialize(value, for: [UInt32].self) {
13131313
array
1314-
} else if let array = _specializingCast(value, to: [UInt64].self) {
1314+
} else if let array = _specialize(value, for: [UInt64].self) {
13151315
array
1316-
} else if let array = _specializingCast(value, to: [UInt128].self) {
1316+
} else if let array = _specialize(value, for: [UInt128].self) {
13171317
array
1318-
} else if let array = _specializingCast(value, to: [UInt].self) {
1318+
} else if let array = _specialize(value, for: [UInt].self) {
13191319
array
1320-
} else if let array = _specializingCast(value, to: [String].self) {
1320+
} else if let array = _specialize(value, for: [String].self) {
13211321
array
1322-
} else if let array = _specializingCast(value, to: [Float].self) {
1322+
} else if let array = _specialize(value, for: [Float].self) {
13231323
array
1324-
} else if let array = _specializingCast(value, to: [Double].self) {
1324+
} else if let array = _specialize(value, for: [Double].self) {
13251325
array
13261326
} else {
13271327
nil

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension StringProtocol {
3333
// Standardize the path to use forward slashes before processing for consistency
3434
return self.replacing(._backslash, with: ._slash)
3535
#else
36-
if let str = _specializingCast(self, to: String.self) {
36+
if let str = _specialize(self, for: String.self) {
3737
return str
3838
} else {
3939
return String(self)

Sources/FoundationEssentials/String/StringProtocol+Essentials.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,10 @@ extension StringProtocol {
415415
// `Substring`. Note that we're only ever calling `_lineBounds` on a `Substring`; this is
416416
// to reduce the code size overhead of having to specialize it multiple times (at a slight
417417
// cost to runtime performance).
418-
if let s = _specializingCast(self, to: String.self) {
418+
if let s = _specialize(self, for: String.self) {
419419
let range = s.unicodeScalars._boundaryAlignedRange(range)
420420
return s[...].utf8._lineBounds(around: range)
421-
} else if let s = _specializingCast(self, to: Substring.self) {
421+
} else if let s = _specialize(self, for: Substring.self) {
422422
let range = s.unicodeScalars._boundaryAlignedRange(range)
423423
return s.utf8._lineBounds(around: range)
424424
} else {
@@ -454,10 +454,10 @@ extension StringProtocol {
454454
// `Substring`. Note that we're only ever calling `_paragraphBounds` on a `Substring`; this is
455455
// to reduce the code size overhead of having to specialize it multiple times (at a slight
456456
// cost to runtime performance).
457-
if let s = _specializingCast(self, to: String.self) {
457+
if let s = _specialize(self, for: String.self) {
458458
let range = s.unicodeScalars._boundaryAlignedRange(range)
459459
return s[...].utf8._paragraphBounds(around: range) // Note: We use [...] to get a Substring
460-
} else if let s = _specializingCast(self, to: Substring.self) {
460+
} else if let s = _specialize(self, for: Substring.self) {
461461
let range = s.unicodeScalars._boundaryAlignedRange(range)
462462
return s.utf8._paragraphBounds(around: range)
463463
} else {

Sources/FoundationEssentials/URL/URLEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ extension StringProtocol {
758758
}
759759
#endif
760760

761-
let string = _specializingCast(self, to: String.self) ?? String(self)
761+
let string = _specialize(self, for: String.self) ?? String(self)
762762
if allowedCharacters.hasIdenticalSwiftStorage(to: .urlPathAllowed) {
763763
return URLEncoder.percentEncode(path: string)
764764
} else if allowedCharacters.hasIdenticalSwiftStorage(to: .urlHostAllowed) {
@@ -788,7 +788,7 @@ extension StringProtocol {
788788
}
789789
#endif
790790
return URLEncoder.percentDecode(
791-
string: _specializingCast(self, to: String.self) ?? String(self)
791+
string: _specialize(self, for: String.self) ?? String(self)
792792
)
793793
}
794794
}

0 commit comments

Comments
 (0)