Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unowned<Object> struct to replace Module.Reference #288

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading