Merge pull request #472 from lorentey/specialize

Reimplement `_specialize(_:for:)` for the 5.9 stdlib
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2025-05-15 14:10:06 -07:00
commit b0020457d5
3 files changed
+90

No files matched your search

@@ -24,6 +24,7 @@ target_sources(${module_name} PRIVATE
"autogenerated/Debugging.swift"
"autogenerated/Descriptions.swift"
"autogenerated/RandomAccessCollection+Offsets.swift"
"autogenerated/Specialize.swift"
"autogenerated/UnsafeBufferPointer+Extras.swift"
"autogenerated/UnsafeMutableBufferPointer+Extras.swift"
"IntegerTricks/autogenerated/FixedWidthInteger+roundUpToPowerOfTwo.swift"
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2023 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import *
}%
${autogenerated_warning()}
#if compiler(<5.10)
% for modifier in visibility_levels:
${visibility_boilerplate(modifier)}
/// Returns `x` as its concrete type `U`, or `nil` if `x` has a different
/// concrete type.
///
/// This cast can be useful for dispatching to specializations of generic
/// functions.
@_alwaysEmitIntoClient
@_transparent
${modifier} func _specialize<T, U>(_ x: T, for: U.Type) -> U? {
guard T.self == U.self else {
return nil
}
return unsafeBitCast(x, to: U.self)
}
% end
${visibility_boilerplate("end")}
#endif
@@ -0,0 +1,54 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2023 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
#if compiler(<5.10)
// In single module mode, we need these declarations to be internal,
// but in regular builds we want them to be public. Unfortunately
// the current best way to do this is to duplicate all definitions.
#if COLLECTIONS_SINGLE_MODULE
/// Returns `x` as its concrete type `U`, or `nil` if `x` has a different
/// concrete type.
///
/// This cast can be useful for dispatching to specializations of generic
/// functions.
@_alwaysEmitIntoClient
@_transparent
internal func _specialize<T, U>(_ x: T, for: U.Type) -> U? {
guard T.self == U.self else {
return nil
}
return unsafeBitCast(x, to: U.self)
}
#else // !COLLECTIONS_SINGLE_MODULE
/// Returns `x` as its concrete type `U`, or `nil` if `x` has a different
/// concrete type.
///
/// This cast can be useful for dispatching to specializations of generic
/// functions.
@_alwaysEmitIntoClient
@_transparent
public func _specialize<T, U>(_ x: T, for: U.Type) -> U? {
guard T.self == U.self else {
return nil
}
return unsafeBitCast(x, to: U.self)
}
#endif // COLLECTIONS_SINGLE_MODULE
#endif