You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/2-foundations-of-rust/3-advanced-syntax/4-ring-buffer/src/main.rs
+70-16Lines changed: 70 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
-
/// One way to implement a queue is to use a linked list; however, that requires a lot of dynamic memory manipulation to add/remove individual items.
2
-
/// A more low-level approach is to use a circular buffer: the compromise is that the capacity of the queue is then "fixed". For a background on circular buffers,
3
-
/// you can consult https://en.wikipedia.org/wiki/Circular_buffer
1
+
//! One way to implement a queue is to use a linked list; however, that requires a lot of dynamic memory manipulation to add/remove individual items.
2
+
//! A more low-level approach is to use a circular buffer: the compromise is that the capacity of the queue is then "fixed". For a background on circular buffers,
3
+
//! you can consult https://en.wikipedia.org/wiki/Circular_buffer
4
4
5
5
// A partial implementation is provided below; please finish it and add some more methods; please remember to run 'cargo fmt' and 'cargo clippy' after
6
6
// every step to get feedback from the rust compiler!
@@ -15,19 +15,23 @@
15
15
// 4) in a queue that has size N, how many elements can be stored at one time? (test your answer experimentally)
16
16
17
17
// 5) EXTRA EXERCISES:
18
-
// - add a method "has_room" so that "queue.has_room()" is true if and only if writing to the queue will succeed
19
-
// - add a method "peek" so that "queue.peek()" returns the same thing as "queue.read()", but leaves the element in the queue
18
+
// - implement the method "has_room" so that "queue.has_room()" is true if and only if writing to the queue will succeed
19
+
// - implement the method "peek" so that "queue.peek()" returns the same thing as "queue.read()", but leaves the element in the queue
20
+
// - eliminate edge cases of the data structure (HINT: what is the behaviour of (n % 0))
21
+
// - test your implementation by running 'cargo test'
22
+
23
+
constRING_SIZE:usize = 16;
20
24
21
25
structRingBuffer{
22
-
data:[u8;16],
26
+
data:[u8;RING_SIZE],
23
27
start:usize,
24
28
end:usize,
25
29
}
26
30
27
31
implRingBuffer{
28
32
fnnew() -> RingBuffer{
29
33
RingBuffer{
30
-
data:[0;16],
34
+
data:[0;RING_SIZE],
31
35
start:0,
32
36
end:0,
33
37
}
@@ -43,18 +47,25 @@ impl RingBuffer {
43
47
/// This function tries to put `value` on the queue; and returns true if this succeeds
44
48
/// It returns false if writing to the queue failed (which can happen if there is not enough room)
0 commit comments