mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
We can not tell the compiler that we can safely return `sending` results, because fetched values are in the same region as the `db` argument, according to SE-0414 (https://github.com/swiftlang/swift-evolution/blob/main/proposals/0414-region-based-isolation.md#rules-for-merging-isolation-regions). ``` class NotSendable { init() { } } func fetchNotSendable(_ db: Database) -> NotSendable { fatalError() } func notOK<T>(_ make: (Database) -> sending T) { } func ok<T>(_ make: () -> sending T) { } func usage() { ok { NotSendable() } // Returning a task-isolated 'NotSendable' value as a 'sending' result // risks causing data races. notOK { db in fetchNotSendable(db) } } ``` Various attempts at modifying all fetching methods so that they have a `sending` return value have failed.
29 lines
961 B
Swift
29 lines
961 B
Swift
extension ValueReducers {
|
|
/// A `ValueReducer` that perform database fetches.
|
|
public struct Fetch<Value: Sendable>: ValueReducer {
|
|
public struct _Fetcher: _ValueReducerFetcher {
|
|
let _fetch: @Sendable (Database) throws -> Value
|
|
|
|
public func fetch(_ db: Database) throws -> Value {
|
|
assert(db.isInsideTransaction, "Fetching in a non-isolated way is illegal")
|
|
return try _fetch(db)
|
|
}
|
|
}
|
|
|
|
private let _fetch: @Sendable (Database) throws -> Value
|
|
|
|
/// Creates a reducer which passes raw fetched values through.
|
|
init(fetch: @escaping @Sendable (Database) throws -> Value) {
|
|
self._fetch = fetch
|
|
}
|
|
|
|
public func _makeFetcher() -> _Fetcher {
|
|
_Fetcher(_fetch: _fetch)
|
|
}
|
|
|
|
public func _value(_ fetched: Value) -> Value? {
|
|
fetched
|
|
}
|
|
}
|
|
}
|