Skip to content

Commit fe76f6b

Browse files
author
Dave Abrahams
committed
[stdlib] Add and use _identityCast
1 parent c117679 commit fe76f6b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

stdlib/public/core/ASCII.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ extension Unicode.ASCII : Unicode.Encoding {
4949
_ content: FromEncoding.EncodedScalar, from _: FromEncoding.Type
5050
) -> EncodedScalar? {
5151
if _fastPath(FromEncoding.self == UTF16.self) {
52-
let c = unsafeBitCast(content, to: UTF16.EncodedScalar.self)
52+
let c = _identityCast(content, to: UTF16.EncodedScalar.self)
5353
guard (c._storage & 0xFF80 == 0) else { return nil }
5454
return EncodedScalar(CodeUnit(c._storage & 0x7f))
5555
}
5656
else if _fastPath(FromEncoding.self == UTF8.self) {
57-
let c = unsafeBitCast(content, to: UTF8.EncodedScalar.self)
57+
let c = _identityCast(content, to: UTF8.EncodedScalar.self)
5858
guard (c._storage & 0x80 == 0) else { return nil }
5959
return EncodedScalar(CodeUnit(c._storage & 0x7f))
6060
}

stdlib/public/core/Builtin.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public func unsafeBitCast<T, U>(_ x: T, to type: U.Type) -> U {
114114
return Builtin.reinterpretCast(x)
115115
}
116116

117+
/// Returns `x` as its concrete type `U`.
118+
///
119+
/// This cast can be useful for dispatching to specializations of generic
120+
/// functions.
121+
///
122+
/// - Requires: `x` has type `U`.
123+
@_transparent
124+
public func _identityCast<T, U>(_ x: T, to expectedType: U.Type) -> U {
125+
_precondition(T.self == expectedType, "_identityCast to wrong type")
126+
return Builtin.reinterpretCast(x)
127+
}
128+
117129
/// `unsafeBitCast` something to `AnyObject`.
118130
@_transparent
119131
internal func _reinterpretCastToAnyObject<T>(_ x: T) -> AnyObject {

0 commit comments

Comments
 (0)