Skip to content

Commit 2b18f4d

Browse files
authored
Merge pull request #1678 from apple/error-bridging-fixed-width-integer
Use FixedWidthInteger rather than BinaryInteger in NSError interoperability
2 parents 7224b35 + 023dc03 commit 2b18f4d

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

Foundation/NSError.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ public extension CustomNSError {
291291
}
292292
}
293293

294-
/// Convert an arbitrary binary integer to an Int, reinterpreting signed ->
295-
/// unsigned if needed but trapping if the result is otherwise not
296-
/// expressible.
297-
func unsafeBinaryIntegerToInt<T: BinaryInteger>(_ value: T) -> Int {
294+
/// Convert an arbitrary fixed-width integer to an Int, reinterpreting
295+
/// signed -> unsigned if needed but trapping if the result is otherwise
296+
/// not expressible.
297+
func unsafeFixedWidthIntegerToInt<T: FixedWidthInteger>(_ value: T) -> Int {
298298
if T.isSigned {
299299
return numericCast(value)
300300
}
@@ -303,10 +303,10 @@ func unsafeBinaryIntegerToInt<T: BinaryInteger>(_ value: T) -> Int {
303303
return Int(bitPattern: uintValue)
304304
}
305305

306-
/// Convert from an Int to an arbitrary binary integer, reinterpreting signed
307-
/// -> unsigned if needed but trapping if the result is otherwise not
306+
/// Convert from an Int to an arbitrary fixed-width integer, reinterpreting
307+
/// signed -> unsigned if needed but trapping if the result is otherwise not
308308
/// expressible.
309-
func unsafeBinaryIntegerFromInt<T: BinaryInteger>(_ value: Int) -> T {
309+
func unsafeFixedWidthIntegerFromInt<T: FixedWidthInteger>(_ value: Int) -> T {
310310
if T.isSigned {
311311
return numericCast(value)
312312
}
@@ -315,10 +315,10 @@ func unsafeBinaryIntegerFromInt<T: BinaryInteger>(_ value: Int) -> T {
315315
return numericCast(uintValue)
316316
}
317317

318-
extension CustomNSError where Self: RawRepresentable, Self.RawValue: BinaryInteger {
318+
extension CustomNSError where Self: RawRepresentable, Self.RawValue: FixedWidthInteger {
319319
// The error code of Error with integral raw values is the raw value.
320320
public var errorCode: Int {
321-
return unsafeBinaryIntegerToInt(self.rawValue)
321+
return unsafeFixedWidthIntegerToInt(self.rawValue)
322322
}
323323
}
324324

@@ -330,7 +330,7 @@ public extension Error where Self : CustomNSError {
330330
var _code: Int { return self.errorCode }
331331
}
332332

333-
public extension Error where Self: CustomNSError, Self: RawRepresentable, Self.RawValue: BinaryInteger {
333+
public extension Error where Self: CustomNSError, Self: RawRepresentable, Self.RawValue: FixedWidthInteger {
334334
/// Default implementation for customized NSErrors.
335335
var _code: Int { return self.errorCode }
336336
}
@@ -427,13 +427,13 @@ public protocol __BridgedNSError : Error {
427427
}
428428

429429
// Allow two bridged NSError types to be compared.
430-
extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: BinaryInteger {
430+
extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: FixedWidthInteger {
431431
public static func ==(lhs: Self, rhs: Self) -> Bool {
432432
return lhs.rawValue == rhs.rawValue
433433
}
434434
}
435435

436-
extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: BinaryInteger {
436+
extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: FixedWidthInteger {
437437
public var _domain: String { return Self._nsErrorDomain }
438438
public var _code: Int {
439439
return Int(rawValue)
@@ -485,16 +485,16 @@ public protocol _BridgedStoredNSError : __BridgedNSError, _ObjectiveCBridgeableE
485485
}
486486

487487
/// Various helper implementations for _BridgedStoredNSError
488-
extension _BridgedStoredNSError where Code: RawRepresentable, Code.RawValue: BinaryInteger {
488+
extension _BridgedStoredNSError where Code: RawRepresentable, Code.RawValue: FixedWidthInteger {
489489
public var code: Code {
490-
return Code(rawValue: unsafeBinaryIntegerFromInt(_nsError.code))!
490+
return Code(rawValue: unsafeFixedWidthIntegerFromInt(_nsError.code))!
491491
}
492492

493493
/// Initialize an error within this domain with the given ``code``
494494
/// and ``userInfo``.
495495
public init(_ code: Code, userInfo: [String : Any] = [:]) {
496496
self.init(_nsError: NSError(domain: Self._nsErrorDomain,
497-
code: unsafeBinaryIntegerToInt(code.rawValue),
497+
code: unsafeFixedWidthIntegerToInt(code.rawValue),
498498
userInfo: userInfo))
499499
}
500500

0 commit comments

Comments
 (0)