generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
22 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// This source file is part of the Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
/// The property wrapper `WeakInjectable` can be used to transparently declare a weak property for a given class type. | ||
@propertyWrapper | ||
public final class WeakInjectable<Type: AnyObject> { | ||
private weak var weakReference: Type? | ||
|
||
/// Queries if the reference was already injected. | ||
public var isInjected: Bool { | ||
weakReference != nil | ||
} | ||
|
||
/// Access the underlying weak reference. | ||
/// - Note: This will crash if the underlying value wasn't injected yet. | ||
public var wrappedValue: Type { | ||
guard let weakReference else { | ||
fatalError("Failed to retrieve `\(Type.self)` object from weak reference is not yet present or not present anymore.") | ||
} | ||
|
||
return weakReference | ||
} | ||
|
||
public init() {} | ||
|
||
func inject(_ type: Type) { | ||
self.weakReference = type | ||
} | ||
} | ||
|
||
extension WeakInjectable: Sendable where Type: Sendable {} |