Skip to content

Commit c1f7e23

Browse files
committed
Synchronize Measurement.swift with swift-corelibs-foundation
1 parent e61a15e commit c1f7e23

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

stdlib/public/SDK/Foundation/Measurement.swift

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if DEPLOYMENT_RUNTIME_SWIFT
14+
import CoreFoundation
15+
#else
1316
@_exported import Foundation // Clang module
1417
import _SwiftCoreFoundationOverlayShims
18+
#endif
1519

1620
/// A `Measurement` is a model type that holds a `Double` value associated with a `Unit`.
1721
///
@@ -25,13 +29,13 @@ public struct Measurement<UnitType : Unit> : ReferenceConvertible, Comparable, E
2529

2630
/// The value component of the `Measurement`.
2731
public var value: Double
28-
32+
2933
/// Create a `Measurement` given a specified value and unit.
3034
public init(value: Double, unit: UnitType) {
3135
self.value = value
3236
self.unit = unit
3337
}
34-
38+
3539
public var hashValue: Int {
3640
return Int(bitPattern: __CFHashDouble(value))
3741
}
@@ -42,11 +46,11 @@ extension Measurement : CustomStringConvertible, CustomDebugStringConvertible, C
4246
public var description: String {
4347
return "\(value) \(unit.symbol)"
4448
}
45-
49+
4650
public var debugDescription: String {
4751
return "\(value) \(unit.symbol)"
4852
}
49-
53+
5054
public var customMirror: Mirror {
5155
var c: [(label: String?, value: Any)] = []
5256
c.append((label: "value", value: value))
@@ -76,7 +80,7 @@ extension Measurement where UnitType : Dimension {
7680
}
7781
}
7882
}
79-
83+
8084
/// Converts the measurement to the specified unit.
8185
///
8286
/// - parameter otherUnit: A unit of the same `Dimension`.
@@ -85,7 +89,7 @@ extension Measurement where UnitType : Dimension {
8589
}
8690

8791
/// Add two measurements of the same Dimension.
88-
///
92+
///
8993
/// If the `unit` of the `lhs` and `rhs` are `isEqual`, then this returns the result of adding the `value` of each `Measurement`. If they are not equal, then this will convert both to the base unit of the `Dimension` and return the result as a `Measurement` of that base unit.
9094
/// - returns: The result of adding the two measurements.
9195
public static func +(lhs: Measurement<UnitType>, rhs: Measurement<UnitType>) -> Measurement<UnitType> {
@@ -170,7 +174,7 @@ extension Measurement {
170174
return lhs.value == rhs.value
171175
} else {
172176
if let lhsDimensionalUnit = lhs.unit as? Dimension,
173-
let rhsDimensionalUnit = rhs.unit as? Dimension {
177+
let rhsDimensionalUnit = rhs.unit as? Dimension {
174178
if type(of: lhsDimensionalUnit).baseUnit() == type(of: rhsDimensionalUnit).baseUnit() {
175179
let lhsValueInTermsOfBase = lhsDimensionalUnit.converter.baseUnitValue(fromValue: lhs.value)
176180
let rhsValueInTermsOfBase = rhsDimensionalUnit.converter.baseUnitValue(fromValue: rhs.value)
@@ -188,7 +192,7 @@ extension Measurement {
188192
return lhs.value < rhs.value
189193
} else {
190194
if let lhsDimensionalUnit = lhs.unit as? Dimension,
191-
let rhsDimensionalUnit = rhs.unit as? Dimension {
195+
let rhsDimensionalUnit = rhs.unit as? Dimension {
192196
if type(of: lhsDimensionalUnit).baseUnit() == type(of: rhsDimensionalUnit).baseUnit() {
193197
let lhsValueInTermsOfBase = lhsDimensionalUnit.converter.baseUnitValue(fromValue: lhs.value)
194198
let rhsValueInTermsOfBase = rhsDimensionalUnit.converter.baseUnitValue(fromValue: rhs.value)
@@ -202,17 +206,23 @@ extension Measurement {
202206

203207
// Implementation note: similar to NSArray, NSDictionary, etc., NSMeasurement's import as an ObjC generic type is suppressed by the importer. Eventually we will need a more general purpose mechanism to correctly import generic types.
204208

209+
#if DEPLOYMENT_RUNTIME_SWIFT
210+
internal typealias MeasurementBridgeType = _ObjectTypeBridgeable
211+
#else
212+
internal typealias MeasurementBridgeType = _ObjectiveCBridgeable
213+
#endif
214+
205215
@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
206-
extension Measurement : _ObjectiveCBridgeable {
216+
extension Measurement : MeasurementBridgeType {
207217
@_semantics("convertToObjectiveC")
208218
public func _bridgeToObjectiveC() -> NSMeasurement {
209219
return NSMeasurement(doubleValue: value, unit: unit)
210220
}
211-
221+
212222
public static func _forceBridgeFromObjectiveC(_ source: NSMeasurement, result: inout Measurement?) {
213223
result = Measurement(value: source.doubleValue, unit: source.unit as! UnitType)
214224
}
215-
225+
216226
public static func _conditionallyBridgeFromObjectiveC(_ source: NSMeasurement, result: inout Measurement?) -> Bool {
217227
if let u = source.unit as? UnitType {
218228
result = Measurement(value: source.doubleValue, unit: u)
@@ -221,7 +231,7 @@ extension Measurement : _ObjectiveCBridgeable {
221231
return false
222232
}
223233
}
224-
234+
225235
public static func _unconditionallyBridgeFromObjectiveC(_ source: NSMeasurement?) -> Measurement {
226236
let u = source!.unit as! UnitType
227237
return Measurement(value: source!.doubleValue, unit: u)
@@ -233,7 +243,11 @@ extension NSMeasurement : _HasCustomAnyHashableRepresentation {
233243
// Must be @nonobjc to avoid infinite recursion during bridging.
234244
@nonobjc
235245
public func _toCustomAnyHashable() -> AnyHashable? {
246+
#if DEPLOYMENT_RUNTIME_SWIFT
247+
return AnyHashable(Measurement._unconditionallyBridgeFromObjectiveC(self))
248+
#else
236249
return AnyHashable(self as Measurement)
250+
#endif
237251
}
238252
}
239253

0 commit comments

Comments
 (0)