@@ -7,39 +7,58 @@ public protocol Copyable {
77@dynamicMemberLookup
88public protocol _CopyOnWriteValue {
99 associatedtype _COWStorage : AnyObject , Copyable
10- var _storage : _COWStorage { get set }
1110}
11+ internal protocol _COWHider {
12+ var _storage : AnyObject { get set }
13+ }
14+ typealias CopyOnWriteValue = _CopyOnWriteValue & _COWHider
1215
1316extension _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+ }
2231 }
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+ }
2647 }
27- }
2848}
2949
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 }
3860 }
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 , " " )
4362
4463 public init ( ) { }
4564}
0 commit comments