#buffer #roll

rollbuf

An extendable buffer which rolls the last incomplete part to the beginning so it can be completed and consumed

1 unstable release

Uses old Rust 2015

0.1.0 Feb 5, 2018

#5 in #test-cases

Download history 13/week @ 2023-11-06 9/week @ 2023-11-13 11/week @ 2023-11-20 13/week @ 2023-11-27 5/week @ 2023-12-04 7/week @ 2023-12-11 9/week @ 2023-12-18 10/week @ 2023-12-25 4/week @ 2024-01-01 11/week @ 2024-01-08 8/week @ 2024-01-15 6/week @ 2024-01-22 6/week @ 2024-01-29 11/week @ 2024-02-05 14/week @ 2024-02-12 30/week @ 2024-02-19

61 downloads per month
Used in 2 crates

Unlicense/MIT

7KB
132 lines

rollbuf

An extendable buffer which rolls the last incomplete part to the beginning so it can be completed and consumed.

Dual-licensed under MIT or unlicense

Documentation

https://docs.rs/rollbuf

Usage

Add this to your Cargo.toml:

[dependencies]
rollbuf = "0.1"

and this to your crate root:

extern crate rollbuf;

Example

use rollbuf::RollBuf;

let inner: &[u8] = &[1, 2, 3, 4, 5, 6, 7];
let mut b = RollBuf::with_capacity(3, inner);

struct TestCase {
    consume: usize,
    roll: bool,
    want: (Vec<u8>, bool),
}

let test_cases = vec![
    TestCase { consume: 2, roll: false, want: (vec![1, 2, 3],           true)  },
    TestCase { consume: 0, roll: true,  want: (vec![1, 2, 3, 4, 5, 6],  true)  },
    TestCase { consume: 4, roll: false, want: (vec![5, 6],              true)  },
    TestCase { consume: 2, roll: true,  want: (vec![7],                 false) },
    TestCase { consume: 2, roll: false, want: (vec![],                  false) },
];

for t in test_cases {
    b.consume(t.consume);
    if t.roll {
        b.roll();
    }
    let is_full = b.fill_buf().unwrap();
    let contents = b.contents();
    assert_eq!((contents, is_full), (t.want.0.as_slice(), t.want.1));
}

No runtime deps