-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `AnyHashableSendable` * conformances * Tests * Update AnyHashableSendable.swift * Update AnyHashableSendableTests.swift * Update AnyHashableSendable.swift
- Loading branch information
1 parent
d5c0298
commit 6054df6
Showing
4 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/// A type-erased hashable, sendable value. | ||
/// | ||
/// A sendable version of `AnyHashable` that is useful in working around the limitation that an | ||
/// existential `any Hashable` does not conform to `Hashable`. | ||
public struct AnyHashableSendable: Hashable, Sendable { | ||
public let base: any Hashable & Sendable | ||
|
||
/// Creates a type-erased hashable, sendable value that wraps the given instance. | ||
public init(_ base: some Hashable & Sendable) { | ||
if let base = base as? AnyHashableSendable { | ||
self = base | ||
} else { | ||
self.base = base | ||
} | ||
} | ||
|
||
public static func == (lhs: Self, rhs: Self) -> Bool { | ||
AnyHashable(lhs.base) == AnyHashable(rhs.base) | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(base) | ||
} | ||
} | ||
|
||
extension AnyHashableSendable: CustomDebugStringConvertible { | ||
public var debugDescription: String { | ||
"AnyHashableSendable(" + String(reflecting: base) + ")" | ||
} | ||
} | ||
|
||
extension AnyHashableSendable: CustomReflectable { | ||
public var customMirror: Mirror { | ||
Mirror(self, children: ["value": base]) | ||
} | ||
} | ||
|
||
extension AnyHashableSendable: CustomStringConvertible { | ||
public var description: String { | ||
String(describing: base) | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
Tests/ConcurrencyExtrasTests/AnyHashableSendableTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import ConcurrencyExtras | ||
import XCTest | ||
|
||
final class AnyHashableSendableTests: XCTestCase { | ||
func testBasics() { | ||
XCTAssertEqual(AnyHashableSendable(1), AnyHashableSendable(1)) | ||
XCTAssertNotEqual(AnyHashableSendable(1), AnyHashableSendable(2)) | ||
|
||
func make(_ base: some Hashable & Sendable) -> AnyHashableSendable { | ||
AnyHashableSendable(base) | ||
} | ||
|
||
let flat = make(1) | ||
let nested = make(flat) | ||
|
||
XCTAssertEqual(flat, nested) | ||
} | ||
} |