Skip to content

Upgrading to the new integer protocols #951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 46 additions & 47 deletions Foundation/CGFloat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ public struct CGFloat {
public var native: NativeType
}

extension CGFloat : SignedNumeric {
// FIXME(integers): implement
public init?<T : BinaryInteger>(exactly source: T) {
fatalError()
}

@_transparent
public var magnitude: CGFloat {
return CGFloat(Swift.abs(native))
}

}

extension CGFloat : BinaryFloatingPoint {

public typealias RawSignificand = UInt
Expand Down Expand Up @@ -278,34 +291,29 @@ extension CGFloat : BinaryFloatingPoint {
return CGFloat(native.nextUp)
}

@_transparent
public var magnitude: CGFloat {
return CGFloat(Swift.abs(native))
}

@_transparent
public mutating func negate() {
native.negate()
}

@_transparent
public mutating func add(_ other: CGFloat) {
native.add(other.native)
public static func +=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a source-compatible change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically not, but these methods were there only to implement operators on the protocol, which is no longer required. In any case these missing methods can now be added for source compatibility in the protocol. Thanks for looking at this!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the missing methods here: swiftlang/swift@192c112

lhs.native += rhs.native
}

@_transparent
public mutating func subtract(_ other: CGFloat) {
native.subtract(other.native)
public static func -=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
lhs.native -= rhs.native
}

@_transparent
public mutating func multiply(by other: CGFloat) {
native.multiply(by: other.native)
public static func *=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
lhs.native *= rhs.native
}

@_transparent
public mutating func divide(by other: CGFloat) {
native.divide(by: other.native)
public static func /=(_ lhs: inout CGFloat, _ rhs: CGFloat) {
lhs.native /= rhs.native
}

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

@_transparent
public func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.adding(rhs)
}

@_transparent
public func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.subtracting(rhs)
}

@_transparent
public func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.multiplied(by: rhs)
}

@_transparent
public func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
return lhs.divided(by: rhs)
}
extension CGFloat {
@_transparent
public static func +(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
var lhs = lhs
lhs += rhs
return lhs
}

@_transparent
public func +=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.add(rhs)
}
@_transparent
public static func -(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
var lhs = lhs
lhs -= rhs
return lhs
}

@_transparent
public func -=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.subtract(rhs)
}
@_transparent
public static func *(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
var lhs = lhs
lhs *= rhs
return lhs
}

@_transparent
public func *=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.multiply(by: rhs)
@_transparent
public static func /(lhs: CGFloat, rhs: CGFloat) -> CGFloat {
var lhs = lhs
lhs /= rhs
return lhs
}
}

@_transparent
public func /=(lhs: inout CGFloat, rhs: CGFloat) {
lhs.divide(by: rhs)
}

