Last active
June 3, 2020 15:24
-
-
Save maximkrouk/9e7b4786a94edf474383d16ee2a4e26a to your computer and use it in GitHub Desktop.
Identifiable wrapper for hashable values
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
@dynamicMemberLookup | |
@propertyWrapper | |
public struct IdentifiableBox<Content: Hashable>: Identifiable { | |
public var id: Int { content.hashValue } | |
public var content: Content | |
@inlinable | |
public var wrappedValue: Content { | |
get { content } | |
set { content = newValue } | |
} | |
@inlinable | |
public var projectedValue: Int { id } | |
@inlinable | |
public init(wrappedValue: Content) { | |
self.init(wrappedValue) | |
} | |
@inlinable | |
public init(_ content: Content) { | |
self.content = content | |
} | |
@inlinable | |
public subscript<T>(dynamicMember keyPath: KeyPath<Content, T>) -> T { | |
get { content[keyPath: keyPath] } | |
} | |
@inlinable | |
public subscript<T>(dynamicMember keyPath: WritableKeyPath<Content, T>) -> T { | |
get { content[keyPath: keyPath] } | |
set { content[keyPath: keyPath] = newValue } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
See more:
Back to index