forked from swiftlang/swift-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonEncodableProtocols.swift
More file actions
710 lines (637 loc) · 35.4 KB
/
CommonEncodableProtocols.swift
File metadata and controls
710 lines (637 loc) · 35.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A type that can encode itself into an external representation based on "common" data types.
public protocol CommonEncodable: ~Copyable {
/// Encodes this value into the given encoder.
///
// TODO: Make sure this is true for the existing encoders.
/// If the value does not actually encode anything, `encoder` will encode an empty
/// dictionary in its place.
///
/// This function throws(CodingError.Encoding) an error if any values are invalid for the given
/// encoder's format.
///
/// - Parameter encoder: The encoder to write data to.
func encode(to encoder: inout some CommonEncoder & ~Copyable & ~Escapable) throws(CodingError.Encoding)
}
public protocol CommonEncodableWithContext: ~Copyable {
associatedtype CommonEncodingContext: ~Copyable & ~Escapable
@_lifetime(encoder: copy encoder)
func encode(to encoder: inout some CommonEncoder & ~Copyable & ~Escapable, context: inout CommonEncodingContext) throws(CodingError.Encoding)
}
// Convenience for Copyable contexts - allows static member syntax
extension CommonEncodableWithContext where Self: ~Copyable, CommonEncodingContext: Copyable {
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(encoder: copy encoder)
public func encode(to encoder: inout some CommonEncoder & ~Copyable & ~Escapable, context: CommonEncodingContext) throws(CodingError.Encoding) {
var mutableContext = context
try self.encode(to: &encoder, context: &mutableContext)
}
}
/// A type that can encode values based on "common" data types into a native format
/// for external representation.
public protocol CommonEncoder: ~Copyable, ~Escapable {
/// The concrete type used by this encoder to encode array values.
associatedtype ArrayEncoder: CommonArrayEncoder & ~Copyable & ~Escapable
/// The concrete type used by this encoder to encode dictionary values.
associatedtype DictionaryEncoder: CommonDictionaryEncoder & ~Copyable & ~Escapable
/// The concrete type used by this encoder to encode struct values.
associatedtype StructEncoder: CommonStructEncoder & ~Copyable & ~Escapable
// TODO: This overload is a workaround for the inability for encoders to dynamically cast to particular Copyable types. It has a default implementation that calls into the normal ~Copyable path.
@_lifetime(self: copy self)
mutating func encode<T: CommonEncodable>(_ value: borrowing T) throws(CodingError.Encoding)
/// Encodes a nil value.
///
/// - throws: TBD
mutating func encodeNil() throws(CodingError.Encoding)
/// Encodes a boolean value.
///
/// - parameter value: The boolean to encode.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encode(_ bool: Bool) throws(CodingError.Encoding)
/// Encodes a integer value.
///
/// - parameter value: The integer to encode.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encode(_ value: Int) throws(CodingError.Encoding)
// TODO: etc.
@_lifetime(self: copy self)
mutating func encode(_ value: Int8) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: Int16) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: Int32) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: Int64) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: Int128) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: UInt) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: UInt8) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: UInt16) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: UInt32) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: UInt64) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: UInt128) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: Float) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(_ value: Double) throws(CodingError.Encoding)
/// Encodes a string value.
///
/// The default implementation calls `encode(_:UTF8Span)`.
///
/// - parameter string: The string to encode..
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeString(_ string: String) throws(CodingError.Encoding)
/// Encodes `UTF8Span` as a string.
///
/// - parameter span: The span to encode.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeString(_ span: UTF8Span) throws(CodingError.Encoding)
/// Encodes an sequence of `UInt8`s as bytes, in whatever representation
/// is best for the format implemented by the encoder.
///
/// This function has a default implementation that calls `encodeArray` and then calls
/// `ArrayEncoder.encode()`inside the closure for each element in the sequence.
///
/// - parameter bytes: The sequence of bytes to encode.
/// - parameter byteCount: A hint indicating the exact number of bytes
/// in the sequence. A non-nil value should always be passed if and only if the exact number
/// is known. Some serialization formats encode this in advance of a byte sequence,
/// so providing an incorrect value may result in undefined behavior, including potential
/// data corruption in the resulting payload. Providing no value may result in the data
/// being copied first before encoding.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeBytes(_ bytes: some Sequence<UInt8>, count: Int?) throws(CodingError.Encoding)
/// Encodes a `RawSpan`as bytes, in whatever representation
/// is best for the format implemented by the encoder.
///
/// - parameter span: The span of bytes to encode..
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeBytes(_ span: RawSpan) throws(CodingError.Encoding)
/// Encodes a struct's fields.
///
/// - parameter count: A hint indicating the exact number of struct fields that will be
/// encoded. A non-nil value should always be passed if and only if the exact number is
/// known. Some serialization formats encode this in advance of the struct contents, so
/// providing an incorrect value may result in undefined behavior, including potential data
/// corruption in the resulting payload. Providing no value may result in the struct values copied first before encoding.
/// - parameter closure: A closure in which the struct's fields are encoded.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeStructFields(count: Int?, _ closure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
/// Encodes a dictionary of keys and values.
///
/// Because encoders are encouraged to serialize values immediately, the order in which
/// dictionary key-value pairs is important and will often have a direct impact on the resulting
/// encoded data. For example, if you desire a particular ordering of keys in the output, the
/// closure passed to this function should encode the key-value pairs in that order.
///
/// - parameter elementCount: A hint indicating the exact number of key-value
/// pairs in the dictionary. A non-nil value should always be passed if and only if the exact
/// number is known. Some serialization formats encode this in advance of the dictionary
/// contents, so providing an incorrect value may result in undefined behavior, including
/// potential data corruption in the resulting payload. Providing no value may result in the
/// dictionary contents being copied first before encoding.
/// - parameter closure: A closure in which the dictionary's contents are encoded.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeDictionary(elementCount: Int?, _ closure: (inout DictionaryEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
/// Encodes a dictionary of keys and values.
///
/// The default implementation calls `self.encodeDictionary` with the dictionary's
/// element cout and encodes key-value pairs in an unpredicable order.
///
/// - parameter dictionary: The dictionary to encode
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeDictionary<Key: CodingStringKeyRepresentable, Value: CommonEncodable>(_ dictionary: [Key:Value]) throws(CodingError.Encoding)
/// Encodes an array of values.
///
/// - parameter elementCount: A hint indicating the exact number of elements
/// in the array. A non-nil value should always be passed if and only if the exact number
/// is known. Some serialization formats encode this in advance of the array contents,
/// so providing an incorrect value may result in undefined behavior, including potential
/// data corruption in the resulting payload. Providing no value may result in the array
/// contents being copied first before encoding.
/// - parameter closure: A closure in which the array's contents are encoded in
/// order.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeArray(elementCount: Int?, _ closure: (inout ArrayEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
/// Encodes an array of values.
///
/// - parameter elementCount: A hint indicating the exact number of elements
/// in the array. A non-nil value should always be passed if and only if the exact number
/// is known. Some serialization formats encode this in advance of the array contents,
/// so providing an incorrect value may result in undefined behavior, including potential
/// data corruption in the resulting payload. Providing no value may result in the array
/// contents being copied first before encoding.
/// - parameter closure: A closure in which the array's contents are encoded in
/// order.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeArray<Element: CommonEncodable>(_ array: [Element]) throws(CodingError.Encoding)
/// Encodes an enum case with no associated values.
///
/// This function has an inlinable default implementation that calls through to
/// `encodeEnumCase(field.key)`. This is more convenient for the caller and
/// also enables the optimizer to eliminate the `switch` statement that usually exists
/// inside the `EncodingField.key` property's implementation.
///
/// This is typically used for `enum` types that are not `RawRepresentable`,
/// but also have no associated values. The standardized representation of these
/// is dictionary whose key identifies the case, and whose value is an empty dictionary.
///
/// - parameter name: The name of the enum case.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ field: some EncodingField) throws(CodingError.Encoding)
/// Encodes an enum case with no associated values.
///
/// This is typically used for `enum` types that are not `RawRepresentable`,
/// but also have no associated values. The standardized representation of these
/// is dictionary whose key identifies the case, and whose value is an empty dictionary.
///
/// Some serialization formats do not encode string-based names for enums, so calling this
/// function directly is typically discouraged without some knowledge about what serialization
/// format is being used.
///
/// - parameter name: The name of the enum case.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: UTF8Span) throws(CodingError.Encoding)
/// Encodes an enum case with associated values.
///
/// This function has an inlinable default implementation that calls through to the variant
/// of this function that takes a `String`, passing `EncodingField.key`.
/// This is more convenient for the caller and also enables the optimizer to eliminate the
/// `switch` statement that usually exists inside the `EncodingField.key` property's
/// implementation.
///
/// This is typically used for `enum` types that have associated values.
/// The standardized representation of these is a struct whose field identifies the case,
/// and whose value is a struct containing the associated values. If the serialization format
/// uses strings, the associated are values keyed by label names, if any or `"_0"`, `"_1"`,
/// etc. for any values that have no explicit label.
///
/// - parameter field: The field of the enum case.
/// - parameter associatedValueCount: The number of associated values
/// that will be encoded. Some serialization formats encode this in advance of the struct
/// contents; providing an incorrect value may result in undefined behavior, including potential
/// data corruption in the resulting payload.
/// - parameter closure: A closure in which the associated values are encoded in order.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ field: some EncodingField, associatedValueCount: Int, _ associatedValueClosure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
/// Encodes an enum case with associated values.
///
/// This is typically used for `enum` types that have associated values.
/// The standardized representation of these is a struct whose key identifies the case,
/// and whose value is a struct containing the associated values. If the serialization format
/// uses strings, the associated are values keyed by label names, if any or `"_0"`, `"_1"`,
/// etc. for any values that have no explicit label.
///
/// Some serialization formats do not encode string-based names for enums or, so calling this
/// function directly is typically discouraged without some knowledge about what serialization
/// format is being used.
///
/// - parameter name: The name of the enum case.
/// - parameter associatedValueCount: The number of associated values
/// that will be encoded. Some serialization formats encode this in advance of the struct
/// contents; providing an incorrect value may result in undefined behavior, including potential
/// data corruption in the resulting payload.
/// - parameter closure: A closure in which the associated values are encoded in order.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: UTF8Span, associatedValueCount: Int, _ associatedValueClosure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
@_disfavoredOverload
@_lifetime(self: copy self)
mutating func encode(_ value: some Encodable) throws(CodingError.Encoding)
}
public extension CommonEncoder where Self: ~Copyable & ~Escapable {
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeBytes(_ bytes: some Sequence<UInt8>, count: Int?) throws(CodingError.Encoding) {
try encodeArray(elementCount: count) { arrayEncoder throws(CodingError.Encoding) in
for byte in bytes {
try arrayEncoder.encode(byte)
}
}
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeDictionary<Key: CodingStringKeyRepresentable, Value: CommonEncodable>(_ dictionary: [Key:Value]) throws(CodingError.Encoding) {
try encodeDictionary(elementCount: dictionary.count) { dictEncoder throws(CodingError.Encoding) in
for (key, value) in dictionary {
try key.withCodingStringUTF8Span { keySpan throws(CodingError.Encoding) in
try dictEncoder.encode(key: keySpan, value: value)
}
}
}
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeArray<Element: CommonEncodable>(_ array: [Element]) throws(CodingError.Encoding) {
try encodeArray(elementCount: array.count) { arrayEncoder throws(CodingError.Encoding) in
for element in array {
try arrayEncoder.encode(element)
}
}
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ field: some EncodingField) throws(CodingError.Encoding) {
try field.withUTF8Span { span throws(CodingError.Encoding) in
try self.encodeEnumCase(span)
}
}
@_disfavoredOverload
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: String) throws(CodingError.Encoding) {
// TODO: watchOS 32-bit
try self.encodeEnumCase(name.utf8Span)
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: StaticString) throws(CodingError.Encoding) {
if name.hasPointerRepresentation {
let utf8Span = UTF8Span(unchecked: .init(_unsafeStart: name.utf8Start, count: name.utf8CodeUnitCount), isKnownASCII: false)
try self.encodeEnumCase(utf8Span)
} else {
#if $Embedded
fatalError("non-pointer representation not supported in embedded Swift")
#else
// TODO: More efficient without allocations.
try self.encodeEnumCase(String(name.unicodeScalar).utf8Span)
#endif
}
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ field: some EncodingField, associatedValueCount: Int, _ associatedValueClosure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) {
try field.withUTF8Span { span throws(CodingError.Encoding) in
try self.encodeEnumCase(span, associatedValueCount: associatedValueCount, associatedValueClosure)
}
}
@_disfavoredOverload
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: String, associatedValueCount: Int, _ associatedValueClosure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) {
// TODO: watchOS 32-bit
try self.encodeEnumCase(name.utf8Span, associatedValueCount: associatedValueCount, associatedValueClosure)
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: StaticString, associatedValueCount: Int, _ associatedValueClosure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) {
if name.hasPointerRepresentation {
let utf8Span = UTF8Span(unchecked: .init(_unsafeStart: name.utf8Start, count: name.utf8CodeUnitCount), isKnownASCII: false)
try self.encodeEnumCase(utf8Span, associatedValueCount: associatedValueCount, associatedValueClosure)
} else {
#if $Embedded
fatalError("non-pointer representation not supported in embedded Swift")
#else
// TODO: More efficient without allocations.
try self.encodeEnumCase(String(name.unicodeScalar).utf8Span, associatedValueCount: associatedValueCount, associatedValueClosure)
#endif
}
}
}
extension CommonEncoder where Self: ~Copyable & ~Escapable {
/// Convenience: encode a JSONEncodable value using its default implementation.
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func encode<T: CommonEncodable & ~Copyable>(_ value: borrowing T) throws(CodingError.Encoding) {
try value.encode(to: &self)
}
// Copyable overload by default calls back into the ~Copyable one. See above.
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func encode<T: CommonEncodable>(_ value: borrowing T) throws(CodingError.Encoding) {
try value.encode(to: &self)
}
/// Convenience: encode using an explicit context (inout version for stateful contexts).
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func encode<T: CommonEncodableWithContext & ~Copyable>(_ value: borrowing T, context: inout T.CommonEncodingContext) throws(CodingError.Encoding) {
try value.encode(to: &self, context: &context)
}
/// Convenience: encode using an explicit context (copyable version for static member syntax).
/// This enables clean syntax: `encoder.encode(date, context: .iso8601)`
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func encode<T: CommonEncodableWithContext & ~Copyable>(_ value: borrowing T, context: T.CommonEncodingContext) throws(CodingError.Encoding) where T.CommonEncodingContext: Copyable {
try value.encode(to: &self, context: context)
}
}
/// A type that can encode a string-based dictionary based on "common"
/// data types into a native format for external representation.
public protocol CommonDictionaryEncoder: ~Copyable, ~Escapable {
associatedtype KeyEncoder: CommonEncoder & ~Copyable & ~Escapable
associatedtype ValueEncoder: CommonEncoder & ~Copyable & ~Escapable
@_lifetime(self: copy self)
mutating func encodeKey(keyEncoder: (inout KeyEncoder) throws(CodingError.Encoding) -> Void, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(key: String, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(key: UTF8Span, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
}
public extension CommonDictionaryEncoder where Self: ~Copyable & ~Escapable {
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) {
try self.encode(key: key.utf8Span, valueEncoder: valueEncoder)
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, value: borrowing some CommonEncodable & ~Copyable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
// TODO: This overload is a workaround for the inability for encoders to dynamically cast to particular Copyable types.
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, value: some CommonEncodable & Copyable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: UTF8Span, value: borrowing some CommonEncodable & ~Copyable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
// TODO: This overload is a workaround for the inability for encoders to dynamically cast to particular Copyable types.
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: UTF8Span, value: some CommonEncodable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
@_disfavoredOverload
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, value: some Encodable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
}
/// A type that can encode a struct type based on "common" data types into a native format for
/// external representation.
public protocol CommonStructEncoder: ~Copyable, ~Escapable {
associatedtype ValueEncoder: CommonEncoder & ~Copyable & ~Escapable
@_lifetime(self: copy self)
mutating func encode(field: some EncodingField, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(key: String, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
@_lifetime(self: copy self)
mutating func encode(key: UTF8Span, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
}
public extension CommonStructEncoder where Self: ~Copyable & ~Escapable {
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) {
try self.encode(key: key.utf8Span, valueEncoder: valueEncoder)
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(field: some EncodingField, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) {
try field.withUTF8Span { span throws(CodingError.Encoding) in
try self.encode(key: span, valueEncoder: valueEncoder)
}
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(field: some EncodingField, value: borrowing some CommonEncodable & ~Copyable) throws(CodingError.Encoding) {
try field.withUTF8Span { span throws(CodingError.Encoding) in
try self.encode(key: span) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, value: borrowing some CommonEncodable & ~Copyable) throws(CodingError.Encoding) {
try self.encode(key: key.utf8Span) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: UTF8Span, value: borrowing some CommonEncodable & ~Copyable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
@_disfavoredOverload
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(field: some EncodingField, value: some Encodable) throws(CodingError.Encoding) {
try field.withUTF8Span { span throws(CodingError.Encoding) in
try self.encode(key: span) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
}
@_disfavoredOverload
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, value: some Encodable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
}
// To avoid duplicate implementations for types that conform to both.
public extension CommonStructEncoder where Self: CommonDictionaryEncoder & ~Copyable & ~Escapable {
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, valueEncoder: (inout ValueEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) {
try self.encode(key: key.utf8Span, valueEncoder: valueEncoder)
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, value: borrowing some CommonEncodable & ~Copyable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: UTF8Span, value: borrowing some CommonEncodable & ~Copyable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
@_disfavoredOverload
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(field: some EncodingField, value: some Encodable) throws(CodingError.Encoding) {
try field.withUTF8Span { span throws(CodingError.Encoding) in
try self.encode(key: span) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
}
@_disfavoredOverload
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
mutating func encode(key: String, value: some Encodable) throws(CodingError.Encoding) {
try self.encode(key: key) { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
}
/// A type that can encode an array based on "common" data types into
/// a native format for external representation.
public protocol CommonArrayEncoder: ~Copyable, ~Escapable {
associatedtype ElementEncoder: CommonEncoder & ~Copyable & ~Escapable
/// Encodes an array element.
///
/// - parameter element: The element to encode.
/// - throws: TBD
@_lifetime(self: copy self)
mutating func encodeElement(_ elementEncoder: (inout ElementEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding)
var codingPath: CodingPath { get }
}
extension CommonArrayEncoder where Self: ~Copyable & ~Escapable {
/// Convenience: encode a CommonEncodable value.
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func encode<T: CommonEncodable & ~Copyable>(_ value: borrowing T) throws(CodingError.Encoding) {
try self.encodeElement { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
/// Convenience: encode an Encodable value.
@_disfavoredOverload
@inline(__always)
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func encode<T: Encodable>(_ value: T) throws(CodingError.Encoding) {
try self.encodeElement { encoder throws(CodingError.Encoding) in try encoder.encode(value) }
}
}
// Fall back error implementations:
public extension CommonEncoder where Self: ~Copyable & ~Escapable {
@_lifetime(self: copy self)
mutating func encodeNil() throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("nil") }
@_lifetime(self: copy self)
mutating func encode(_ bool: Bool) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("boolean") }
@_lifetime(self: copy self)
mutating func encode(_ value: Int) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Int") }
@_lifetime(self: copy self)
mutating func encode(_ value: Int8) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Int8") }
@_lifetime(self: copy self)
mutating func encode(_ value: Int16) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Int16") }
@_lifetime(self: copy self)
mutating func encode(_ value: Int32) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Int32") }
@_lifetime(self: copy self)
mutating func encode(_ value: Int64) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Int64") }
@_lifetime(self: copy self)
mutating func encode(_ value: Int128) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Int128") }
@_lifetime(self: copy self)
mutating func encode(_ value: UInt) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("UInt") }
@_lifetime(self: copy self)
mutating func encode(_ value: UInt8) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("UInt8") }
@_lifetime(self: copy self)
mutating func encode(_ value: UInt16) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("UInt16") }
@_lifetime(self: copy self)
mutating func encode(_ value: UInt32) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("UInt32") }
@_lifetime(self: copy self)
mutating func encode(_ value: UInt64) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("UInt64") }
@_lifetime(self: copy self)
mutating func encode(_ value: UInt128) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("UInt128") }
@_lifetime(self: copy self)
mutating func encode(_ value: Float) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Float") }
@_lifetime(self: copy self)
mutating func encode(_ value: Double) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Double") }
@_lifetime(self: copy self)
mutating func encodeString(_ string: String) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("string") }
@_lifetime(self: copy self)
mutating func encodeString(_ span: UTF8Span) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("string (UTF8Span)") }
@_lifetime(self: copy self)
mutating func encodeBytes(_ span: RawSpan) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("bytes") }
@_lifetime(self: copy self)
mutating func encodeStructFields(count: Int?, _ closure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) -> Void { throw CodingError.unsupportedEncodingType("struct") }
@_lifetime(self: copy self)
mutating func encodeDictionary(elementCount: Int?, _ closure: (inout DictionaryEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) -> Void { throw CodingError.unsupportedEncodingType("dictionary") }
@_lifetime(self: copy self)
mutating func encodeArray(elementCount: Int?, _ closure: (inout ArrayEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) -> Void { throw CodingError.unsupportedEncodingType("array") }
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: UTF8Span) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("enum") }
@_lifetime(self: copy self)
mutating func encodeEnumCase(_ name: UTF8Span, associatedValueCount: Int, _ associatedValueClosure: (inout StructEncoder) throws(CodingError.Encoding) -> Void) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("enum") }
@_disfavoredOverload
@_lifetime(self: copy self)
mutating func encode(_ value: some Encodable) throws(CodingError.Encoding) { throw CodingError.unsupportedEncodingType("Encodable") }
}