Skip to content

Commit

Permalink
Add Unowned<Object> struct to replace Module.Reference (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Sep 15, 2024
1 parent ba04678 commit d6757d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
18 changes: 3 additions & 15 deletions Generator/Sources/ProjectionModel/SwiftProjection+Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension SwiftProjection {
private var typeDefinitionsSorted = true

public private(set) var genericInstantiationsByDefinition = [TypeDefinition: [[TypeNode]]]()
private(set) var weakReferences: Set<Reference> = []
private var weakReferences: Set<Unowned<Module>> = []

internal init(projection: SwiftProjection, name: String, flattenNamespaces: Bool = false) {
self.projection = projection
Expand All @@ -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 }

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
}
}
16 changes: 16 additions & 0 deletions Generator/Sources/ProjectionModel/Unowned.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// A wrapper around an unowned reference to an object.
internal struct Unowned<Object>: 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
}
}

0 comments on commit d6757d3

Please sign in to comment.