@@ -7,39 +7,58 @@ public protocol Copyable {
7
7
@dynamicMemberLookup
8
8
public protocol _CopyOnWriteValue {
9
9
associatedtype _COWStorage : AnyObject , Copyable
10
- var _storage : _COWStorage { get set }
11
10
}
11
+ internal protocol _COWHider {
12
+ var _storage : AnyObject { get set }
13
+ }
14
+ typealias CopyOnWriteValue = _CopyOnWriteValue & _COWHider
12
15
13
16
extension _CopyOnWriteValue {
14
- public subscript< T> ( dynamicMember keypath: KeyPath < _COWStorage , T > ) -> T {
15
- get { _storage [ keyPath: keypath] }
16
- }
17
- public subscript< T> ( dynamicMember keypath: ReferenceWritableKeyPath < _COWStorage , T > ) -> T {
18
- get { _storage [ keyPath: keypath] }
19
- _modify {
20
- if !isKnownUniquelyReferenced( & _storage) { _storage = _storage. copy ( ) }
21
- yield & _storage[ keyPath: keypath]
17
+
18
+ private var _storage : _COWStorage {
19
+ get {
20
+ ( ( self as! _COWHider ) . _storage as? _COWStorage ) . unsafelyUnwrapped
21
+ }
22
+ _modify {
23
+ var c = ( self as! _COWHider ) . _storage as! _COWStorage
24
+ yield & c
25
+ }
26
+ set {
27
+ var c = ( self as! _COWHider )
28
+ c. _storage = newValue
29
+ self = c as! Self
30
+ }
22
31
}
23
- set {
24
- if !isKnownUniquelyReferenced( & _storage) { _storage = _storage. copy ( ) }
25
- _storage [ keyPath: keypath] = newValue
32
+
33
+ public subscript< T> ( dynamicMember keypath: KeyPath < _COWStorage , T > ) -> T {
34
+ get { _storage [ keyPath: keypath] }
35
+ }
36
+
37
+ public subscript< T> ( dynamicMember keypath: ReferenceWritableKeyPath < _COWStorage , T > ) -> T {
38
+ get { _storage [ keyPath: keypath] }
39
+ _modify {
40
+ if !isKnownUniquelyReferenced( & _storage) { _storage = _storage. copy ( ) }
41
+ yield & _storage[ keyPath: keypath]
42
+ }
43
+ set {
44
+ if !isKnownUniquelyReferenced( & _storage) { _storage = _storage. copy ( ) }
45
+ _storage [ keyPath: keypath] = newValue
46
+ }
26
47
}
27
- }
28
48
}
29
49
30
- public struct TestObj : _CopyOnWriteValue {
31
-
32
- public final class _COWStorage : Copyable {
33
- public var propOne : Int
34
- public var propTwo : String
35
- public func copy( ) -> Self {
36
- print ( " copying " )
37
- return Self ( propOne, propTwo)
50
+ public struct TestObj : CopyOnWriteValue {
51
+ public final class _COWStorage : Copyable {
52
+ public var propOne : Int
53
+ public var propTwo : String
54
+ public func copy( ) -> Self {
55
+ print ( " copying " )
56
+ return Self ( propOne, propTwo)
57
+ }
58
+
59
+ init ( _ p1: Int , _ p2: String ) { self . propOne = p1; self . propTwo = p2 }
38
60
}
39
-
40
- init ( _ p1: Int , _ p2: String ) { self . propOne = p1; self . propTwo = p2 }
41
- }
42
- public var _storage = _COWStorage ( 0 , " " )
61
+ var _storage : AnyObject = _COWStorage ( 0 , " " )
43
62
44
63
public init ( ) { }
45
64
}
0 commit comments