//===----------------------------------------------------------------------===//
// Strideable Conformance
Expand Down
103 changes: 57 additions & 46 deletions Foundation/NSDecimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,6 @@ extension Decimal {
if !self.isFinite { return Decimal.nan }
return Decimal(_exponent: _exponent, _length: 8, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
}
public mutating func add(_ other: Decimal) {
var rhs = other
_ = NSDecimalAdd(&self, &self, &rhs, .plain)
}
public mutating func subtract(_ other: Decimal) {
var rhs = other
_ = NSDecimalSubtract(&self, &self, &rhs, .plain)
}
public mutating func multiply(by other: Decimal) {
var rhs = other
_ = NSDecimalMultiply(&self, &self, &rhs, .plain)
}
public mutating func divide(by other: Decimal) {
var rhs = other
_ = NSDecimalDivide(&self, &self, &rhs, .plain)
}
public mutating func negate() {
_isNegative = _isNegative == 0 ? 1 : 0
}
public func isEqual(to other: Decimal) -> Bool {
return self.compare(to: other) == .orderedSame
}
Expand Down Expand Up @@ -216,26 +197,6 @@ extension Decimal {
public var nextDown: Decimal {
return self - Decimal(_exponent: _exponent, _length: 1, _isNegative: 0, _isCompact: 1, _reserved: 0, _mantissa: (0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000))
}
public static func +(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer.add(rhs)
return answer;
}
public static func -(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer.subtract(rhs)
return answer;
}
public static func /(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer.divide(by: rhs)
return answer;
}
public static func *(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer.multiply(by: rhs)
return answer;
}
}

extension Decimal : Hashable, Comparable {
Expand Down Expand Up @@ -320,7 +281,63 @@ extension Decimal : ExpressibleByIntegerLiteral {
}
}

extension Decimal : SignedNumber {
extension Decimal : SignedNumeric {
public var magnitude: Decimal {
return Decimal(_exponent: _exponent, _length: _length, _isNegative: 0, _isCompact: _isCompact, _reserved: 0, _mantissa: _mantissa)
}

// FIXME(integers): implement properly
public init?<T : BinaryInteger>(exactly source: T) {
fatalError()
}

public static func +=(_ lhs: inout Decimal, _ rhs: Decimal) {
var rhs = rhs
_ = NSDecimalAdd(&lhs, &lhs, &rhs, .plain)
}

public static func -=(_ lhs: inout Decimal, _ rhs: Decimal) {
var rhs = rhs
_ = NSDecimalSubtract(&lhs, &lhs, &rhs, .plain)
}

public static func *=(_ lhs: inout Decimal, _ rhs: Decimal) {
var rhs = rhs
_ = NSDecimalMultiply(&lhs, &lhs, &rhs, .plain)
}

public static func /=(_ lhs: inout Decimal, _ rhs: Decimal) {
var rhs = rhs
_ = NSDecimalDivide(&lhs, &lhs, &rhs, .plain)
}

public static func +(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer += rhs
return answer;
}

public static func -(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer -= rhs
return answer;
}

public static func /(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer /= rhs
return answer;
}

public static func *(lhs: Decimal, rhs: Decimal) -> Decimal {
var answer = lhs
answer *= rhs
return answer;
}

public mutating func negate() {
_isNegative = _isNegative == 0 ? 1 : 0
}
}

extension Decimal : Strideable {
Expand All @@ -332,12 +349,6 @@ extension Decimal : Strideable {
}
}

extension Decimal : AbsoluteValuable {
public static func abs(_ x: Decimal) -> Decimal {
return Decimal(_exponent: x._exponent, _length: x._length, _isNegative: 0, _isCompact: x._isCompact, _reserved: 0, _mantissa: x._mantissa)
}
}

extension Decimal {
public typealias RoundingMode = NSDecimalNumber.RoundingMode
public typealias CalculationError = NSDecimalNumber.CalculationError
Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -372,13 +372,13 @@ public protocol __BridgedNSError : Error {
// Allow two bridged NSError types to be compared.
extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: SignedInteger {
public static func ==(lhs: Self, rhs: Self) -> Bool {
return lhs.rawValue.toIntMax() == rhs.rawValue.toIntMax()
return lhs.rawValue == rhs.rawValue
}
}

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

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

public extension __BridgedNSError where Self: RawRepresentable, Self.RawValue: UnsignedInteger {
public var _domain: String { return Self._nsErrorDomain }
public var _code: Int {
return Int(bitPattern: UInt(rawValue.toUIntMax()))
return Int(bitPattern: UInt(rawValue))
}

public init?(rawValue: RawValue) {
Expand Down
3 changes: 2 additions & 1 deletion Foundation/NSScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ internal protocol _BitShiftable {
static func <<(lhs: Self, rhs: Self) -> Self
}

internal protocol _IntegerLike : Integer, _BitShiftable {
// FIXME(integers): replace this protocol with just a FixedWidthInteger
internal protocol _IntegerLike : FixedWidthInteger, _BitShiftable {
init(_ value: Int)
static var max: Self { get }
static var min: Self { get }
Expand Down
Loading