Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created August 15, 2021 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwaldowski/2c6a285f8f7d292a155ae90fbbf1d719 to your computer and use it in GitHub Desktop.
Save zwaldowski/2c6a285f8f7d292a155ae90fbbf1d719 to your computer and use it in GitHub Desktop.
Property wrapper - Assume equal until modified; use `UIConfigurationColorTransformer` in custom structs
/// Customizes the behavior of automatically-generated `Equatable` and `Hashable` conformances.
@propertyWrapper
public struct AssumeEqualUntilModified<Wrapped> {
var modificationCount = 0
public var wrappedValue: Wrapped {
didSet {
modificationCount += 1
}
}
public init(wrappedValue: Wrapped) {
self.wrappedValue = wrappedValue
}
}
extension AssumeEqualUntilModified: Hashable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.modificationCount == 0 && rhs.modificationCount == 0
}
public func hash(into hasher: inout Hasher) {
hasher.combine(modificationCount)
}
}
struct MyConfiguration: Hashable {
var color: UIColor?
@AssumeEqualUntilModified var colorTransformer: UIConfigurationColorTransformer?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment