@@ -4146,153 +4146,72 @@ public extension RawRepresentable where RawValue == String, Self : Decodable {
4146
4146
// Optional/Collection Type Conformances
4147
4147
//===----------------------------------------------------------------------===//
4148
4148
4149
- @_inlineable // FIXME(sil-serialize-all)
4150
- @_versioned // FIXME(sil-serialize-all)
4151
- internal func assertTypeIsEncodable< T> ( _ type: T . Type , in wrappingType: Any . Type ) {
4152
- guard T . self is Encodable . Type else {
4153
- if T . self == Encodable . self || T . self == Codable . self {
4154
- preconditionFailure ( " \( wrappingType) does not conform to Encodable because Encodable does not conform to itself. You must use a concrete type to encode or decode. " )
4155
- } else {
4156
- preconditionFailure ( " \( wrappingType) does not conform to Encodable because \( T . self) does not conform to Encodable. " )
4157
- }
4158
- }
4159
- }
4160
-
4161
- @_inlineable // FIXME(sil-serialize-all)
4162
- @_versioned // FIXME(sil-serialize-all)
4163
- internal func assertTypeIsDecodable< T> ( _ type: T . Type , in wrappingType: Any . Type ) {
4164
- guard T . self is Decodable . Type else {
4165
- if T . self == Decodable . self || T . self == Codable . self {
4166
- preconditionFailure ( " \( wrappingType) does not conform to Decodable because Decodable does not conform to itself. You must use a concrete type to encode or decode. " )
4167
- } else {
4168
- preconditionFailure ( " \( wrappingType) does not conform to Decodable because \( T . self) does not conform to Decodable. " )
4169
- }
4170
- }
4171
- }
4172
-
4173
- // Temporary resolution for SR-5206.
4174
- //
4175
- // The following two extension on Encodable and Decodable are used below to provide static type information where we don't have any yet.
4176
- // The wrapped contents of the below generic types have to expose their Encodable/Decodable conformance via an existential cast/their metatype.
4177
- // Since those are dynamic types without static type guarantees, we cannot call generic methods taking those arguments, e.g.
4178
- //
4179
- // try container.encode((someElement as! Encodable))
4180
- //
4181
- // One way around this is to get elements to encode into `superEncoder`s and decode from `superDecoder`s because those interfaces are available via the existentials/metatypes.
4182
- // However, this direct encoding/decoding never gives containers a chance to intercept and do something custom on types.
4183
- //
4184
- // If we instead expose this custom private functionality of writing to/reading from containers directly, the containers do get this chance.
4185
-
4186
- // FIXME: Remove when conditional conformance is available.
4187
- extension Encodable {
4188
- @_inlineable // FIXME(sil-serialize-all)
4189
- @_versioned // FIXME(sil-serialize-all)
4190
- internal func __encode( to container: inout SingleValueEncodingContainer ) throws { try container. encode ( self ) }
4191
- @_inlineable // FIXME(sil-serialize-all)
4192
- @_versioned // FIXME(sil-serialize-all)
4193
- internal func __encode( to container: inout UnkeyedEncodingContainer ) throws { try container. encode ( self ) }
4194
- @_inlineable // FIXME(sil-serialize-all)
4195
- @_versioned // FIXME(sil-serialize-all)
4196
- internal func __encode< Key> ( to container: inout KeyedEncodingContainer < Key > , forKey key: Key ) throws { try container. encode ( self , forKey: key) }
4197
- }
4198
-
4199
- // FIXME: Remove when conditional conformance is available.
4200
- extension Decodable {
4201
- // Since we cannot call these __init, we'll give the parameter a '__'.
4202
- @_inlineable // FIXME(sil-serialize-all)
4203
- @_versioned // FIXME(sil-serialize-all)
4204
- internal init ( __from container: SingleValueDecodingContainer ) throws { self = try container. decode ( Self . self) }
4205
- @_inlineable // FIXME(sil-serialize-all)
4206
- @_versioned // FIXME(sil-serialize-all)
4207
- internal init ( __from container: inout UnkeyedDecodingContainer ) throws { self = try container. decode ( Self . self) }
4208
- @_inlineable // FIXME(sil-serialize-all)
4209
- @_versioned // FIXME(sil-serialize-all)
4210
- internal init < Key> ( __from container: KeyedDecodingContainer < Key > , forKey key: Key ) throws { self = try container. decode ( Self . self, forKey: key) }
4211
- }
4212
-
4213
- // FIXME: Uncomment when conditional conformance is available.
4214
- extension Optional : Encodable /* where Wrapped : Encodable */ {
4149
+ extension Optional : Encodable where Wrapped : Encodable {
4215
4150
@_inlineable // FIXME(sil-serialize-all)
4216
4151
public func encode( to encoder: Encoder ) throws {
4217
- assertTypeIsEncodable ( Wrapped . self, in: type ( of: self ) )
4218
-
4219
4152
var container = encoder. singleValueContainer ( )
4220
4153
switch self {
4221
4154
case . none: try container. encodeNil ( )
4222
- case . some( let wrapped) : try ( wrapped as! Encodable ) . __encode ( to : & container )
4155
+ case . some( let wrapped) : try container . encode ( wrapped )
4223
4156
}
4224
4157
}
4225
4158
}
4226
4159
4227
- extension Optional : Decodable /* where Wrapped : Decodable */ {
4160
+ extension Optional : Decodable where Wrapped : Decodable {
4228
4161
@_inlineable // FIXME(sil-serialize-all)
4229
4162
public init ( from decoder: Decoder ) throws {
4230
- // Initialize self here so we can get type(of: self).
4231
- self = . none
4232
- assertTypeIsDecodable ( Wrapped . self, in: type ( of: self ) )
4233
-
4234
4163
let container = try decoder. singleValueContainer ( )
4235
- if !container. decodeNil ( ) {
4236
- let metaType = ( Wrapped . self as! Decodable . Type)
4237
- let element = try metaType. init ( __from: container)
4238
- self = . some( element as! Wrapped )
4164
+ if container. decodeNil ( ) {
4165
+ self = . none
4166
+ } else {
4167
+ let element = try container. decode ( Wrapped . self)
4168
+ self = . some( element)
4239
4169
}
4240
4170
}
4241
4171
}
4242
4172
4243
- // FIXME: Uncomment when conditional conformance is available.
4244
- extension Array : Encodable /* where Element : Encodable */ {
4173
+ extension Array : Encodable where Element : Encodable {
4245
4174
@_inlineable // FIXME(sil-serialize-all)
4246
4175
public func encode( to encoder: Encoder ) throws {
4247
- assertTypeIsEncodable ( Element . self, in: type ( of: self ) )
4248
-
4249
4176
var container = encoder. unkeyedContainer ( )
4250
4177
for element in self {
4251
- try ( element as! Encodable ) . __encode ( to : & container )
4178
+ try container . encode ( element )
4252
4179
}
4253
4180
}
4254
4181
}
4255
4182
4256
- extension Array : Decodable /* where Element : Decodable */ {
4183
+ extension Array : Decodable where Element : Decodable {
4257
4184
@_inlineable // FIXME(sil-serialize-all)
4258
4185
public init ( from decoder: Decoder ) throws {
4259
- // Initialize self here so we can get type(of: self).
4260
4186
self . init ( )
4261
- assertTypeIsDecodable ( Element . self, in: type ( of: self ) )
4262
4187
4263
- let metaType = ( Element . self as! Decodable . Type)
4264
4188
var container = try decoder. unkeyedContainer ( )
4265
4189
while !container. isAtEnd {
4266
- let element = try metaType . init ( __from : & container )
4267
- self . append ( element as! Element )
4190
+ let element = try container . decode ( Element . self )
4191
+ self . append ( element)
4268
4192
}
4269
4193
}
4270
4194
}
4271
4195
4272
- extension Set : Encodable /* where Element : Encodable */ {
4196
+ extension Set : Encodable where Element : Encodable {
4273
4197
@_inlineable // FIXME(sil-serialize-all)
4274
4198
public func encode( to encoder: Encoder ) throws {
4275
- assertTypeIsEncodable ( Element . self, in: type ( of: self ) )
4276
-
4277
4199
var container = encoder. unkeyedContainer ( )
4278
4200
for element in self {
4279
- try ( element as! Encodable ) . __encode ( to : & container )
4201
+ try container . encode ( element )
4280
4202
}
4281
4203
}
4282
4204
}
4283
4205
4284
- extension Set : Decodable /* where Element : Decodable */ {
4206
+ extension Set : Decodable where Element : Decodable {
4285
4207
@_inlineable // FIXME(sil-serialize-all)
4286
4208
public init ( from decoder: Decoder ) throws {
4287
- // Initialize self here so we can get type(of: self).
4288
4209
self . init ( )
4289
- assertTypeIsDecodable ( Element . self, in: type ( of: self ) )
4290
4210
4291
- let metaType = ( Element . self as! Decodable . Type)
4292
4211
var container = try decoder. unkeyedContainer ( )
4293
4212
while !container. isAtEnd {
4294
- let element = try metaType . init ( __from : & container )
4295
- self . insert ( element as! Element )
4213
+ let element = try container . decode ( Element . self )
4214
+ self . insert ( element)
4296
4215
}
4297
4216
}
4298
4217
}
@@ -4321,57 +4240,49 @@ internal struct _DictionaryCodingKey : CodingKey {
4321
4240
}
4322
4241
}
4323
4242
4324
- extension Dictionary : Encodable /* where Key : Encodable, Value : Encodable */ {
4243
+ extension Dictionary : Encodable where Key : Encodable , Value : Encodable {
4325
4244
@_inlineable // FIXME(sil-serialize-all)
4326
4245
public func encode( to encoder: Encoder ) throws {
4327
- assertTypeIsEncodable ( Key . self, in: type ( of: self ) )
4328
- assertTypeIsEncodable ( Value . self, in: type ( of: self ) )
4329
-
4330
4246
if Key . self == String . self {
4331
4247
// Since the keys are already Strings, we can use them as keys directly.
4332
4248
var container = encoder. container ( keyedBy: _DictionaryCodingKey. self)
4333
4249
for (key, value) in self {
4334
4250
let codingKey = _DictionaryCodingKey ( stringValue: key as! String ) !
4335
- try ( value as! Encodable ) . __encode ( to : & container , forKey: codingKey)
4251
+ try container . encode ( value , forKey: codingKey)
4336
4252
}
4337
4253
} else if Key . self == Int . self {
4338
4254
// Since the keys are already Ints, we can use them as keys directly.
4339
4255
var container = encoder. container ( keyedBy: _DictionaryCodingKey. self)
4340
4256
for (key, value) in self {
4341
4257
let codingKey = _DictionaryCodingKey ( intValue: key as! Int ) !
4342
- try ( value as! Encodable ) . __encode ( to : & container , forKey: codingKey)
4258
+ try container . encode ( value , forKey: codingKey)
4343
4259
}
4344
4260
} else {
4345
4261
// Keys are Encodable but not Strings or Ints, so we cannot arbitrarily convert to keys.
4346
4262
// We can encode as an array of alternating key-value pairs, though.
4347
4263
var container = encoder. unkeyedContainer ( )
4348
4264
for (key, value) in self {
4349
- try ( key as! Encodable ) . __encode ( to : & container )
4350
- try ( value as! Encodable ) . __encode ( to : & container )
4265
+ try container . encode ( key )
4266
+ try container . encode ( value )
4351
4267
}
4352
4268
}
4353
4269
}
4354
4270
}
4355
4271
4356
- extension Dictionary : Decodable /* where Key : Decodable, Value : Decodable */ {
4272
+ extension Dictionary : Decodable where Key : Decodable , Value : Decodable {
4357
4273
@_inlineable // FIXME(sil-serialize-all)
4358
4274
public init ( from decoder: Decoder ) throws {
4359
- // Initialize self here so we can print type(of: self).
4360
4275
self . init ( )
4361
- assertTypeIsDecodable ( Key . self, in: type ( of: self ) )
4362
- assertTypeIsDecodable ( Value . self, in: type ( of: self ) )
4363
4276
4364
4277
if Key . self == String . self {
4365
4278
// The keys are Strings, so we should be able to expect a keyed container.
4366
4279
let container = try decoder. container ( keyedBy: _DictionaryCodingKey. self)
4367
- let valueMetaType = Value . self as! Decodable . Type
4368
4280
for key in container. allKeys {
4369
- let value = try valueMetaType . init ( __from : container , forKey: key)
4370
- self [ key. stringValue as! Key ] = ( value as! Value )
4281
+ let value = try container . decode ( Value . self , forKey: key)
4282
+ self [ key. stringValue as! Key ] = value
4371
4283
}
4372
4284
} else if Key . self == Int . self {
4373
4285
// The keys are Ints, so we should be able to expect a keyed container.
4374
- let valueMetaType = Value . self as! Decodable . Type
4375
4286
let container = try decoder. container ( keyedBy: _DictionaryCodingKey. self)
4376
4287
for key in container. allKeys {
4377
4288
guard key. intValue != nil else {
@@ -4385,8 +4296,8 @@ extension Dictionary : Decodable /* where Key : Decodable, Value : Decodable */
4385
4296
debugDescription: " Expected Int key but found String key instead. " ) )
4386
4297
}
4387
4298
4388
- let value = try valueMetaType . init ( __from : container , forKey: key)
4389
- self [ key. intValue! as! Key ] = ( value as! Value )
4299
+ let value = try container . decode ( Value . self , forKey: key)
4300
+ self [ key. intValue! as! Key ] = value
4390
4301
}
4391
4302
} else {
4392
4303
// We should have encoded as an array of alternating key-value pairs.
@@ -4400,18 +4311,16 @@ extension Dictionary : Decodable /* where Key : Decodable, Value : Decodable */
4400
4311
}
4401
4312
}
4402
4313
4403
- let keyMetaType = ( Key . self as! Decodable . Type)
4404
- let valueMetaType = ( Value . self as! Decodable . Type)
4405
4314
while !container. isAtEnd {
4406
- let key = try keyMetaType . init ( __from : & container )
4315
+ let key = try container . decode ( Key . self )
4407
4316
4408
4317
guard !container. isAtEnd else {
4409
4318
throw DecodingError . dataCorrupted ( DecodingError . Context ( codingPath: decoder. codingPath,
4410
4319
debugDescription: " Unkeyed container reached end before value in key-value pair. " ) )
4411
4320
}
4412
4321
4413
- let value = try valueMetaType . init ( __from : & container )
4414
- self [ key as! Key ] = ( value as! Value )
4322
+ let value = try container . decode ( Value . self )
4323
+ self [ key] = value
4415
4324
}
4416
4325
}
4417
4326
}
0 commit comments