Crates.io | vmcircbuf |
lib.rs | vmcircbuf |
version | 0.2.8 |
source | src |
created_at | 2020-04-30 15:37:56.799777 |
updated_at | 2020-05-06 09:42:51.063223 |
description | A circular buffer that magically wraps around without any copying |
homepage | |
repository | https://github.com/mmaroti/vmcircbuf/ |
max_upload_size | |
id | 235861 |
size | 21,394 |
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);