1
- // REQUIRES: executable_test
2
-
3
1
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all)
4
2
// RUN: %target-run-simple-swift(-O -Xfrontend -sil-verify-all)
5
3
4
+ // REQUIRES: executable_test
5
+
6
6
enum FixedSizeQueueError : Error {
7
7
case outOfSpace
8
8
}
9
9
10
10
struct 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
13
13
private var ptr : UnsafeMutableBufferPointer < T >
14
14
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)
17
19
}
18
20
19
21
deinit {
@@ -38,16 +40,23 @@ struct FixedSizeQueue<T> : ~Copyable {
38
40
writeOffset = ( writeOffset + 1 ) % ptr. count
39
41
}
40
42
41
- // Drain the queue... returning the value.
42
- consuming func drain( ) -> UnsafeMutableBufferPointer < T > {
43
+ private consuming func cleanupQueue( ) -> UnsafeMutableBufferPointer < T > {
43
44
let buffer = self . ptr
44
45
discard self
45
46
return buffer
46
47
}
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
+ }
47
56
}
48
57
49
58
func main( ) throws {
50
- var x = FixedSizeQueue < Int > ( size : 10 )
59
+ var x = FixedSizeQueue < Int > ( capacity : 10 )
51
60
precondition ( x. read ( ) == nil )
52
61
try x. write ( 0 )
53
62
try x. write ( 9 )
@@ -63,6 +72,16 @@ func main() throws {
63
72
precondition ( d [ 1 ] == 9 )
64
73
precondition ( d [ 2 ] == 1 )
65
74
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 )
66
85
}
67
86
68
87
try main ( )
0 commit comments