@@ -162,34 +162,17 @@ public extension CustomNSError {
162
162
}
163
163
}
164
164
165
- /// Convert an arbitrary binary integer to an Int, reinterpreting signed
166
- /// -> unsigned if needed but trapping if the result is otherwise not
167
- /// expressible.
168
- func unsafeBinaryIntegerToInt< T: BinaryInteger > ( _ value: T ) -> Int {
169
- if T . isSigned {
170
- return numericCast ( value)
171
- }
172
-
173
- let uintValue : UInt = numericCast ( value)
174
- return Int ( bitPattern: uintValue)
175
- }
176
-
177
- /// Convert from an Int to an arbitrary binary integer, reinterpreting signed ->
178
- /// unsigned if needed but trapping if the result is otherwise not expressible.
179
- func unsafeBinaryIntegerFromInt< T: BinaryInteger > ( _ value: Int ) -> T {
180
- if T . isSigned {
181
- return numericCast ( value)
165
+ extension CustomNSError where Self: RawRepresentable , Self. RawValue: SignedInteger {
166
+ // The error code of Error with integral raw values is the raw value.
167
+ public var errorCode : Int {
168
+ return numericCast ( self . rawValue)
182
169
}
183
-
184
- let uintValue = UInt ( bitPattern: value)
185
- return numericCast ( uintValue)
186
170
}
187
171
188
- extension CustomNSError
189
- where Self: RawRepresentable , Self. RawValue: FixedWidthInteger {
172
+ extension CustomNSError where Self: RawRepresentable , Self. RawValue: UnsignedInteger {
190
173
// The error code of Error with integral raw values is the raw value.
191
174
public var errorCode : Int {
192
- return unsafeBinaryIntegerToInt ( self . rawValue)
175
+ return numericCast ( self . rawValue)
193
176
}
194
177
}
195
178
@@ -202,7 +185,13 @@ public extension Error where Self : CustomNSError {
202
185
}
203
186
204
187
public extension Error where Self: CustomNSError , Self: RawRepresentable ,
205
- Self. RawValue: FixedWidthInteger {
188
+ Self. RawValue: SignedInteger {
189
+ /// Default implementation for customized NSErrors.
190
+ var _code : Int { return self . errorCode }
191
+ }
192
+
193
+ public extension Error where Self: CustomNSError , Self: RawRepresentable ,
194
+ Self. RawValue: UnsignedInteger {
206
195
/// Default implementation for customized NSErrors.
207
196
var _code : Int { return self . errorCode }
208
197
}
@@ -395,7 +384,7 @@ extension _BridgedNSError {
395
384
public var _domain : String { return Self . _nsErrorDomain }
396
385
}
397
386
398
- extension _BridgedNSError where Self. RawValue: FixedWidthInteger {
387
+ extension _BridgedNSError where Self. RawValue: SignedInteger {
399
388
public var _code : Int { return Int ( rawValue) }
400
389
401
390
public init ? ( _bridgedNSError: NSError ) {
@@ -409,6 +398,22 @@ extension _BridgedNSError where Self.RawValue: FixedWidthInteger {
409
398
public var hashValue : Int { return _code }
410
399
}
411
400
401
+ extension _BridgedNSError where Self. RawValue: UnsignedInteger {
402
+ public var _code : Int {
403
+ return Int ( bitPattern: UInt ( rawValue) )
404
+ }
405
+
406
+ public init ? ( _bridgedNSError: NSError ) {
407
+ if _bridgedNSError. domain != Self . _nsErrorDomain {
408
+ return nil
409
+ }
410
+
411
+ self . init ( rawValue: RawValue ( UInt ( bitPattern: _bridgedNSError. code) ) )
412
+ }
413
+
414
+ public var hashValue : Int { return _code }
415
+ }
416
+
412
417
/// Describes a bridged error that stores the underlying NSError, so
413
418
/// it can be queried.
414
419
public protocol _BridgedStoredNSError :
@@ -441,14 +446,14 @@ internal func _stringDictToAnyHashableDict(_ input: [String : Any])
441
446
/// Various helper implementations for _BridgedStoredNSError
442
447
extension _BridgedStoredNSError {
443
448
public var code : Code {
444
- return Code ( rawValue: unsafeBinaryIntegerFromInt ( _nsError. code) ) !
449
+ return Code ( rawValue: numericCast ( _nsError. code) ) !
445
450
}
446
451
447
452
/// Initialize an error within this domain with the given ``code``
448
453
/// and ``userInfo``.
449
454
public init ( _ code: Code , userInfo: [ String : Any ] = [ : ] ) {
450
455
self . init ( _nsError: NSError ( domain: Self . errorDomain,
451
- code: unsafeBinaryIntegerToInt ( code. rawValue) ,
456
+ code: numericCast ( code. rawValue) ,
452
457
userInfo: _stringDictToAnyHashableDict ( userInfo) ) )
453
458
}
454
459
0 commit comments