File tree Expand file tree Collapse file tree 4 files changed +18
-4
lines changed Expand file tree Collapse file tree 4 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -12,9 +12,14 @@ import Foundation
12
12
/// The `Weak` class implements the `Instance` protocol and represents a weak reference scope for dependency injection.
13
13
/// It holds a weak reference to the product instance, allowing it to be deallocated when
14
14
/// no longer strongly referenced elsewhere.
15
- class Weak < Product: AnyObject > : Instance {
15
+ class Weak < Product> : Instance {
16
16
/// The weak reference to the product instance.
17
- weak var product : Product ?
17
+ weak var object : AnyObject ?
18
+
19
+ var product : Product ? {
20
+ get { object as? Product }
21
+ set { object = newValue as AnyObject }
22
+ }
18
23
19
24
/// Initializes a new `Weak` instance.
20
25
init ( ) { }
Original file line number Diff line number Diff line change @@ -90,7 +90,7 @@ public extension Registrable {
90
90
///
91
91
/// - Returns: The modified `Registration` instance.
92
92
@discardableResult
93
- func asWeak( ) -> Self where Product : AnyObject {
93
+ func asWeak( ) -> Self {
94
94
return self . as ( Weak ( ) )
95
95
}
96
96
Original file line number Diff line number Diff line change @@ -966,7 +966,6 @@ extension ContainerTests {
966
966
967
967
#expect( object3Count == 10 )
968
968
}
969
-
970
969
}
971
970
}
972
971
Original file line number Diff line number Diff line change @@ -85,6 +85,16 @@ struct InstanceTests {
85
85
dog = nil
86
86
#expect( instance. get ( for: context) == nil )
87
87
}
88
+
89
+ @Test ( " Instance can hold structs " )
90
+ func weakStruct( ) {
91
+ let instance = Weak < StructAnimal > ( )
92
+ let context = Context . fresh ( )
93
+ let animal = StructAnimal ( )
94
+ instance. set ( animal, for: context)
95
+ instance. release ( for: context)
96
+ #expect( instance. get ( for: context) == nil )
97
+ }
88
98
}
89
99
90
100
@Suite ( " Graph " )
You can’t perform that action at this time.
0 commit comments