From d6757d375c428ba22c0695b899c2e9469c65e9f0 Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Sun, 15 Sep 2024 09:03:39 -0400 Subject: [PATCH] Add Unowned struct to replace Module.Reference (#288) --- .../SwiftProjection+Module.swift | 18 +++--------------- .../Sources/ProjectionModel/Unowned.swift | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 Generator/Sources/ProjectionModel/Unowned.swift diff --git a/Generator/Sources/ProjectionModel/SwiftProjection+Module.swift b/Generator/Sources/ProjectionModel/SwiftProjection+Module.swift index eed0cd8..4c605eb 100644 --- a/Generator/Sources/ProjectionModel/SwiftProjection+Module.swift +++ b/Generator/Sources/ProjectionModel/SwiftProjection+Module.swift @@ -14,7 +14,7 @@ extension SwiftProjection { private var typeDefinitionsSorted = true public private(set) var genericInstantiationsByDefinition = [TypeDefinition: [[TypeNode]]]() - private(set) var weakReferences: Set = [] + private var weakReferences: Set> = [] internal init(projection: SwiftProjection, name: String, flattenNamespaces: Bool = false) { self.projection = projection @@ -31,7 +31,7 @@ extension SwiftProjection { return lazySortedTypeDefinitions } - public var references: [Module] { weakReferences.map { $0.target } } + public var references: [Module] { weakReferences.map { $0.object } } public var isEmpty: Bool { lazySortedTypeDefinitions.isEmpty } @@ -56,7 +56,7 @@ extension SwiftProjection { } public func addReference(_ other: Module) { - weakReferences.insert(Reference(target: other)) + weakReferences.insert(Unowned(other)) } public func getNamespaceModuleName(namespace: String) -> String { @@ -89,17 +89,5 @@ extension SwiftProjection { } return namespacedType.namespace ?? "" } - - struct Reference: Hashable { - unowned var target: Module - - func hash(into hasher: inout Hasher) { - hasher.combine(ObjectIdentifier(target)) - } - - static func == (lhs: Module.Reference, rhs: Module.Reference) -> Bool { - lhs.target === rhs.target - } - } } } \ No newline at end of file diff --git a/Generator/Sources/ProjectionModel/Unowned.swift b/Generator/Sources/ProjectionModel/Unowned.swift new file mode 100644 index 0000000..d01b5d5 --- /dev/null +++ b/Generator/Sources/ProjectionModel/Unowned.swift @@ -0,0 +1,16 @@ +/// A wrapper around an unowned reference to an object. +internal struct Unowned: Hashable where Object: AnyObject { + unowned var object: Object + + public init(_ object: Object) { + self.object = object + } + + public func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(object)) + } + + public static func == (lhs: Self, rhs: Self) -> Bool { + lhs.object === rhs.object + } +} \ No newline at end of file