Sha256: efea562398cc761ee20853c0300dffd4f6e1cca599384d967cefa5d3855d0037

Contents?: true

Size: 1.63 KB

Versions: 171

Compression:

Stored size: 1.63 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

171 entries across 171 versions & 1 rubygems

Version Path
trackler-2.2.1.179 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.178 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.177 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.176 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.175 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.174 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.173 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.172 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.171 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.170 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.169 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.167 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.166 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.165 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.164 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.163 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.162 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.161 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.160 tracks/rust/exercises/circular-buffer/example.rs
trackler-2.2.1.159 tracks/rust/exercises/circular-buffer/example.rs