1- // REQUIRES: executable_test
2-
31// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all)
42// RUN: %target-run-simple-swift(-O -Xfrontend -sil-verify-all)
53
4+ // REQUIRES: executable_test
5+
66enum FixedSizeQueueError : Error {
77 case outOfSpace
88}
99
1010struct FixedSizeQueue < T> : ~ Copyable {
11- private var readOffset : Int = 0
12- private var writeOffset : Int = 0
11+ private var readOffset : Int
12+ private var writeOffset : Int
1313 private var ptr : UnsafeMutableBufferPointer < T >
1414
15- init ( size: Int ) {
16- ptr = UnsafeMutableBufferPointer . allocate ( capacity: size)
15+ init ( capacity: Int ) {
16+ readOffset = 0
17+ writeOffset = 0
18+ ptr = UnsafeMutableBufferPointer . allocate ( capacity: capacity)
1719 }
1820
1921 deinit {
@@ -38,16 +40,23 @@ struct FixedSizeQueue<T> : ~Copyable {
3840 writeOffset = ( writeOffset + 1 ) % ptr. count
3941 }
4042
41- // Drain the queue... returning the value.
42- consuming func drain( ) -> UnsafeMutableBufferPointer < T > {
43+ private consuming func cleanupQueue( ) -> UnsafeMutableBufferPointer < T > {
4344 let buffer = self . ptr
4445 discard self
4546 return buffer
4647 }
48+
49+ // Drain the queue... returning the value.
50+ mutating func drain( ) -> UnsafeMutableBufferPointer < T > {
51+ let count = ptr. count
52+ let buffer = cleanupQueue ( )
53+ self = . init( capacity: count)
54+ return buffer
55+ }
4756}
4857
4958func main( ) throws {
50- var x = FixedSizeQueue < Int > ( size : 10 )
59+ var x = FixedSizeQueue < Int > ( capacity : 10 )
5160 precondition ( x. read ( ) == nil )
5261 try x. write ( 0 )
5362 try x. write ( 9 )
@@ -63,6 +72,16 @@ func main() throws {
6372 precondition ( d [ 1 ] == 9 )
6473 precondition ( d [ 2 ] == 1 )
6574 precondition ( d [ 3 ] == 3 )
75+
76+ precondition ( x. read ( ) == nil )
77+ try x. write ( 3 )
78+ try x. write ( 1 )
79+ try x. write ( 9 )
80+ try x. write ( 0 )
81+ precondition ( x. read ( ) == 3 )
82+ precondition ( x. read ( ) == 1 )
83+ precondition ( x. read ( ) == 9 )
84+ precondition ( x. read ( ) == 0 )
6685}
6786
6887try main ( )
0 commit comments