Skip to content

Commit ec07107

Browse files
authored
Merge pull request #951 from moiseev/new-integer-protocols
Upgrading to the new integer protocols
2 parents 8eca58f + 5be1b20 commit ec07107

File tree

5 files changed

+132
-121
lines changed

5 files changed

+132
-121
lines changed

Foundation/CGFloat.swift

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ public struct CGFloat {
145145
public var native: NativeType
146146
}
147147

148+
extension CGFloat : SignedNumeric {
149+
// FIXME(integers): implement
150+
public init?<T : BinaryInteger>(exactly source: T) {
151+
fatalError()
152+
}
153+
154+
@_transparent
155+
public var magnitude: CGFloat {
156+
return CGFloat(Swift.abs(native))
157+
}
158+
159+
}
160+
148161
extension CGFloat : BinaryFloatingPoint {
149162

150163
public typealias RawSignificand = UInt
@@ -278,34 +291,29 @@ extension CGFloat : BinaryFloatingPoint {
278291
return CGFloat(native.nextUp)
279292
}
280293

281-
@_transparent
282-
public var magnitude: CGFloat {
283-
return CGFloat(Swift.abs(native))
284-
}
285-
286294
@_transparent
287295
public mutating func negate() {
288296
native.negate()
289297
}
290298

291299
@_transparent
292-
public mutating func add(_ other: CGFloat) {
293-
native.add(other.native)
300+
public static func +=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
301+
lhs.native += rhs.native
294302
}
295303

296304
@_transparent
297-
public mutating func subtract(_ other: CGFloat) {
298-
native.subtract(other.native)
305+
public static func -=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
306+
lhs.native -= rhs.native
299307
}
300308

301309
@_transparent
302-
public mutating func multiply(by other: CGFloat) {
303-
native.multiply(by: other.native)
310+
public static func *=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
311+
lhs.native *= rhs.native
304312
}
305313

306314
@_transparent
307-
public mutating func divide(by other: CGFloat) {
308-
native.divide(by: other.native)
315+
public static func /=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
316+
lhs.native /= rhs.native
309317
}
310318

311319
@_transparent
@@ -564,45 +572,36 @@ extension Float {
564572
// tweaking the overload resolution rules, or by removing the other
565573
// definitions in the standard lib, or both.
566574

567-
@_transparent
568-
public func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
569-
return lhs.adding(rhs)
570-
}
571-
572-
@_transparent
573-
public func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
574-
return lhs.subtracting(rhs)
575-
}
576-
577-
@_transparent
578-
public func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
579-
return lhs.multiplied(by: rhs)
580-
}
581-
582-
@_transparent
583-
public func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
584-
return lhs.divided(by: rhs)
585-
}
575+
extension CGFloat {
576+
@_transparent
577+
public static func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
578+
var lhs = lhs
579+
lhs += rhs
580+
return lhs
581+
}
586582

587-
@_transparent
588-
public func +=(lhs: inout CGFloat, rhs: CGFloat) {
589-
lhs.add(rhs)
590-
}
583+
@_transparent
584+
public static func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
585+
var lhs = lhs
586+
lhs -= rhs
587+
return lhs
588+
}
591589

592-
@_transparent
593-
public func -=(lhs: inout CGFloat, rhs: CGFloat) {
594-
lhs.subtract(rhs)
595-
}
590+
@_transparent
591+
public static func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
592+
var lhs = lhs
593+
lhs *= rhs
594+
return lhs
595+
}
596596

597-
@_transparent
598-
public func *=(lhs: inout CGFloat, rhs: CGFloat) {
599-
lhs.multiply(by: rhs)
597+
@_transparent
598+
public static func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
599+
var lhs = lhs
600+
lhs /= rhs
601+
return lhs
602+
}
600603
}
601604

602-
@_transparent
603-
public func /=(lhs: inout CGFloat, rhs: CGFloat) {
604-
lhs.divide(by: rhs)
605-
}
606605

607606
//===----------------------------------------------------------------------===//
608607
// Strideable Conformance

