From 00ae83df1ed4f68eb75fcb03b596e891a88e2506 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Sat, 24 Sep 2022 17:02:15 -0700 Subject: [PATCH] [PersistentSet] Add ExpressibleByArrayLiteral, Codable, Custom[Debug]StringConvertible, CustomReflectable conformances --- .../PersistentSet/PersistentSet+Codable.swift | 46 +++++++++++++++++++ .../PersistentSet+CustomReflectable.swift} | 5 +- .../PersistentSet+Debugging.swift | 37 +++++++++++++++ .../PersistentSet+Descriptions.swift | 31 +++++++++++++ ...sistentSet+ExpressibleByArrayLiteral.swift | 31 +++++++++++++ .../PersistentSet/PersistentSet.swift | 8 ---- 6 files changed, 148 insertions(+), 10 deletions(-) create mode 100644 Sources/PersistentCollections/PersistentSet/PersistentSet+Codable.swift rename Sources/PersistentCollections/{PersistentDictionary/PersistentDictionary+CustomReflectible.swift => PersistentSet/PersistentSet+CustomReflectable.swift} (75%) create mode 100644 Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift create mode 100644 Sources/PersistentCollections/PersistentSet/PersistentSet+Descriptions.swift create mode 100644 Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Codable.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Codable.swift new file mode 100644 index 00000000..d4a41b77 --- /dev/null +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Codable.swift @@ -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) + } + } + } +} diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+CustomReflectible.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+CustomReflectable.swift similarity index 75% rename from Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+CustomReflectible.swift rename to Sources/PersistentCollections/PersistentSet/PersistentSet+CustomReflectable.swift index fde019a9..1a22473c 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+CustomReflectible.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+CustomReflectable.swift @@ -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) } } diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift new file mode 100644 index 00000000..59363858 --- /dev/null +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Debugging.swift @@ -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 + } +} diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+Descriptions.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+Descriptions.swift new file mode 100644 index 00000000..13a3dd7f --- /dev/null +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+Descriptions.swift @@ -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)>" + } +} diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift new file mode 100644 index 00000000..b5b45297 --- /dev/null +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet+ExpressibleByArrayLiteral.swift @@ -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) + } +} diff --git a/Sources/PersistentCollections/PersistentSet/PersistentSet.swift b/Sources/PersistentCollections/PersistentSet/PersistentSet.swift index 428edf5e..ca3bd732 100644 --- a/Sources/PersistentCollections/PersistentSet/PersistentSet.swift +++ b/Sources/PersistentCollections/PersistentSet/PersistentSet.swift @@ -30,11 +30,3 @@ public struct PersistentSet { self.init(_root: _new, version: _new.initialVersionNumber) } } - - -extension PersistentSet { - @inlinable - public func _invariantCheck() { - _root._fullInvariantCheck(.top, _Hash(_value: 0)) - } -}