Skip to content

Commit 22b5143

Browse files
authored
Merge pull request #20715 from anayini/contiguous-array-codable
[SR-7076] Make ContiguousArray Codable
2 parents 7a383f4 + a15ecd0 commit 22b5143

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

stdlib/public/core/Codable.swift.gyb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,40 @@ extension Array : Decodable where Element : Decodable {
17671767
}
17681768
}
17691769

1770+
extension ContiguousArray : Encodable where Element : Encodable {
1771+
/// Encodes the elements of this contiguous array into the given encoder
1772+
/// in an unkeyed container.
1773+
///
1774+
/// This function throws an error if any values are invalid for the given
1775+
/// encoder's format.
1776+
///
1777+
/// - Parameter encoder: The encoder to write data to.
1778+
public func encode(to encoder: Encoder) throws {
1779+
var container = encoder.unkeyedContainer()
1780+
for element in self {
1781+
try container.encode(element)
1782+
}
1783+
}
1784+
}
1785+
1786+
extension ContiguousArray : Decodable where Element : Decodable {
1787+
/// Creates a new contiguous array by decoding from the given decoder.
1788+
///
1789+
/// This initializer throws an error if reading from the decoder fails, or
1790+
/// if the data read is corrupted or otherwise invalid.
1791+
///
1792+
/// - Parameter decoder: The decoder to read data from.
1793+
public init(from decoder: Decoder) throws {
1794+
self.init()
1795+
1796+
var container = try decoder.unkeyedContainer()
1797+
while !container.isAtEnd {
1798+
let element = try container.decode(Element.self)
1799+
self.append(element)
1800+
}
1801+
}
1802+
}
1803+
17701804
extension Set : Encodable where Element : Encodable {
17711805
/// Encodes the elements of this set into the given encoder in an unkeyed
17721806
/// container.

0 commit comments

Comments
 (0)