Skip to content

Commit 370007d

Browse files
ZapAntonpetertseng
authored andcommitted
circular-buffer: Add template to the stub file (#608)
Contributes to #551
1 parent ea042f8 commit 370007d

File tree

1 file changed

+42
-0
lines changed
  • exercises/circular-buffer/src

1 file changed

+42
-0
lines changed

exercises/circular-buffer/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1+
use std::marker::PhantomData;
12

3+
pub struct CircularBuffer<T> {
4+
// This field is here to make the template compile and not to
5+
// complain about unused type parameter 'T'. Once you start
6+
// solving the exercise, delete this field and the 'std::marker::PhantomData'
7+
// import.
8+
field: PhantomData<T>,
9+
}
10+
11+
#[derive(Debug, PartialEq)]
12+
pub enum Error {
13+
EmptyBuffer,
14+
FullBuffer,
15+
}
16+
17+
impl<T> CircularBuffer<T> {
18+
pub fn new(capacity: usize) -> Self {
19+
unimplemented!(
20+
"Construct a new CircularBuffer with the capacity to hold {}.",
21+
match capacity {
22+
1 => format!("1 element"),
23+
_ => format!("{} elements", capacity),
24+
}
25+
);
26+
}
27+
28+
pub fn write(&mut self, _element: T) -> Result<(), Error> {
29+
unimplemented!("Write the passed element to the CircularBuffer or return FullBuffer error if CircularBuffer is full.");
30+
}
31+
32+
pub fn read(&mut self) -> Result<T, Error> {
33+
unimplemented!("Read the oldest element from the CircularBuffer or return EmptyBuffer error if CircularBuffer is empty.");
34+
}
35+
36+
pub fn clear(&mut self) {
37+
unimplemented!("Clear the CircularBuffer.");
38+
}
39+
40+
pub fn overwrite(&mut self, _element: T) {
41+
unimplemented!("Write the passed element to the CircularBuffer, overwriting the existing elements if CircularBuffer is full.");
42+
}
43+
}

0 commit comments

Comments
 (0)