[PersistentSet] Add ExpressibleByArrayLiteral, Codable, Custom[Debug]StringConvertible, CustomReflectable conformances

This commit is contained in:
Karoy Lorentey committed 2022-09-24 17:02:15 -07:00
1 parent d87f5fd1e1
commit 00ae83df1e
6 files changed
+148 -10

No files matched your search

@@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
extension PersistentSet: Encodable where Element: Encodable {
/// Encodes the elements of this ordered set into the given encoder.
///
/// - Parameter encoder: The encoder to write data to.
@inlinable
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(contentsOf: self)
}
}
extension PersistentSet: Decodable where Element: Decodable {
/// Creates a new ordered set by decoding from the given decoder.
///
/// This initializer throws an error if reading from the decoder fails, or
/// if the decoded contents contain duplicate values.
///
/// - Parameter decoder: The decoder to read data from.
@inlinable
public init(from decoder: Decoder) throws {
self.init()
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
let element = try container.decode(Element.self)
let inserted = self._insert(element)
guard inserted else {
let context = DecodingError.Context(
codingPath: container.codingPath,
debugDescription: "Decoded elements aren't unique (first duplicate at offset \(self.count))")
throw DecodingError.dataCorrupted(context)
}
}
}
}
@@ -9,8 +9,9 @@
//
//===----------------------------------------------------------------------===//
extension PersistentDictionary: CustomReflectable {
extension PersistentSet: CustomReflectable {
/// The custom mirror for this instance.
public var customMirror: Mirror {
Mirror(self, unlabeledChildren: self, displayStyle: .dictionary)
Mirror(self, unlabeledChildren: self, displayStyle: .collection)
}
}
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
import _CollectionsUtilities
extension PersistentSet {
public static var _isConsistencyCheckingEnabled: Bool {
_isCollectionsInternalCheckingEnabled
}
@inlinable
public func _invariantCheck() {
_root._fullInvariantCheck(.top, .emptyPrefix)
}
public func _dump(iterationOrder: Bool = false) {
_root.dump(iterationOrder: iterationOrder)
}
public static var _maxDepth: Int {
_Level.limit
}
public var _statistics: _HashTreeStatistics {
var stats = _HashTreeStatistics()
_root.gatherStatistics(.top, &stats)
return stats
}
}
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
import _CollectionsUtilities
extension PersistentSet: CustomStringConvertible {
/// A textual representation of this instance.
public var description: String {
_arrayDescription(for: self)
}
}
extension PersistentSet: CustomDebugStringConvertible {
/// A textual representation of this instance, suitable for debugging.
public var debugDescription: String {
_arrayDescription(
for: self, debug: true, typeName: _debugTypeName())
}
internal func _debugTypeName() -> String {
"PersistentSet<\(Element.self)>"
}
}
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2022 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
//
//===----------------------------------------------------------------------===//
extension PersistentSet: ExpressibleByArrayLiteral {
/// Creates a new set from the contents of an array literal.
///
/// Duplicate elements in the literal are allowed, but the resulting ordered
/// set will only contain the first occurrence of each.
///
/// Do not call this initializer directly. It is used by the compiler when
/// you use an array literal. Instead, create a new ordered set using an array
/// literal as its value by enclosing a comma-separated list of values in
/// square brackets. You can use an array literal anywhere an ordered set is
/// expected by the type context.
///
/// - Parameter elements: A variadic list of elements of the new ordered set.
///
/// - Complexity: O(`elements.count`) if `Element` implements
/// high-quality hashing.
public init(arrayLiteral elements: Element...) {
self.init(elements)
}
}
@@ -30,11 +30,3 @@ public struct PersistentSet<Element: Hashable> {
self.init(_root: _new, version: _new.initialVersionNumber)
}
}
extension PersistentSet {
@inlinable
public func _invariantCheck() {
_root._fullInvariantCheck(.top, _Hash(_value: 0))
}
}