-
Notifications
You must be signed in to change notification settings - Fork 48
Add equivalency protocols and implementations (1/3) #568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maxg-square
wants to merge
8
commits into
main
Choose a base branch
from
maxg/cache_1_equivalency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a63d20d
Add equivalency protocols and implementations
maxg-square 0e78167
Fix testkey
maxg-square 8a39fce
include other TestKeys
maxg-square deb85cb
Cleanup
maxg-square 1c28569
Merge branch 'main' into maxg/cache_1_equivalency
maxg-square 54a8a25
Merge branch 'main' into maxg/cache_1_equivalency
maxg-square d4418a8
PR feedback cleanup
maxg-square ebcb1b5
Merge branch 'main' into maxg/cache_1_equivalency
maxg-square File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
BlueprintUI/Sources/CrossLayoutCaching/AnyCrossLayoutCacheable.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import Foundation | ||
|
|
||
| /// Type eraser for CrossLayoutCacheable. | ||
| public struct AnyCrossLayoutCacheable: CrossLayoutCacheable { | ||
|
|
||
| let base: Any | ||
|
|
||
| public init(_ value: some CrossLayoutCacheable) { | ||
| base = value | ||
| } | ||
|
|
||
| public func isCacheablyEquivalent(to other: AnyCrossLayoutCacheable?, in context: CrossLayoutCacheableContext) -> Bool { | ||
| guard let base = (base as? any CrossLayoutCacheable) else { return false } | ||
| return base.isCacheablyEquivalent(to: other?.base as? CrossLayoutCacheable, in: context) | ||
| } | ||
|
|
||
| } | ||
|
|
44 changes: 44 additions & 0 deletions
44
BlueprintUI/Sources/CrossLayoutCaching/CrossLayoutCacheable.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import Foundation | ||
|
|
||
| /// Protocol that allows a value to be cached between layout passes. | ||
| public protocol CrossLayoutCacheable { | ||
|
|
||
| /// Allows a type to express cacheability of a value within certain contexts. For example, an Environment that represents dark mode would be equivalent to an Environment that represents light mode in a `elementSizing` context, but not in `all` contexts. | ||
| /// - Parameters: | ||
| /// - other: The instance of the type being compared against. | ||
| /// - context: The context to compare within. | ||
| /// - Returns: Whether or not the other instance is equivalent in the specified context. | ||
| /// - Note: Equivilancy within a given context is transitive – that is, if value A is equivalent to value B in a given context, and B is equivalent to C in that same context, A will be considered equivalent to C with that context. | ||
| func isCacheablyEquivalent(to other: Self?, in context: CrossLayoutCacheableContext) -> Bool | ||
|
|
||
| } | ||
|
|
||
| extension CrossLayoutCacheable { | ||
|
|
||
| /// Convenience equivalency check passing in .all for context. | ||
| /// - other: The instance of the type being compared against. | ||
| /// - Returns: Whether or not the other instance is equivalent in all contexts. | ||
| public func isCacheablyEquivalent(to other: Self?) -> Bool { | ||
| isCacheablyEquivalent(to: other, in: .all) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| extension CrossLayoutCacheable { | ||
|
|
||
| // Allows comparison between types which may or may not be equivalent. | ||
| @_disfavoredOverload | ||
| public func isCacheablyEquivalent(to other: (any CrossLayoutCacheable)?, in context: CrossLayoutCacheableContext) -> Bool { | ||
| isCacheablyEquivalent(to: other as? Self, in: context) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Default implementation that always returns strict equivalency. | ||
| extension CrossLayoutCacheable where Self: Equatable { | ||
|
|
||
| public func isCacheablyEquivalent(to other: Self?, in context: CrossLayoutCacheableContext) -> Bool { | ||
| self == other | ||
| } | ||
|
|
||
| } |
14 changes: 14 additions & 0 deletions
14
BlueprintUI/Sources/CrossLayoutCaching/CrossLayoutCacheableContext.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import Foundation | ||
|
|
||
| // A context in which to evaluate whether or not a value is cacheable. | ||
| public enum CrossLayoutCacheableContext: Hashable, Sendable, CaseIterable { | ||
|
|
||
| /// The two values are identicial in every respect that could affect displayed output. | ||
| case all | ||
|
|
||
| // More fine-grained contexts: | ||
|
|
||
| /// The two values are equivalent in all aspects that would affect the size of the element. | ||
| /// - Warning:Non-obvious things may affect element-sizing – for example, setting a time zone may seem like something that would only affect date calculations, but can result in different text being displayed, and therefore affect sizing. Consider carefully whether you are truly affecting sizing or not. | ||
| case elementSizing | ||
| } |
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
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
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
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd still prefer to see a default implementation here, but it's not a blocker.