Last active
September 6, 2025 18:56
-
-
Save ryanbooker/2993b71b944419ba6507cebebecd9dc8 to your computer and use it in GitHub Desktop.
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
| /// A way to handle unknown enum cases semi transparently | |
| struct Raw<T>: RawRepresentable where T: RawRepresentable { | |
| let rawValue: T.RawValue | |
| private var value: T? { | |
| .init(rawValue: rawValue) | |
| } | |
| init(rawValue: T.RawValue) { | |
| self.rawValue = rawValue | |
| } | |
| } | |
| extension Raw: Codable where T.RawValue: Codable {} | |
| extension Raw: Equatable where T.RawValue: Equatable {} | |
| extension Raw: Hashable where T.RawValue: Hashable {} | |
| extension Raw: Sendable where T.RawValue: Sendable {} | |
| /// Support pattern matching with T | |
| extension Raw where T.RawValue: Equatable { | |
| static func ~= (pattern: T, value: Self) -> Bool { | |
| pattern.rawValue == value.rawValue | |
| } | |
| } | |
| /// Support equality with T | |
| func == <T>(lhs: Raw<T>, rhs: T) -> Bool where T.RawValue: Equatable { | |
| lhs.rawValue == rhs.rawValue | |
| } | |
| /// Support equality with T | |
| func == <T>(lhs: T, rhs: Raw<T>) -> Bool where T.RawValue: Equatable { | |
| lhs.rawValue == rhs.rawValue | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment