Skip to content

Commit 25dd336

Browse files
committed
[NFC] Adopt Darwin overlay's implementation of Decimal assignment operators += -= *= /=
1 parent d8fe904 commit 25dd336

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

Foundation/Decimal.swift

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public struct Decimal {
8484
self.__reserved = 0
8585
}
8686

87-
public init(_exponent: Int32, _length: UInt32, _isNegative: UInt32, _isCompact: UInt32, _reserved: UInt32, _mantissa: (UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16)){
87+
public init(_exponent: Int32, _length: UInt32, _isNegative: UInt32, _isCompact: UInt32, _reserved: UInt32, _mantissa: (UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16, UInt16)) {
8888
self._mantissa = _mantissa
8989
self.__exponent = Int8(truncatingIfNeeded: _exponent)
9090
self.__lengthAndFlags = UInt8(_length & 0b1111)
@@ -340,56 +340,57 @@ extension Decimal : SignedNumeric {
340340
fatalError()
341341
}
342342

343-
public static func +=(_ lhs: inout Decimal, _ rhs: Decimal) {
344-
var leftOp = lhs
345-
var rightOp = rhs
346-
_ = NSDecimalAdd(&lhs, &leftOp, &rightOp, .plain)
343+
public static func +=(lhs: inout Decimal, rhs: Decimal) {
344+
var rhs = rhs
345+
_ = withUnsafeMutablePointer(to: &lhs) {
346+
NSDecimalAdd($0, $0, &rhs, .plain)
347+
}
347348
}
348349

349-
public static func -=(_ lhs: inout Decimal, _ rhs: Decimal) {
350-
var leftOp = lhs
351-
var rightOp = rhs
352-
_ = NSDecimalSubtract(&lhs, &leftOp, &rightOp, .plain)
350+
public static func -=(lhs: inout Decimal, rhs: Decimal) {
351+
var rhs = rhs
352+
_ = withUnsafeMutablePointer(to: &lhs) {
353+
NSDecimalSubtract($0, $0, &rhs, .plain)
354+
}
353355
}
354356

355-
public static func *=(_ lhs: inout Decimal, _ rhs: Decimal) {
356-
var leftOp = lhs
357-
var rightOp = rhs
358-
_ = NSDecimalMultiply(&lhs, &leftOp, &rightOp, .plain)
357+
public static func *=(lhs: inout Decimal, rhs: Decimal) {
358+
var rhs = rhs
359+
_ = withUnsafeMutablePointer(to: &lhs) {
360+
NSDecimalMultiply($0, $0, &rhs, .plain)
361+
}
359362
}
360363

361-
public static func /=(_ lhs: inout Decimal, _ rhs: Decimal) {
362-
var leftOp = lhs
363-
var rightOp = rhs
364-
_ = NSDecimalDivide(&lhs, &leftOp, &rightOp, .plain)
364+
public static func /=(lhs: inout Decimal, rhs: Decimal) {
365+
var rhs = rhs
366+
_ = withUnsafeMutablePointer(to: &lhs) {
367+
NSDecimalDivide($0, $0, &rhs, .plain)
368+
}
365369
}
366370

367371
public static func +(lhs: Decimal, rhs: Decimal) -> Decimal {
368372
var answer = lhs
369373
answer += rhs
370-
return answer;
374+
return answer
371375
}
372376

373377
public static func -(lhs: Decimal, rhs: Decimal) -> Decimal {
374378
var answer = lhs
375379
answer -= rhs
376-
return answer;
377-
}
378-
379-
public static func /(lhs: Decimal, rhs: Decimal) -> Decimal {
380-
var answer = lhs
381-
answer /= rhs
382-
return answer;
380+
return answer
383381
}
384382

385383
public static func *(lhs: Decimal, rhs: Decimal) -> Decimal {
386384
var answer = lhs
387385
answer *= rhs
388-
return answer;
386+
return answer
389387
}
390388

391-
@available(*, unavailable, message: "Decimal does not yet fully adopt FloatingPoint.")
392-
public mutating func formTruncatingRemainder(dividingBy other: Decimal) { fatalError("Decimal does not yet fully adopt FloatingPoint") }
389+
public static func /(lhs: Decimal, rhs: Decimal) -> Decimal {
390+
var answer = lhs
391+
answer /= rhs
392+
return answer
393+
}
393394

394395
public mutating func negate() {
395396
guard _length != 0 else { return }
@@ -625,6 +626,9 @@ extension Decimal {
625626
public var isSignaling: Bool {
626627
return false
627628
}
629+
630+
@available(*, unavailable, message: "Decimal does not yet fully adopt FloatingPoint.")
631+
public mutating func formTruncatingRemainder(dividingBy other: Decimal) { fatalError("Decimal does not yet fully adopt FloatingPoint") }
628632
}
629633

630634
extension Decimal: CustomStringConvertible {

0 commit comments

Comments
 (0)