Sha256: 8093ffce75cbede49fdcb5543102f8396fd4a08c7a580e7d340b4c7898677551

Contents?: true

Size: 1.66 KB

Versions: 224

Compression:

Stored size: 1.66 KB

Contents

#[derive(Debug, PartialEq)]
pub enum Error {
    EmptyBuffer,
    FullBuffer,
}

pub struct CircularBuffer<T: Default + Clone> {
    buffer: Vec<T>,
    size: usize,
    start: usize,
    end: usize,
}

impl<T: Default + Clone> CircularBuffer<T> {
    // this circular buffer keeps an unallocated slot between the start and the end
    // when the buffer is full. 
    pub fn new(size: usize) -> CircularBuffer<T> {
        CircularBuffer { 
            buffer: vec![T::default(); size + 1], 
            size: size + 1, 
            start: 0, 
            end: 0 
        }
    }

    pub fn read(&mut self) -> Result<T, Error> {
        if self.is_empty() {
            return Err(Error::EmptyBuffer);
        }

        let v = self.buffer.get(self.start).unwrap().clone();
        self.advance_start();
        Ok(v)
    }

    pub fn write(&mut self, byte: T) -> Result<(), Error> {
        if self.is_full() {
            return Err(Error::FullBuffer);
        } 
        
        self.buffer[self.end] = byte;
        self.advance_end();
        Ok(())
        
    }

    pub fn overwrite(&mut self, byte: T) {
        if self.is_full() {
            self.advance_start();
        }

        self.buffer[self.end] = byte;
        self.advance_end();
    }

    pub fn clear(&mut self) {
        self.start = 0;
        self.end = 0;
    }

    pub fn is_empty(&self) -> bool {
        self.start == self.end
    }

    pub fn is_full(&self) -> bool {
        (self.end + 1) % self.size == self.start
    }

    fn advance_start(&mut self) {
        self.start = (self.start + 1) % self.size;
    }

    fn advance_end(&mut self) {
        self.end = (self.end + 1) % self.size;
    }

}

Version data entries

224 entries across 224 versions & 1 rubygems

Version Path
trackler-2.0.8.44 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.43 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.42 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.41 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.40 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.39 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.38 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.37 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.36 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.35 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.34 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.33 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.32 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.31 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.30 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.29 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.28 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.27 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.26 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.0.8.24 tracks/rust/exercises/circular-buffer/example.rs