Foundation/NSDecimal.swift

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,6 @@ extension Decimal {
166166
if !self.isFinite { return Decimal.nan }
167167
return Decimal(_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
168168
}
169-
public mutating func add(_ other: Decimal) {
170-
var rhs = other
171-
_ = NSDecimalAdd(&self, &self, &rhs, .plain)
172-
}
173-
public mutating func subtract(_ other: Decimal) {
174-
var rhs = other
175-
_ = NSDecimalSubtract(&self, &self, &rhs, .plain)
176-
}
177-
public mutating func multiply(by other: Decimal) {
178-
var rhs = other
179-
_ = NSDecimalMultiply(&self, &self, &rhs, .plain)
180-
}
181-
public mutating func divide(by other: Decimal) {
182-
var rhs = other
183-
_ = NSDecimalDivide(&self, &self, &rhs, .plain)
184-
}
185-
public mutating func negate() {
186-
_isNegative = _isNegative == 0 ? 1 : 0
187-
}
188169
public func isEqual(to other: Decimal) -> Bool {
189170
return self.compare(to: other) == .orderedSame
190171
}
@@ -216,26 +197,6 @@ extension Decimal {
216197
public var nextDown: Decimal {
217198
return self - Decimal(_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
218199
}
219-
public static func +(lhs: Decimal, rhs: Decimal) -> Decimal {
220-
var answer = lhs
221-
answer.add(rhs)
222-
return answer;
223-
}
224-
public static func -(lhs: Decimal, rhs: Decimal) -> Decimal {
225-
var answer = lhs
226-
answer.subtract(rhs)
227-
return answer;
228-
}
229-
public static func /(lhs: Decimal, rhs: Decimal) -> Decimal {
230-
var answer = lhs
231-
answer.divide(by: rhs)
232-
return answer;
233-
}
234-
public static func *(lhs: Decimal, rhs: Decimal) -> Decimal {
235-
var answer = lhs
236-
answer.multiply(by: rhs)
237-
return answer;
238-
}
239200
}
240201

241202
extension Decimal : Hashable, Comparable {
@@ -320,7 +281,63 @@ extension Decimal : ExpressibleByIntegerLiteral {
320281
}
321282
}
322283

323-
extension Decimal : SignedNumber {
284+
extension Decimal : SignedNumeric {
285+
public var magnitude: Decimal {
286+
return Decimal(_exponent: _exponent, _length: _length, _isNegative: 0, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa)
287+
}
288+
289+
// FIXME(integers): implement properly
290+
public init?<T : BinaryInteger>(exactly source: T) {
291+
fatalError()
292+
}
293+
294+
public static func +=(_ lhs: inout Decimal, _ rhs: Decimal) {
295+
var rhs = rhs
296+
_ = NSDecimalAdd(&lhs, &lhs, &rhs, .plain)
297+
}
298+
299+
public static func -=(_ lhs: inout Decimal, _ rhs: Decimal) {
300+
var rhs = rhs
301+
_ = NSDecimalSubtract(&lhs, &lhs, &rhs, .plain)
302+
}
303+
304+
public static func *=(_ lhs: inout Decimal, _ rhs: Decimal) {
305+
var rhs = rhs
306+
_ = NSDecimalMultiply(&lhs, &lhs, &rhs, .plain)
307+
}
308+
309+
public static func /=(_ lhs: inout Decimal, _ rhs: Decimal) {
310+
var rhs = rhs
311+
_ = NSDecimalDivide(&lhs, &lhs, &rhs, .plain)
312+
}
313+
314+
public static func +(lhs: Decimal, rhs: Decimal) -> Decimal {
315+
var answer = lhs
316+
answer += rhs
317+
return answer;
318+
}
319+
320+
public static func -(lhs: Decimal, rhs: Decimal) -> Decimal {
321+
var answer = lhs
322+
answer -= rhs
323+
return answer;
324+
}
325+
326+
public static func /(lhs: Decimal, rhs: Decimal) -> Decimal {
327+
var answer = lhs
328+
answer /= rhs
329+
return answer;
330+
}
331+
332+
public static func *(lhs: Decimal, rhs: Decimal) -> Decimal {
333+
var answer = lhs
334+
answer *= rhs
335+
return answer;
336+
}
337+
338+
public mutating func negate() {
339+
_isNegative = _isNegative == 0 ? 1 : 0
340+
}
324341
}
325342

326343
extension Decimal : Strideable {
@@ -332,12 +349,6 @@ extension Decimal : Strideable {
332349
}
333350
}
334351

335-
extension Decimal : AbsoluteValuable {
336-
public static func abs(_ x: Decimal) -> Decimal {
337-
return Decimal(_exponent: x._exponent, _length: x._length, _isNegative: 0, _isCompact: x._isCompact, _reserved: 0, _mantissa: x._mantissa)
338-
}
339-
}
340-
341352
extension Decimal {
342353
public typealias RoundingMode = NSDecimalNumber.RoundingMode
343354
public typealias CalculationError = NSDecimalNumber.CalculationError

Foundation/NSError.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,13 @@ public protocol __BridgedNSError : Error {
372372
// Allow two bridged NSError types to be compared.
373373
extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: SignedInteger {
374374
public static func ==(lhs: Self, rhs: Self) -> Bool {
375-
return lhs.rawValue.toIntMax() == rhs.rawValue.toIntMax()
375+
return lhs.rawValue == rhs.rawValue
376376
}
377377
}
378378

379379
public extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: SignedInteger {
380380
public var _domain: String { return Self._nsErrorDomain }
381-
public var _code: Int { return Int(rawValue.toIntMax()) }
381+
public var _code: Int { return Int(rawValue) }
382382

383383
public init?(rawValue: RawValue) {
384384
self = unsafeBitCast(rawValue, to: Self.self)
@@ -398,14 +398,14 @@ public extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: S
398398
// Allow two bridged NSError types to be compared.
399399
extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
400400
public static func ==(lhs: Self, rhs: Self) -> Bool {
401-
return lhs.rawValue.toUIntMax() == rhs.rawValue.toUIntMax()
401+
return lhs.rawValue == rhs.rawValue
402402
}
403403
}
404404

405405
public extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
406406
public var _domain: String { return Self._nsErrorDomain }
407407
public var _code: Int {
408-
return Int(bitPattern: UInt(rawValue.toUIntMax()))
408+
return Int(bitPattern: UInt(rawValue))
409409
}
410410

411411
public init?(rawValue: RawValue) {

Foundation/NSScanner.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ internal protocol _BitShiftable {
219219
static func <<(lhs: Self, rhs: Self) -> Self
220220
}
221221

222-
internal protocol _IntegerLike : Integer, _BitShiftable {
222+
// FIXME(integers): replace this protocol with just a FixedWidthInteger
223+
internal protocol _IntegerLike : FixedWidthInteger, _BitShiftable {
223224
init(_ value: Int)
224225
static var max: Self { get }
225226
static var min: Self { get }

0 commit comments

Comments
 (0)