Created
August 15, 2021 21:03
-
-
Save zwaldowski/2c6a285f8f7d292a155ae90fbbf1d719 to your computer and use it in GitHub Desktop.
Property wrapper - Assume equal until modified; use `UIConfigurationColorTransformer` in custom structs
This file contains hidden or 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
/// 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) | |
} | |
} |
This file contains hidden or 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
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