1212
1313struct NCContainer : ~ Copyable {
1414 let ptr : UnsafeRawBufferPointer
15- init ( _ ptr: UnsafeRawBufferPointer ) {
15+ let c : Int
16+ init ( _ ptr: UnsafeRawBufferPointer , _ c: Int ) {
1617 self . ptr = ptr
18+ self . c = c
1719 }
1820}
1921
2022struct NEContainer : ~ Escapable {
2123 let ptr : UnsafeRawBufferPointer
24+ let c : Int
2225 @_unsafeNonescapableResult
23- init ( _ ptr: UnsafeRawBufferPointer ) {
26+ init ( _ ptr: UnsafeRawBufferPointer , _ c : Int ) {
2427 self . ptr = ptr
28+ self . c = c
2529 }
2630}
2731
2832struct View : ~ Escapable {
2933 let ptr : UnsafeRawBufferPointer
34+ let c : Int
3035 @_unsafeNonescapableResult
31- init ( _ ptr: UnsafeRawBufferPointer ) {
36+ init ( _ ptr: UnsafeRawBufferPointer , _ c : Int ) {
3237 self . ptr = ptr
38+ self . c = c
3339 }
3440 init ( _ otherBV: borrowing View ) {
3541 self . ptr = otherBV. ptr
42+ self . c = otherBV. c
3643 }
3744 init ( _ k: borrowing NCContainer ) {
3845 self . ptr = k. ptr
46+ self . c = k. c
3947 }
4048 init ( _ k: consuming NEContainer ) {
4149 self . ptr = k. ptr
50+ self . c = k. c
4251 }
4352}
4453
4554struct MutableView : ~ Copyable, ~ Escapable {
4655 let ptr : UnsafeRawBufferPointer
56+ let c : Int
4757 @_unsafeNonescapableResult
48- init ( _ ptr: UnsafeRawBufferPointer ) {
58+ init ( _ ptr: UnsafeRawBufferPointer , _ c : Int ) {
4959 self . ptr = ptr
60+ self . c = c
5061 }
5162 init ( _ otherBV: borrowing View ) {
5263 self . ptr = otherBV. ptr
64+ self . c = otherBV. c
5365 }
5466 init ( _ k: borrowing NCContainer ) {
5567 self . ptr = k. ptr
68+ self . c = k. c
5669 }
5770 init ( _ k: consuming NEContainer ) {
5871 self . ptr = k. ptr
72+ self . c = k. c
5973 }
6074}
6175
@@ -72,24 +86,24 @@ func getConsumingView(_ x: consuming NEContainer) -> _consume(x) View {
7286}
7387
7488func getConsumingView( _ x: consuming View ) -> _consume( x ) View {
75- return View ( x. ptr)
89+ return View ( x. ptr, x . c )
7690}
7791
7892func getBorrowingView( _ x: borrowing View ) -> _borrow( x ) View {
79- return View ( x. ptr)
93+ return View ( x. ptr, x . c )
8094}
8195
8296func getBorrowingView( _ x: borrowing NCContainer ) -> _borrow( x ) View {
83- return View ( x. ptr)
97+ return View ( x. ptr, x . c )
8498}
8599
86100func getBorrowingView( _ x: borrowing NEContainer ) -> _borrow( x ) View {
87- return View ( x. ptr)
101+ return View ( x. ptr, x . c )
88102}
89103
90104func test1( _ a: Array < Int > ) {
91105 a. withUnsafeBytes {
92- var x = NEContainer ( $0)
106+ var x = NEContainer ( $0, a . count )
93107 mutate ( & x)
94108 let view = getConsumingView ( x)
95109 let newView = View ( view)
@@ -100,7 +114,7 @@ func test1(_ a: Array<Int>) {
100114
101115func test2( _ a: Array < Int > ) {
102116 a. withUnsafeBytes {
103- var x = NCContainer ( $0)
117+ var x = NCContainer ( $0, a . count )
104118 mutate ( & x)
105119 let view = getBorrowingView ( x)
106120 use ( view)
@@ -110,7 +124,7 @@ func test2(_ a: Array<Int>) {
110124
111125func test3( _ a: Array < Int > ) {
112126 a. withUnsafeBytes {
113- var x = View ( $0)
127+ var x = View ( $0, a . count )
114128 mutate ( & x)
115129 let view = getConsumingView ( x)
116130 use ( view)
@@ -123,7 +137,7 @@ func test3(_ a: Array<Int>) {
123137// def-use chain involving a stack temporary
124138func test4(_ a: Array<Int>) {
125139 a.withUnsafeBytes {
126- var x = NCContainer($0)
140+ var x = NCContainer($0, a.count )
127141 mutate(&x)
128142 let view = MutableView(x)
129143 use(view)
@@ -134,7 +148,7 @@ func test4(_ a: Array<Int>) {
134148
135149func test5( _ a: Array < Int > ) {
136150 a. withUnsafeBytes {
137- let x = NEContainer ( $0)
151+ let x = NEContainer ( $0, a . count )
138152 let view = getBorrowingView ( x)
139153 let anotherView = getConsumingView ( view)
140154 use ( anotherView)
@@ -144,7 +158,7 @@ func test5(_ a: Array<Int>) {
144158func test6( _ a: Array < Int > ) {
145159 var p : View ?
146160 a. withUnsafeBytes {
147- var x = NCContainer ( $0)
161+ var x = NCContainer ( $0, a . count )
148162 mutate ( & x)
149163 let view = View ( x)
150164 p = view
@@ -153,7 +167,7 @@ func test6(_ a: Array<Int>) {
153167}
154168
155169func test7( _ a: UnsafeRawBufferPointer ) {
156- var x = NEContainer ( a)
170+ var x = NEContainer ( a, a . count )
157171 do {
158172 let view = getBorrowingView ( x)
159173 use ( view)
@@ -163,10 +177,36 @@ func test7(_ a: UnsafeRawBufferPointer) {
163177
164178func test8( _ a: Array < Int > ) {
165179 a. withUnsafeBytes {
166- var x = NEContainer ( $0)
180+ var x = NEContainer ( $0, a . count )
167181 mutate ( & x)
168182 let view = MutableView ( x)
169183 use ( view)
170184 consume ( view)
171185 }
172186}
187+
188+ struct Wrapper : ~ Escapable {
189+ var _view : View
190+ var view : View {
191+ _read {
192+ yield _view
193+ }
194+ _modify {
195+ yield & _view
196+ }
197+ }
198+ init ( _ view: consuming View ) {
199+ self . _view = view
200+ }
201+ }
202+
203+ func test9( ) {
204+ let a = [ Int] ( repeating: 0 , count: 4 )
205+ a. withUnsafeBytes {
206+ let view = View ( $0, a. count)
207+ var c = Wrapper ( view)
208+ use ( c. view)
209+ mutate ( & c. view)
210+ }
211+ }
212+
0 commit comments