vmcircbuf

A circular buffer that magically wraps around without any copying

11 releases

0.2.8 May 6, 2020
0.2.7 May 6, 2020
0.2.3 Apr 30, 2020
0.1.3 Apr 30, 2020

#1196 in Data structures

26 downloads per month

MIT license

20KB
347 lines

vmcircbuf

Build Status Crate Documentation GitHub

This is a simple crate to create a circular buffer that magically wraps around without any copying. The buffer holds exactly size many bytes but it is presented as a size + wrap length slice where the last wrap many bytes overlap with the first wrap many bytes of the slice. This magic trick is performed with virtual memory, the same physical pages are mapped both at the start and at the end of the buffer. This crate is working on Linux, OSX, Windows, iOS, Android, Raspberry PI and MIPS.

let mut buffer = Buffer::new(1024, 100).unwrap();
let size = buffer.size();
let wrap = buffer.wrap();
assert!(size >= 1024 && wrap >= 100);
let slice: &mut [u8] = buffer.as_mut_slice();
assert_eq!(slice.len(), size + wrap);

for a in slice.iter_mut() {
    *a = 0;
}

slice[0] = 123;
assert_eq!(slice[size], 123);

Dependencies

~220KB