From 2896e67d87b08a1021101b869757be38e7bd3023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Kali=C5=84ski?= Date: Mon, 17 Jul 2023 14:41:54 +0200 Subject: [PATCH] Add retainContainer method --- FormatTool/Package.swift | 2 +- Sources/MQDo/Container/Features.swift | 16 ++++++++++++++ .../Container/Internal/FeaturesProxy.swift | 22 +++++++++++++++++++ .../Container/Internal/RootFeatures.swift | 8 +++++++ .../Container/Internal/ScopedFeatures.swift | 8 +++++++ Sources/MQDummy/Container/DummyFeatures.swift | 8 +++++++ 6 files changed, 63 insertions(+), 1 deletion(-) diff --git a/FormatTool/Package.swift b/FormatTool/Package.swift index aad66a1..ea7ede3 100644 --- a/FormatTool/Package.swift +++ b/FormatTool/Package.swift @@ -7,7 +7,7 @@ let package = Package( dependencies: [ .package( url: "https://github.com/apple/swift-format.git", - exact: "508.0.0" + exact: "508.0.1" ), ], targets: [ diff --git a/Sources/MQDo/Container/Features.swift b/Sources/MQDo/Container/Features.swift index 82598d1..7d7f6a4 100644 --- a/Sources/MQDo/Container/Features.swift +++ b/Sources/MQDo/Container/Features.swift @@ -1,5 +1,10 @@ public protocol Features: Sendable { + @Sendable func retainContainer( + file: StaticString, + line: UInt + ) throws -> FeaturesContainer + @Sendable func branch( _ scope: Scope.Type, context: Scope.Context, @@ -60,6 +65,17 @@ public protocol Features: Sendable { extension Features { + @_transparent + @Sendable func retainContainer( + file: StaticString = #file, + line: UInt = #line + ) throws -> FeaturesContainer { + try self.retainContainer( + file: file, + line: line + ) + } + @_transparent @Sendable public func require( _ scope: Scope.Type, diff --git a/Sources/MQDo/Container/Internal/FeaturesProxy.swift b/Sources/MQDo/Container/Internal/FeaturesProxy.swift index b2c81bc..e4b5737 100644 --- a/Sources/MQDo/Container/Internal/FeaturesProxy.swift +++ b/Sources/MQDo/Container/Internal/FeaturesProxy.swift @@ -14,6 +14,28 @@ internal struct FeaturesProxy { extension FeaturesProxy: Features { + @Sendable internal func retainContainer( + file: StaticString, + line: UInt + ) throws -> FeaturesContainer { + if let container: FeaturesContainer = self.container { + return container + } + else { + throw + FeaturesContainerUnavailable + .error( + file: file, + line: line + ) + .asRuntimeWarning( + message: "Using detached features container. This is likely a memory issue.", + file: file, + line: line + ) + } + } + @_transparent @Sendable internal func require( _ scope: Scope.Type, diff --git a/Sources/MQDo/Container/Internal/RootFeatures.swift b/Sources/MQDo/Container/Internal/RootFeatures.swift index acc20fd..03a7cd0 100644 --- a/Sources/MQDo/Container/Internal/RootFeatures.swift +++ b/Sources/MQDo/Container/Internal/RootFeatures.swift @@ -21,6 +21,14 @@ extension RootFeatures: @unchecked Sendable {} extension RootFeatures: FeaturesContainer { + @_transparent + @Sendable internal func retainContainer( + file: StaticString, + line: UInt + ) throws -> FeaturesContainer { + self + } + @_transparent @Sendable internal func require( _ scope: Scope.Type, diff --git a/Sources/MQDo/Container/Internal/ScopedFeatures.swift b/Sources/MQDo/Container/Internal/ScopedFeatures.swift index 19aa5d7..c6c8390 100644 --- a/Sources/MQDo/Container/Internal/ScopedFeatures.swift +++ b/Sources/MQDo/Container/Internal/ScopedFeatures.swift @@ -28,6 +28,14 @@ extension ScopedFeatures: @unchecked Sendable {} extension ScopedFeatures: FeaturesContainer { + @_transparent + @Sendable internal func retainContainer( + file: StaticString, + line: UInt + ) throws -> FeaturesContainer { + self + } + @Sendable internal func require( _ scope: RequestedScope.Type, file: StaticString, diff --git a/Sources/MQDummy/Container/DummyFeatures.swift b/Sources/MQDummy/Container/DummyFeatures.swift index 3992772..1701853 100644 --- a/Sources/MQDummy/Container/DummyFeatures.swift +++ b/Sources/MQDummy/Container/DummyFeatures.swift @@ -20,6 +20,14 @@ extension DummyFeatures: @unchecked Sendable {} extension DummyFeatures: FeaturesContainer { + @_transparent + @Sendable public func retainContainer( + file: StaticString, + line: UInt + ) throws -> FeaturesContainer { + self + } + @Sendable public func context( for scope: Scope.Type, file: StaticString,