Created
October 31, 2021 08:27
-
-
Save kvs-coder/d84f1288b6d05561e1c30868f3bd1680 to your computer and use it in GitHub Desktop.
Reflection Hashable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyHashable: Hashable { | |
var dumped: String { | |
var str = String() | |
dump(self, to: &str) | |
return str | |
} | |
static func == (lhs: MyHashable, rhs: MyHashable) -> Bool { | |
lhs.dumped == rhs.dumped | |
} | |
func hash(into hasher: inout Hasher) { | |
Mirror(reflecting: self).children | |
.map { $0.value } | |
.withUnsafeBytes { hasher.combine(bytes: $0) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment