Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jun 15, 2024
1 parent 9352b0e commit 4512e89
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 62 deletions.
18 changes: 0 additions & 18 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,6 @@
"version" : "1.0.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "46989693916f56d1186bd59ac15124caef896560",
"version" : "1.3.1"
}
},
{
"identity" : "swift-benchmark",
"kind" : "remoteSourceControl",
"location" : "https://github.com/google/swift-benchmark",
"state" : {
"revision" : "8163295f6fe82356b0bcf8e1ab991645de17d096",
"version" : "0.1.2"
}
},
{
"identity" : "swift-clocks",
"kind" : "remoteSourceControl",
Expand Down
4 changes: 2 additions & 2 deletions Sources/Dependencies/Dependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ public struct Dependency<Value>: @unchecked Sendable, _HasInitialValues {
/// ```
///
/// - Parameter key: A dependency key to a specific resulting value.
public init<Key: TestDependencyKey>(
public init<Key: TestDependencyKey<Value>>(
_ key: Key.Type,
file: StaticString = #file,
fileID: StaticString = #fileID,
line: UInt = #line
) where Key.Value == Value {
) {
self.init(
\DependencyValues.[HashableType<Key>(file: file, line: line)],
file: file,
Expand Down
31 changes: 11 additions & 20 deletions Sources/Dependencies/DependencyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public struct DependencyValues: Sendable {
@TaskLocal static var currentDependency = CurrentDependency()

fileprivate var cachedValues = CachedValues()
private var storage: [ObjectIdentifier: AnySendable] = [:]
private var storage: [ObjectIdentifier: any Sendable] = [:]

/// Creates a dependency values instance.
///
Expand Down Expand Up @@ -214,13 +214,12 @@ public struct DependencyValues: Sendable {
file file: StaticString = #file,
function function: StaticString = #function,
line line: UInt = #line
) -> Key.Value where Key.Value: Sendable {
) -> Key.Value {
get {
guard let base = self.storage[ObjectIdentifier(key)]?.base,
let dependency = base as? Key.Value
guard let base = self.storage[ObjectIdentifier(key)], let dependency = base as? Key.Value
else {
let context =
self.storage[ObjectIdentifier(DependencyContextKey.self)]?.base as? DependencyContext
self.storage[ObjectIdentifier(DependencyContextKey.self)] as? DependencyContext
?? defaultContext

switch context {
Expand Down Expand Up @@ -249,7 +248,7 @@ public struct DependencyValues: Sendable {
return dependency
}
set {
self.storage[ObjectIdentifier(key)] = AnySendable(newValue)
self.storage[ObjectIdentifier(key)] = newValue
}
}

Expand Down Expand Up @@ -295,14 +294,6 @@ public struct DependencyValues: Sendable {
}
}

private struct AnySendable: @unchecked Sendable {
let base: Any
@inlinable
init<Base: Sendable>(_ base: Base) {
self.base = base
}
}

struct CurrentDependency {
var name: StaticString?
var file: StaticString?
Expand Down Expand Up @@ -352,21 +343,21 @@ private final class CachedValues: @unchecked Sendable {
}

private let lock = NSRecursiveLock()
fileprivate var cached = [CacheKey: AnySendable]()
fileprivate var cached = [CacheKey: any Sendable]()

func value<Key: TestDependencyKey>(
for key: Key.Type,
context: DependencyContext,
file: StaticString = #file,
function: StaticString = #function,
line: UInt = #line
) -> Key.Value where Key.Value: Sendable {
) -> Key.Value {
XCTFailContext.$current.withValue(XCTFailContext(file: file, line: line)) {
self.lock.lock()
defer { self.lock.unlock() }

let cacheKey = CacheKey(id: ObjectIdentifier(key), context: context)
guard let base = self.cached[cacheKey]?.base, let value = base as? Key.Value
guard let base = self.cached[cacheKey], let value = base as? Key.Value
else {
let value: Key.Value?
switch context {
Expand All @@ -378,7 +369,7 @@ private final class CachedValues: @unchecked Sendable {
value = Key.testValue
}

guard let value = value
guard let value
else {
#if DEBUG
if !DependencyValues.isSetting {
Expand Down Expand Up @@ -436,12 +427,12 @@ private final class CachedValues: @unchecked Sendable {
#endif
let value = Key.testValue
if !DependencyValues.isSetting {
self.cached[cacheKey] = AnySendable(value)
self.cached[cacheKey] = value
}
return value
}

self.cached[cacheKey] = AnySendable(value)
self.cached[cacheKey] = value
return value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,13 @@ extension DependencyValues {
///
/// See ``DependencyValues/withRandomNumberGenerator`` for more information.
public final class WithRandomNumberGenerator: @unchecked Sendable {
private var generator: _AnyRandomNumberGenerator
private let lock = NSLock()
private var generator: any RandomNumberGenerator & Sendable

public init<T: RandomNumberGenerator & Sendable>(_ generator: T) {
self.generator = _AnyRandomNumberGenerator(generator)
public init(_ generator: some RandomNumberGenerator & Sendable) {
self.generator = generator
}

public func callAsFunction<R>(_ work: (inout _AnyRandomNumberGenerator) -> R) -> R {
self.lock.lock()
defer { self.lock.unlock() }
public func callAsFunction<R>(_ work: (inout any RandomNumberGenerator & Sendable) -> R) -> R {
return work(&self.generator)
}

public class _AnyRandomNumberGenerator: RandomNumberGenerator {
private(set) public var base: RandomNumberGenerator

public init(_ base: RandomNumberGenerator) {
self.base = base
}

public func next() -> UInt64 {
self.base.next()
}
}
}
2 changes: 1 addition & 1 deletion Sources/Dependencies/Internal/RuntimeWarnings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func runtimeWarn(
let message = message()
let category = category ?? "Runtime Warning"
if _XCTIsTesting {
if let file = file, let line = line {
if let file, let line {
XCTFail(message, file: file, line: line)
} else {
XCTFail(message)
Expand Down
4 changes: 2 additions & 2 deletions Sources/DependenciesMacrosPlugin/Support.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ extension MacroExpansionContext {
}
}

extension Array where Element == String {
extension Array<String> {
func qualified(_ module: String) -> Self {
self.flatMap { [$0, "\(module).\($0)"] }
flatMap { [$0, "\(module).\($0)"] }
}
}

Expand Down

0 comments on commit 4512e89

Please sign in to comment.