vmcircbuf

Crates.iovmcircbuf
lib.rsvmcircbuf
version0.2.8
sourcesrc
created_at2020-04-30 15:37:56.799777
updated_at2020-05-06 09:42:51.063223
descriptionA circular buffer that magically wraps around without any copying
homepage
repositoryhttps://github.com/mmaroti/vmcircbuf/
max_upload_size
id235861
size21,394
Miklos Maroti (mmaroti)

documentation

https://docs.rs/vmcircbuf/

README

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);
Commit count: 54

cargo fmt