Skip to content

Commit

Permalink
Added more sendable conformances
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitribouniol committed May 22, 2024
1 parent d896503 commit cb1590d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
11 changes: 7 additions & 4 deletions Sources/CodableDatastore/Datastore/Datastore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,10 @@ extension Datastore {
/// - index: The index to load from.
/// - Returns: An asynchronous sequence containing the instances matching the range of values in that sequence.
@usableFromInline
nonisolated func _load<Index: IndexRepresentation<InstanceType> & Sendable, Bound: Indexable & Sendable>(
nonisolated func _load<
Index: IndexRepresentation<InstanceType>,
Bound: Indexable
>(
_ range: some IndexRangeExpression<Bound> & Sendable,
order: RangeOrder = .ascending,
from index: KeyPath<Format, Index>
Expand Down Expand Up @@ -652,7 +655,7 @@ extension Datastore {
Value: DiscreteIndexable,
Index: RetrievableIndexRepresentation<InstanceType, Value>
>(
_ value: Index.Value,
_ value: Value,
order: RangeOrder = .ascending,
from index: KeyPath<Format, Index>
) -> some TypedAsyncSequence<InstanceType> & Sendable {
Expand All @@ -667,10 +670,10 @@ extension Datastore {
/// - index: The index to load from.
/// - Returns: The instance keyed to the specified indexed value, or nil if none are found.
public nonisolated func load<
Value,
Value: DiscreteIndexable,
Index: SingleInstanceIndexRepresentation<InstanceType, Value>
>(
_ value: Index.Value,
_ value: Value,
from index: KeyPath<Format, Index>
) async throws -> InstanceType? {
try await _load(IndexRange(only: value), from: index).first(where: { _ in true })
Expand Down
17 changes: 10 additions & 7 deletions Sources/CodableDatastore/Indexes/IndexRepresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public protocol MultipleInputIndexRepresentation<
Value
>: RetrievableIndexRepresentation {
/// The sequence of values represented in the index.
associatedtype Sequence: Swift.Sequence<Value>
associatedtype Sequence: Swift.Sequence<Value> & Sendable
}

/// An index where every value matches at most a single instance.
Expand All @@ -76,7 +76,7 @@ public protocol MultipleInputIndexRepresentation<
public struct OneToOneIndexRepresentation<
Instance: Sendable,
Value: Indexable & DiscreteIndexable
>: SingleInstanceIndexRepresentation, @unchecked Sendable {
>: SingleInstanceIndexRepresentation {
@usableFromInline
let keypath: KeyPath<Instance, Value>

Expand Down Expand Up @@ -109,7 +109,7 @@ public struct OneToOneIndexRepresentation<
public struct OneToManyIndexRepresentation<
Instance: Sendable,
Value: Indexable
>: RetrievableIndexRepresentation, @unchecked Sendable {
>: RetrievableIndexRepresentation {
@usableFromInline
let keypath: KeyPath<Instance, Value>

Expand Down Expand Up @@ -142,9 +142,9 @@ public struct OneToManyIndexRepresentation<
/// This type of index can be used if several alternative identifiers can reference an instance, and they all reside in a single property.
public struct ManyToOneIndexRepresentation<
Instance: Sendable,
Sequence: Swift.Sequence<Value>,
Sequence: Swift.Sequence<Value> & Sendable,
Value: Indexable & DiscreteIndexable
>: SingleInstanceIndexRepresentation & MultipleInputIndexRepresentation, @unchecked Sendable {
>: SingleInstanceIndexRepresentation & MultipleInputIndexRepresentation {
@usableFromInline
let keypath: KeyPath<Instance, Sequence>

Expand Down Expand Up @@ -177,9 +177,9 @@ public struct ManyToOneIndexRepresentation<
/// This type of index is common when building relationships between different instances, where one instance may be related to several others in some way.
public struct ManyToManyIndexRepresentation<
Instance: Sendable,
Sequence: Swift.Sequence<Value>,
Sequence: Swift.Sequence<Value> & Sendable,
Value: Indexable
>: MultipleInputIndexRepresentation, @unchecked Sendable {
>: MultipleInputIndexRepresentation {
@usableFromInline
let keypath: KeyPath<Instance, Sequence>

Expand Down Expand Up @@ -274,3 +274,6 @@ public struct AnyIndexRepresentation<Instance: Sendable>: Hashable, Sendable {
hasher.combine(indexRepresentation)
}
}

/// Forced KeyPath conformance since Swift 5.10 doesn't support it out of the box.
extension KeyPath: @unchecked Sendable where Root: Sendable, Value: Sendable {}

0 comments on commit cb1590d

Please sign in to comment.