Skip to content

Commit 3e19882

Browse files
committed
Mark a lot of methods as @inlinable
1 parent 3095119 commit 3e19882

5 files changed

Lines changed: 61 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
272272
* Split `JSON.Encoder.encodeAs*` and `JSON.Decoder.decode` methods into overload pairs where one takes `options:` and the other doesn't. This makes it easier to replace function references to `JSONEncoder`/`JSONDecoder` methods with the equivalents from PMJSON.
273273
* Add conformance to Combine's `TopLevelEncoder` and `TopLevelDecoder`, using `Data` as the input/output type. This means that `JSON.Encoder.encode(_:)` is now marked as deprecated instead of unavailable.
274274
* Rename `JSON.flatMap*` and `JSONObject.flatMap*` methods to `.compactMap*` instead when the transformation returns an optional. ([#28][])
275+
* Mark a lot of methods as `@inlinable`.
275276

276277
[#25]: https://github.com/postmates/PMJSON/issues/25 "GitHub: JSONObject.ns should return [String: Any]"
277278
[#28]: https://github.com/postmates/PMJSON/issues/28 "GitHub: Rename JSON.flatMap to JSON.compactMap"

Sources/Accessors.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import struct Foundation.Decimal
1717

1818
public extension JSON {
1919
/// Returns `true` iff the receiver is `.null`.
20+
@inlinable
2021
var isNull: Bool {
2122
switch self {
2223
case .null: return true
@@ -25,6 +26,7 @@ public extension JSON {
2526
}
2627

2728
/// Returns `true` iff the receiver is `.bool`.
29+
@inlinable
2830
var isBool: Bool {
2931
switch self {
3032
case .bool: return true
@@ -33,6 +35,7 @@ public extension JSON {
3335
}
3436

3537
/// Returns `true` iff the receiver is `.string`.
38+
@inlinable
3639
var isString: Bool {
3740
switch self {
3841
case .string: return true
@@ -41,6 +44,7 @@ public extension JSON {
4144
}
4245

4346
/// Returns `true` iff the receiver is `.int64`.
47+
@inlinable
4448
var isInt64: Bool {
4549
switch self {
4650
case .int64: return true
@@ -49,6 +53,7 @@ public extension JSON {
4953
}
5054

5155
/// Returns `true` iff the receiver is `.double`.
56+
@inlinable
5257
var isDouble: Bool {
5358
switch self {
5459
case .double: return true
@@ -57,6 +62,7 @@ public extension JSON {
5762
}
5863

5964
/// Returns `true` iff the receiver is a `.decimal`.
65+
@inlinable
6066
var isDecimal: Bool {
6167
switch self {
6268
case .decimal: return true
@@ -65,6 +71,7 @@ public extension JSON {
6571
}
6672

6773
/// Returns `true` iff the receiver is `.int64`, `.double`, or `.decimal`.
74+
@inlinable
6875
var isNumber: Bool {
6976
switch self {
7077
case .int64, .double, .decimal: return true
@@ -73,6 +80,7 @@ public extension JSON {
7380
}
7481

7582
/// Returns `true` iff the receiver is `.object`.
83+
@inlinable
7684
var isObject: Bool {
7785
switch self {
7886
case .object: return true
@@ -81,6 +89,7 @@ public extension JSON {
8189
}
8290

8391
/// Returns `true` iff the receiver is `.array`.
92+
@inlinable
8493
var isArray: Bool {
8594
switch self {
8695
case .array: return true
@@ -94,6 +103,7 @@ public extension JSON {
94103
///
95104
/// When setting, replaces the receiver with the given boolean value, or with
96105
/// null if the value is `nil`.
106+
@inlinable
97107
var bool: Bool? {
98108
get {
99109
switch self {
@@ -110,6 +120,7 @@ public extension JSON {
110120
///
111121
/// When setting, replaces the receiver with the given string value, or with
112122
/// null if the value is `nil`.
123+
@inlinable
113124
var string: String? {
114125
get {
115126
switch self {
@@ -129,6 +140,7 @@ public extension JSON {
129140
///
130141
/// When setting, replaces the receiver with the given integral value, or with
131142
/// null if the value is `nil`.
143+
@inlinable
132144
var int64: Int64? {
133145
get {
134146
switch self {
@@ -150,6 +162,7 @@ public extension JSON {
150162
///
151163
/// When setting, replaces the receiver with the given integral value, or with
152164
/// null if the value is `nil`.
165+
@inlinable
153166
var int: Int? {
154167
get {
155168
guard let value = self.int64 else { return nil}
@@ -167,6 +180,7 @@ public extension JSON {
167180
///
168181
/// When setting, replaces the receiver with the given double value, or with
169182
/// null if the value is `nil`.
183+
@inlinable
170184
var double: Double? {
171185
get {
172186
switch self {
@@ -187,6 +201,7 @@ public extension JSON {
187201
///
188202
/// When setting, replaces the receiver with the given object value, or with
189203
/// null if the value is `nil`.
204+
@inlinable
190205
var object: JSONObject? {
191206
get {
192207
switch self {
@@ -203,6 +218,7 @@ public extension JSON {
203218
///
204219
/// When setting, replaces the receiver with the given array value, or with
205220
/// null if the value is `nil`.
221+
@inlinable
206222
var array: JSONArray? {
207223
get {
208224
switch self {
@@ -219,6 +235,7 @@ public extension JSON {
219235
public extension JSON {
220236
/// Returns the string value if the receiver is `.string`, coerces the value to a string if
221237
/// the receiver is `.bool`, `.null`, `.int64`, `.double`, or `.decimal, or otherwise returns `nil`.
238+
@inlinable
222239
var asString: String? {
223240
return try? toString()
224241
}
@@ -230,6 +247,7 @@ public extension JSON {
230247
/// in 64 bits, `nil` is returned.
231248
/// If the receiver is `.string`, it must parse fully as an integral or floating-point number.
232249
/// If it parses as a floating-point number, it is truncated. If it does not fit in 64 bits, `nil` is returned.
250+
@inlinable
233251
var asInt64: Int64? {
234252
return try? toInt64()
235253
}
@@ -241,13 +259,15 @@ public extension JSON {
241259
/// in an `Int`, `nil` is returned.
242260
/// If the receiver is `.string`, it must parse fully as an integral or floating-point number.
243261
/// If it parses as a floating-point number, it is truncated. If it does not fit in an `Int`, `nil` is returned.
262+
@inlinable
244263
var asInt: Int? {
245264
return try? toInt()
246265
}
247266

248267
/// Returns the double value if the receiver is `.int64`, `.double`, or `.decimal`, coerces the value
249268
/// if the receiver is `.string`, otherwise returns `nil`.
250269
/// If the receiver is `.string`, it must parse fully as a floating-point number.
270+
@inlinable
251271
var asDouble: Double? {
252272
return try? toDouble()
253273
}
@@ -256,19 +276,22 @@ public extension JSON {
256276
public extension JSON {
257277
/// If the receiver is `.object`, returns the result of subscripting the object.
258278
/// Otherwise, returns `nil`.
279+
@inlinable
259280
subscript(key: String) -> JSON? {
260281
return self.object?[key]
261282
}
262283

263284
/// If the receiver is `.array` and the index is in range of the array, returns the result of subscripting the array.
264285
/// Otherwise returns `nil`.
286+
@inlinable
265287
subscript(index: Int) -> JSON? {
266288
guard let ary = self.array else { return nil }
267289
guard index >= ary.startIndex && index < ary.endIndex else { return nil }
268290
return ary[index]
269291
}
270292
}
271293

294+
@usableFromInline
272295
internal func convertDoubleToInt64(_ d: Double) -> Int64? {
273296
// Int64(Double(Int64.max)) asserts because it interprets it as out of bounds.
274297
// Int64(Double(Int64.min)) works just fine.
@@ -278,6 +301,7 @@ internal func convertDoubleToInt64(_ d: Double) -> Int64? {
278301
return Int64(d)
279302
}
280303

304+
@usableFromInline
281305
internal func convertDecimalToInt64(_ d: Decimal) -> Int64? {
282306
if d > Int64.maxDecimal || d < Int64.minDecimal {
283307
return nil
@@ -286,6 +310,7 @@ internal func convertDecimalToInt64(_ d: Decimal) -> Int64? {
286310
return NSDecimalNumber(decimal: d).int64Value
287311
}
288312

313+
@usableFromInline
289314
internal func convertDecimalToUInt64(_ d: Decimal) -> UInt64? {
290315
if d > UInt64.maxDecimal || d < UInt64.minDecimal {
291316
return nil

Sources/DecimalNumber.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ internal extension UInt64 {
466466

467467
internal extension Decimal {
468468
// NB: As of Swift 3.0.1, Decimal(_: Double) can produce incorrect results (SR-3130)
469+
@usableFromInline
469470
init(workaround value: Double) {
470471
self = NSNumber(value: value).decimalValue
471472
}

Sources/JSON.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,37 @@ public enum JSON {
3838
case array(JSONArray)
3939

4040
/// Initializes `self` as a boolean with the value `bool`.
41+
@inlinable
4142
public init(_ bool: Bool) {
4243
self = .bool(bool)
4344
}
4445
/// Initializes `self` as a string with the value `str`.
46+
@inlinable
4547
public init(_ str: String) {
4648
self = .string(str)
4749
}
4850
/// Initializes `self` as a 64-bit integer with the value `i`.
51+
@inlinable
4952
public init(_ i: Int64) {
5053
self = .int64(i)
5154
}
5255
/// Initializes `self` as a double with the value `d`.
56+
@inlinable
5357
public init(_ d: Double) {
5458
self = .double(d)
5559
}
60+
/// Initializes `self` as a decimal with the value `d`.
61+
@inlinable
5662
public init(_ d: Decimal) {
5763
self = .decimal(d)
5864
}
5965
/// Initializes `self` as an object with the value `obj`.
66+
@inlinable
6067
public init(_ obj: JSONObject) {
6168
self = .object(obj)
6269
}
6370
/// Initializes `self` as an array with the value `ary`.
71+
@inlinable
6472
public init(_ ary: JSONArray) {
6573
self = .array(ary)
6674
}
@@ -69,26 +77,31 @@ public enum JSON {
6977
// Convenience conversions.
7078
extension JSON {
7179
/// Returns a `JSON.int64` with the given value.
80+
@inlinable
7281
public static func int(_ value: Int) -> JSON {
7382
return .int64(Int64(value))
7483
}
7584

7685
/// Initializes `self` as a 64-bit integer with the value `i`.
86+
@inlinable
7787
public init(_ i: Int) {
7888
self = .int64(Int64(i))
7989
}
8090

8191
/// Initializes `self` as an array with the contents of the sequence `seq`.
92+
@inlinable
8293
public init<S: Sequence>(_ seq: S) where S.Iterator.Element == JSON {
8394
self = .array(JSONArray(seq))
8495
}
8596

8697
/// Initializes `self` as an array with the contents of the sequence `seq`.
98+
@inlinable
8799
public init<S: Sequence>(_ seq: S) where S.Iterator.Element == JSONObject {
88100
self = .array(JSONArray(seq.lazy.map(JSON.init)))
89101
}
90102

91103
/// Initializes `self` as an array with the contents of the sequence `seq`.
104+
@inlinable
92105
public init<S: Sequence>(_ seq: S) where S.Iterator.Element == JSONArray {
93106
self = .array(JSONArray(seq.lazy.map(JSON.init)))
94107
}
@@ -97,6 +110,7 @@ extension JSON {
97110
public typealias JSONArray = ContiguousArray<JSON>
98111

99112
extension JSON: Equatable {
113+
@inlinable
100114
public static func ==(lhs: JSON, rhs: JSON) -> Bool {
101115
switch (lhs, rhs) {
102116
case (.null, .null): return true
@@ -119,10 +133,12 @@ extension JSON: Equatable {
119133
}
120134

121135
extension JSON: TextOutputStreamable, CustomStringConvertible, CustomDebugStringConvertible {
136+
@inlinable
122137
public func write<Target : TextOutputStream>(to target: inout Target) {
123138
JSON.encode(self, to: &target)
124139
}
125140

141+
@inlinable
126142
public var description: String {
127143
return JSON.encodeAsString(self)
128144
}
@@ -139,42 +155,51 @@ extension JSON: TextOutputStreamable, CustomStringConvertible, CustomDebugString
139155
}
140156

141157
extension JSON: ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, ExpressibleByBooleanLiteral, ExpressibleByNilLiteral {
158+
@inlinable
142159
public init(integerLiteral value: Int64) {
143160
self = .int64(value)
144161
}
145162

163+
@inlinable
146164
public init(floatLiteral value: Double) {
147165
self = .double(value)
148166
}
149167

168+
@inlinable
150169
public init(booleanLiteral value: Bool) {
151170
self = .bool(value)
152171
}
153172

173+
@inlinable
154174
public init(nilLiteral: ()) {
155175
self = .null
156176
}
157177
}
158178

159179
extension JSON: ExpressibleByStringLiteral {
180+
@inlinable
160181
public init(stringLiteral value: String) {
161182
self = .string(value)
162183
}
163184

185+
@inlinable
164186
public init(extendedGraphemeClusterLiteral value: String) {
165187
self = .string(value)
166188
}
167189

190+
@inlinable
168191
public init(unicodeScalarLiteral value: String) {
169192
self = .string(value)
170193
}
171194
}
172195

173196
extension JSON: ExpressibleByArrayLiteral, ExpressibleByDictionaryLiteral {
197+
@inlinable
174198
public init(arrayLiteral elements: JSON...) {
175199
self = .array(JSONArray(elements))
176200
}
177201

202+
@inlinable
178203
public init(dictionaryLiteral elements: (String, JSON)...) {
179204
self = .object(JSONObject(elements))
180205
}

0 commit comments

Comments
 (0)