Skip to content

Commit 7a02b26

Browse files
authored
Merge pull request #67061 from gottesmm/pr-a5898f4f889e53f34d0616f05e963b5306178921
[move-only] Rename moveonly_fixedqueue.swift -> moveonly_fixedsizequeue.swift and add some more to the test.
2 parents a9df3e2 + 4f24a2f commit 7a02b26

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

test/Interpreter/moveonly_fixedqueue.swift renamed to test/Interpreter/moveonly_fixedsizequeue.swift

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
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+
66
enum FixedSizeQueueError : Error {
77
case outOfSpace
88
}
99

1010
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
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

4958
func 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

6887
try main()

0 commit comments

Comments
 (0)