Skip to content

Fix noCacheKeyConformance error on make-linux-sdk #97

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

Merged
merged 1 commit into from
Apr 4, 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
39 changes: 22 additions & 17 deletions Sources/GeneratorEngine/Cache/CacheKeyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ import struct SystemPackage.FilePath
public protocol CacheKey: Encodable {
}

extension Bool: CacheKey {
/// Types that cannot be decomposed more to be hashed
protocol LeafCacheKey: CacheKey {
func hash(with hashFunction: inout some HashFunction)
}

extension Bool: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
hashFunction.update(data: self ? [1] : [0])
}
}

extension Int: CacheKey {
extension Int: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -35,7 +40,7 @@ extension Int: CacheKey {
}
}

extension Int8: CacheKey {
extension Int8: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -44,7 +49,7 @@ extension Int8: CacheKey {
}
}

extension Int16: CacheKey {
extension Int16: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -53,7 +58,7 @@ extension Int16: CacheKey {
}
}

extension Int32: CacheKey {
extension Int32: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -62,7 +67,7 @@ extension Int32: CacheKey {
}
}

extension Int64: CacheKey {
extension Int64: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -71,7 +76,7 @@ extension Int64: CacheKey {
}
}

extension UInt: CacheKey {
extension UInt: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -80,7 +85,7 @@ extension UInt: CacheKey {
}
}

extension UInt8: CacheKey {
extension UInt8: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -89,7 +94,7 @@ extension UInt8: CacheKey {
}
}

extension UInt16: CacheKey {
extension UInt16: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -98,7 +103,7 @@ extension UInt16: CacheKey {
}
}

extension UInt32: CacheKey {
extension UInt32: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -107,7 +112,7 @@ extension UInt32: CacheKey {
}
}

extension UInt64: CacheKey {
extension UInt64: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -116,7 +121,7 @@ extension UInt64: CacheKey {
}
}

extension Float: CacheKey {
extension Float: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -125,7 +130,7 @@ extension Float: CacheKey {
}
}

extension Double: CacheKey {
extension Double: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
withUnsafeBytes(of: self) {
Expand All @@ -134,7 +139,7 @@ extension Double: CacheKey {
}
}

extension String: CacheKey {
extension String: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
var t = String(reflecting: Self.self)
t.withUTF8 {
Expand All @@ -147,21 +152,21 @@ extension String: CacheKey {
}
}

extension FilePath: CacheKey {
extension FilePath: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
}
}

extension FilePath.Component: CacheKey {
extension FilePath.Component: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.string.hash(with: &hashFunction)
}
}

extension URL: CacheKey {
extension URL: LeafCacheKey {
func hash(with hashFunction: inout some HashFunction) {
String(reflecting: Self.self).hash(with: &hashFunction)
self.description.hash(with: &hashFunction)
Expand Down
4 changes: 4 additions & 0 deletions Sources/GeneratorEngine/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ extension HashEncoder {
}

mutating func encode<T>(_ value: T, forKey key: K) throws where T : Encodable {
if let leaf = value as? LeafCacheKey {
leaf.hash(with: &self.encoder.hashFunction)
return
}
guard value is CacheKey else {
throw Error.noCacheKeyConformance(T.self)
}
Expand Down