Skip to content

Commit e1e6784

Browse files
author
Itai Ferber
committed
Split RawRep extensions for Encodable + Decodable
You shouldn't need to be Codable in order to get these implementations; being either Encodable or Decodable should be enough to get the relevant default implementation
1 parent 4c23207 commit e1e6784

File tree

1 file changed

+55
-19
lines changed

1 file changed

+55
-19
lines changed

stdlib/public/core/Codable.swift

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,14 @@ public struct CodingUserInfoKey : RawRepresentable, Equatable, Hashable {
18331833
self.rawValue = rawValue
18341834
}
18351835

1836+
/// Returns whether the given keys are equal.
1837+
///
1838+
/// - parameter lhs: The key to compare against.
1839+
/// - parameter rhs: The key to compare with.
1840+
public static func ==(_ lhs: CodingUserInfoKey, _ rhs: CodingUserInfoKey) -> Bool {
1841+
return lhs.rawValue == rhs.rawValue
1842+
}
1843+
18361844
/// The key's hash value.
18371845
public var hashValue: Int {
18381846
return self.rawValue.hashValue
@@ -2441,7 +2449,14 @@ extension String : Codable {
24412449
}
24422450
}
24432451

2444-
public extension RawRepresentable where RawValue == Bool, Self : Codable {
2452+
public extension RawRepresentable where RawValue == Bool, Self : Encodable {
2453+
public func encode(to encoder: Encoder) throws {
2454+
var container = encoder.singleValueContainer()
2455+
try container.encode(self.rawValue)
2456+
}
2457+
}
2458+
2459+
public extension RawRepresentable where RawValue == Bool, Self : Decodable {
24452460
public init(from decoder: Decoder) throws {
24462461
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
24472462
guard let value = Self(rawValue: decoded) else {
@@ -2450,14 +2465,16 @@ public extension RawRepresentable where RawValue == Bool, Self : Codable {
24502465

24512466
self = value
24522467
}
2468+
}
24532469

2470+
public extension RawRepresentable where RawValue == Int, Self : Encodable {
24542471
public func encode(to encoder: Encoder) throws {
24552472
var container = encoder.singleValueContainer()
24562473
try container.encode(self.rawValue)
24572474
}
24582475
}
24592476

2460-
public extension RawRepresentable where RawValue == Int, Self : Codable {
2477+
public extension RawRepresentable where RawValue == Int, Self : Decodable {
24612478
public init(from decoder: Decoder) throws {
24622479
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
24632480
guard let value = Self(rawValue: decoded) else {
@@ -2466,14 +2483,16 @@ public extension RawRepresentable where RawValue == Int, Self : Codable {
24662483

24672484
self = value
24682485
}
2486+
}
24692487

2488+
public extension RawRepresentable where RawValue == Int8, Self : Encodable {
24702489
public func encode(to encoder: Encoder) throws {
24712490
var container = encoder.singleValueContainer()
24722491
try container.encode(self.rawValue)
24732492
}
24742493
}
24752494

2476-
public extension RawRepresentable where RawValue == Int8, Self : Codable {
2495+
public extension RawRepresentable where RawValue == Int8, Self : Decodable {
24772496
public init(from decoder: Decoder) throws {
24782497
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
24792498
guard let value = Self(rawValue: decoded) else {
@@ -2482,14 +2501,16 @@ public extension RawRepresentable where RawValue == Int8, Self : Codable {
24822501

24832502
self = value
24842503
}
2504+
}
24852505

2506+
public extension RawRepresentable where RawValue == Int16, Self : Encodable {
24862507
public func encode(to encoder: Encoder) throws {
24872508
var container = encoder.singleValueContainer()
24882509
try container.encode(self.rawValue)
24892510
}
24902511
}
24912512

2492-
public extension RawRepresentable where RawValue == Int16, Self : Codable {
2513+
public extension RawRepresentable where RawValue == Int16, Self : Decodable {
24932514
public init(from decoder: Decoder) throws {
24942515
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
24952516
guard let value = Self(rawValue: decoded) else {
@@ -2498,14 +2519,16 @@ public extension RawRepresentable where RawValue == Int16, Self : Codable {
24982519

24992520
self = value
25002521
}
2522+
}
25012523

2524+
public extension RawRepresentable where RawValue == Int32, Self : Encodable {
25022525
public func encode(to encoder: Encoder) throws {
25032526
var container = encoder.singleValueContainer()
25042527
try container.encode(self.rawValue)
25052528
}
25062529
}
25072530

2508-
public extension RawRepresentable where RawValue == Int32, Self : Codable {
2531+
public extension RawRepresentable where RawValue == Int32, Self : Decodable {
25092532
public init(from decoder: Decoder) throws {
25102533
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
25112534
guard let value = Self(rawValue: decoded) else {
@@ -2514,14 +2537,16 @@ public extension RawRepresentable where RawValue == Int32, Self : Codable {
25142537

25152538
self = value
25162539
}
2540+
}
25172541

2542+
public extension RawRepresentable where RawValue == Int64, Self : Encodable {
25182543
public func encode(to encoder: Encoder) throws {
25192544
var container = encoder.singleValueContainer()
25202545
try container.encode(self.rawValue)
25212546
}
25222547
}
25232548

2524-
public extension RawRepresentable where RawValue == Int64, Self : Codable {
2549+
public extension RawRepresentable where RawValue == Int64, Self : Decodable {
25252550
public init(from decoder: Decoder) throws {
25262551
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
25272552
guard let value = Self(rawValue: decoded) else {
@@ -2530,14 +2555,16 @@ public extension RawRepresentable where RawValue == Int64, Self : Codable {
25302555

25312556
self = value
25322557
}
2558+
}
25332559

2560+
public extension RawRepresentable where RawValue == UInt, Self : Encodable {
25342561
public func encode(to encoder: Encoder) throws {
25352562
var container = encoder.singleValueContainer()
25362563
try container.encode(self.rawValue)
25372564
}
25382565
}
25392566

2540-
public extension RawRepresentable where RawValue == UInt, Self : Codable {
2567+
public extension RawRepresentable where RawValue == UInt, Self : Decodable {
25412568
public init(from decoder: Decoder) throws {
25422569
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
25432570
guard let value = Self(rawValue: decoded) else {
@@ -2546,14 +2573,16 @@ public extension RawRepresentable where RawValue == UInt, Self : Codable {
25462573

25472574
self = value
25482575
}
2576+
}
25492577

2578+
public extension RawRepresentable where RawValue == UInt8, Self : Encodable {
25502579
public func encode(to encoder: Encoder) throws {
25512580
var container = encoder.singleValueContainer()
25522581
try container.encode(self.rawValue)
25532582
}
25542583
}
25552584

2556-
public extension RawRepresentable where RawValue == UInt8, Self : Codable {
2585+
public extension RawRepresentable where RawValue == UInt8, Self : Decodable {
25572586
public init(from decoder: Decoder) throws {
25582587
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
25592588
guard let value = Self(rawValue: decoded) else {
@@ -2562,14 +2591,16 @@ public extension RawRepresentable where RawValue == UInt8, Self : Codable {
25622591

25632592
self = value
25642593
}
2594+
}
25652595

2596+
public extension RawRepresentable where RawValue == UInt16, Self : Encodable {
25662597
public func encode(to encoder: Encoder) throws {
25672598
var container = encoder.singleValueContainer()
25682599
try container.encode(self.rawValue)
25692600
}
25702601
}
25712602

2572-
public extension RawRepresentable where RawValue == UInt16, Self : Codable {
2603+
public extension RawRepresentable where RawValue == UInt16, Self : Decodable {
25732604
public init(from decoder: Decoder) throws {
25742605
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
25752606
guard let value = Self(rawValue: decoded) else {
@@ -2578,14 +2609,16 @@ public extension RawRepresentable where RawValue == UInt16, Self : Codable {
25782609

25792610
self = value
25802611
}
2612+
}
25812613

2614+
public extension RawRepresentable where RawValue == UInt32, Self : Encodable {
25822615
public func encode(to encoder: Encoder) throws {
25832616
var container = encoder.singleValueContainer()
25842617
try container.encode(self.rawValue)
25852618
}
25862619
}
25872620

2588-
public extension RawRepresentable where RawValue == UInt32, Self : Codable {
2621+
public extension RawRepresentable where RawValue == UInt32, Self : Decodable {
25892622
public init(from decoder: Decoder) throws {
25902623
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
25912624
guard let value = Self(rawValue: decoded) else {
@@ -2594,14 +2627,16 @@ public extension RawRepresentable where RawValue == UInt32, Self : Codable {
25942627

25952628
self = value
25962629
}
2630+
}
25972631

2632+
public extension RawRepresentable where RawValue == UInt64, Self : Encodable {
25982633
public func encode(to encoder: Encoder) throws {
25992634
var container = encoder.singleValueContainer()
26002635
try container.encode(self.rawValue)
26012636
}
26022637
}
26032638

2604-
public extension RawRepresentable where RawValue == UInt64, Self : Codable {
2639+
public extension RawRepresentable where RawValue == UInt64, Self : Decodable {
26052640
public init(from decoder: Decoder) throws {
26062641
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
26072642
guard let value = Self(rawValue: decoded) else {
@@ -2610,14 +2645,16 @@ public extension RawRepresentable where RawValue == UInt64, Self : Codable {
26102645

26112646
self = value
26122647
}
2648+
}
26132649

2650+
public extension RawRepresentable where RawValue == Float, Self : Encodable {
26142651
public func encode(to encoder: Encoder) throws {
26152652
var container = encoder.singleValueContainer()
26162653
try container.encode(self.rawValue)
26172654
}
26182655
}
26192656

2620-
public extension RawRepresentable where RawValue == Float, Self : Codable {
2657+
public extension RawRepresentable where RawValue == Float, Self : Decodable {
26212658
public init(from decoder: Decoder) throws {
26222659
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
26232660
guard let value = Self(rawValue: decoded) else {
@@ -2626,14 +2663,16 @@ public extension RawRepresentable where RawValue == Float, Self : Codable {
26262663

26272664
self = value
26282665
}
2666+
}
26292667

2668+
public extension RawRepresentable where RawValue == Double, Self : Encodable {
26302669
public func encode(to encoder: Encoder) throws {
26312670
var container = encoder.singleValueContainer()
26322671
try container.encode(self.rawValue)
26332672
}
26342673
}
26352674

2636-
public extension RawRepresentable where RawValue == Double, Self : Codable {
2675+
public extension RawRepresentable where RawValue == Double, Self : Decodable {
26372676
public init(from decoder: Decoder) throws {
26382677
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
26392678
guard let value = Self(rawValue: decoded) else {
@@ -2642,14 +2681,16 @@ public extension RawRepresentable where RawValue == Double, Self : Codable {
26422681

26432682
self = value
26442683
}
2684+
}
26452685

2686+
public extension RawRepresentable where RawValue == String, Self : Encodable {
26462687
public func encode(to encoder: Encoder) throws {
26472688
var container = encoder.singleValueContainer()
26482689
try container.encode(self.rawValue)
26492690
}
26502691
}
26512692

2652-
public extension RawRepresentable where RawValue == String, Self : Codable {
2693+
public extension RawRepresentable where RawValue == String, Self : Decodable {
26532694
public init(from decoder: Decoder) throws {
26542695
let decoded = try decoder.singleValueContainer().decode(RawValue.self)
26552696
guard let value = Self(rawValue: decoded) else {
@@ -2658,11 +2699,6 @@ public extension RawRepresentable where RawValue == String, Self : Codable {
26582699

26592700
self = value
26602701
}
2661-
2662-
public func encode(to encoder: Encoder) throws {
2663-
var container = encoder.singleValueContainer()
2664-
try container.encode(self.rawValue)
2665-
}
26662702
}
26672703

26682704
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)