vecdep 0.1.1

A simple VecDeque implementation that doesn't allocate if nothing is pushed.
Documentation

use std::time::Instant;
use crate::{wrapping::Wrapping, VecDep};

#[test]
fn wrapping() {

    let mut num = Wrapping::new(0, 5);

    num += 2;
    println!("{:?}", *num);
    
    num += 2;
    println!("{:?}", *num);
    
    num += 2;
    println!("{:?}", *num);
    
    num += 2;
    println!("{:?}", *num);
    
    num += 2;
    println!("{:?}", *num);
    
    num += 2;
    println!("{:?}", *num);

}

#[test]
fn vecdep() {

    let mut queue: VecDep<usize> = VecDep::new(8);

    println!("state: {:?}", queue);
    println!("len: {:?}, empty: {}, full: {}", queue.len(), queue.is_empty(), queue.is_full());

    let mut buf = String::new();
    loop {

        std::io::stdin().read_line(&mut buf).unwrap();

        if &buf[0..1] == "+" { queue.push((&buf[1..buf.len()-2]).parse().unwrap()) }
        else if &buf[0..1] == "-" { println!("pop: {:?}", queue.pop()); }
        else { println!("Exited"); break };

        println!("state: {:?}", queue);
        println!("len: {:?}, empty: {}, full: {}", queue.len(), queue.is_empty(), queue.is_full());

        buf.clear();

    }

}

static mut QUEUE: VecDep<u16> = VecDep::new(3);

#[test]
fn vecst() {
    unsafe {

        assert_eq!(QUEUE.len(), 0);

        QUEUE.push(1);
        assert_eq!(QUEUE.len(), 1);
        assert_eq!(QUEUE.pop(), Some(1));
        assert_eq!(QUEUE.pop(), None);
        
        QUEUE.push(1);
        QUEUE.push(2);
        assert_eq!(QUEUE.len(), 2);
        assert_eq!(QUEUE.pop(), Some(1));
        assert_eq!(QUEUE.pop(), Some(2));
        assert!(QUEUE.is_empty());

        QUEUE.push(1);
        QUEUE.push(2);
        QUEUE.push(3);
        assert_eq!(QUEUE.len(), 3);
        QUEUE.push(4);
        assert_eq!(QUEUE.len(), 3);
        assert!(QUEUE.is_full());
        QUEUE.push(5);
        assert_eq!(QUEUE.pop(), Some(3));
        assert_eq!(QUEUE.pop(), Some(4));
        assert_eq!(QUEUE.peek(), Some(&5));
        assert_eq!(QUEUE.peek(), Some(&5));
        assert_eq!(QUEUE.pop(), Some(5));
        assert!(QUEUE.is_empty());

    }
}

const BENCH: usize = 1_000_000;
const REP: usize = 100;

#[test]
fn bench() {

    let mut time;
    let mut time2;

    //? without wrapping (just once)
    let mut queue = VecDep::new(BENCH / 2);

    let start = Instant::now();
    for _ in 0..BENCH {
        queue.push(8i8);
    }
    time = (Instant::now() - start) / BENCH as u32;

    //? with wrapping (every second push)
    let mut queue2 = VecDep::new(2);

    let start2 = Instant::now();
    for _ in 0..BENCH {
        queue2.push(8i8);
    }
    time2 = (Instant::now() - start2) / BENCH as u32;

    for _ in 0..REP {

        //? without wrapping (just once)
        let start = Instant::now();
        for _ in 0..BENCH {
            queue.push(8i8);
        }
        time = (time + (Instant::now() - start) / BENCH as u32) / 2;

        //? with wrapping (every second push)
        let start2 = Instant::now();
        for _ in 0..BENCH {
            queue2.push(8i8);
        }
        time2 = (time2 + (Instant::now() - start2) / BENCH as u32) / 2;

    }

    println!("average time per push (without wrap) : {:?}", time);
    println!("average time per push (with wrap)    : {:?}", time2);

}

const VSVEC: usize = 1_000_000;

#[test]
fn vsvec() {

    let mut v = Vec::new();

    let start = Instant::now();
    for _ in 0..VSVEC { v.push(8i8) };
    for _ in 0..VSVEC { v.remove(0); };
    println!("normal vec: {:?}", Instant::now() - start);
    
    let mut q = VecDep::new(VSVEC);

    let start2 = Instant::now();
    for _ in 0..VSVEC { q.push(8i8) };
    for _ in 0..VSVEC { q.pop(); };
    println!("ring buffer: {:?}", Instant::now() - start2);

}

#[test]
fn zst() {

    let mut queue = VecDep::new(10);
    queue.push(());
    queue.push(());
    queue.push(());

    assert_eq!(queue.pop(), Some(()));
    assert_eq!(queue.pop(), Some(()));
    assert_eq!(queue.pop(), Some(()));
    assert_eq!(queue.pop(), None);

}