Skip to content

Commit 9cbc1df

Browse files
committed
Add AnyHashableSendable
1 parent d5c0298 commit 9cbc1df

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This library comes with a number of tools that make working with Swift concurren
3535
testable.
3636

3737
* [`LockIsolated`](#lockisolated)
38+
* [`AnyHashableSendable`](#anyhashablesendable)
3839
* [Streams](#streams)
3940
* [Tasks](#tasks)
4041
* [Serial execution](#serial-execution)
@@ -44,6 +45,11 @@ testable.
4445
The `LockIsolated` type helps wrap other values in an isolated context. It wraps the value in a
4546
class with a lock, which allows you to read and write the value with a synchronous interface.
4647

48+
### `AnyHashableSendable`
49+
50+
The `AnyHashableSendable` type is a type-erased wrapper like `AnyHashable` that preserves the
51+
sendability of the underlying value.
52+
4753
### Streams
4854

4955
The library comes with numerous helper APIs spread across the two Swift stream types:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// A type-erased hashable, sendable value.
2+
///
3+
/// A sendable version of `AnyHashable` that is useful in working around the limitation that an
4+
/// existential `any Hashable` does not conform to `Hashable`.
5+
public struct AnyHashableSendable: Hashable, Sendable {
6+
public let base: any Hashable & Sendable
7+
8+
/// Creates a type-erased hashable, sendable value that wraps the given instance.
9+
public init(_ base: some Hashable & Sendable) {
10+
self.base = base
11+
}
12+
13+
public static func == (lhs: Self, rhs: Self) -> Bool {
14+
func open<T: Equatable>(_ lhs: T) -> Bool {
15+
lhs == rhs as? T
16+
}
17+
return open(lhs)
18+
}
19+
20+
public func hash(into hasher: inout Hasher) {
21+
hasher.combine(base)
22+
}
23+
}

Sources/ConcurrencyExtras/Documentation.docc/ConcurrencyExtras.md

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ need to make weaker assertions due to non-determinism, but can still assert on s
236236
### Data races
237237

238238
- ``LockIsolated``
239+
- ``AnyHashableSendable``
239240

240241
### Serial execution
241242

0 commit comments

Comments
 (0)