generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Sendable SharedRepository (#14)
# Introduce Sendable SharedRepository ## ♻️ Current situation & Problem Until now, `Sendable` conformance for values stored in the `ValueRepository` (the builtin `SharedRepository` implementation) never checked if values do indeed conform to Sendable. Implementors were always required ## ⚙️ Release Notes * Introduced new `SendableSharedRepository` and `SendableValueRepository` variant. ### Breaking Changes * Computed KnowledgeSources now need to specify the type of the SharedRepository via an associated type. * The `HeapRepository` was removed. ## 📚 Documentation Documentation was updated. ## ✅ Testing Additional unit testing was added for the new sendable shared repository variants. ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
Showing
15 changed files
with
705 additions
and
476 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
50 changes: 0 additions & 50 deletions
50
Sources/SpeziFoundation/SharedRepository/Builtin/BuiltinRepository.swift
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
Sources/SpeziFoundation/SharedRepository/Builtin/HeapRepository.swift
This file was deleted.
Oops, something went wrong.
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
56 changes: 56 additions & 0 deletions
56
Sources/SpeziFoundation/SharedRepository/Builtin/SendableValueRepository.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,56 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
|
||
/// A Sendable Shared Repository. | ||
public struct SendableValueRepository<Anchor> { | ||
private var storage: [ObjectIdentifier: AnyRepositoryValue & Sendable] = [:] | ||
|
||
|
||
/// Initializes an empty shared repository. | ||
public init() {} | ||
} | ||
|
||
|
||
extension SendableValueRepository: SendableSharedRepository { | ||
public func get<Source: KnowledgeSource<Anchor>>(_ source: Source.Type) -> Source.Value? where Source.Value: Sendable { | ||
(storage[ObjectIdentifier(source)] as? RepositoryValue<Source>)?.value | ||
} | ||
|
||
public mutating func set<Source: KnowledgeSource<Anchor>>(_ source: Source.Type, value newValue: Source.Value?) where Source.Value: Sendable { | ||
storage[ObjectIdentifier(source)] = newValue.map { RepositoryValue<Source>($0) } | ||
} | ||
|
||
public func collect<Value>(allOf type: Value.Type) -> [Value] { | ||
storage.values.compactMap { value in | ||
value.anyValue as? Value | ||
} | ||
} | ||
} | ||
|
||
|
||
extension SendableValueRepository: Collection { | ||
public typealias Index = Dictionary<ObjectIdentifier, AnyRepositoryValue & Sendable>.Index | ||
|
||
public var startIndex: Index { | ||
storage.values.startIndex | ||
} | ||
|
||
public var endIndex: Index { | ||
storage.values.endIndex | ||
} | ||
|
||
public func index(after index: Index) -> Index { | ||
storage.values.index(after: index) | ||
} | ||
|
||
|
||
public subscript(position: Index) -> AnyRepositoryValue & Sendable { | ||
storage.values[position] | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
Sources/SpeziFoundation/SharedRepository/Builtin/SimpleRepositoryValue.swift
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.