[−][src]Struct vmcircbuf::Buffer
A raw circular buffer of bytes. 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.
Examples
let mut buffer = vmcircbuf::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);
Methods
impl Buffer
[src]
pub fn granularity() -> Result<usize, Error>
[src]
Returns the virtual memory mapping granularity of the underlying operating system. On Unix this is the page size, which is typically 4096 bytes. On Windows this is the allocation granularity, which is typically 65536 bytes.
pub fn new(size: usize, wrap: usize) -> Result<Buffer, Error>
[src]
Creates a new circular buffer with the given size
and wrap
. The
returned size
and wrap
will be rounded up to an integer multiple
of the granularity. This size
parameter must be greater than zero.
The wrap
parameter cannot be larger than size
, but it can be zero.
pub fn size(&self) -> usize
[src]
Returns the size of the circular buffer.
pub fn wrap(&self) -> usize
[src]
Returns the wrap of the circular buffer.
pub fn as_slice(&self) -> &[u8]
[src]
Returns an immutable slice of the circular buffer. The last wrap
many bytes are mapped to the first wrap
many bytes, so you can
read the same content at both places. The length of the slice
is always size + wrap
but it contains only size
many elements.
pub fn as_mut_slice(&mut self) -> &mut [u8]
[src]
Returns a mutable slice of the circular buffer. The last wrap
many bytes are mapped to the first wrap
many bytes, so you can
read and write the same content at both places. The length of the
slice is always size + wrap
, but contains only size
many elements.
Trait Implementations
impl Drop for Buffer
[src]
impl<I: SliceIndex<[u8]>> Index<I> for Buffer
[src]
type Output = I::Output
The returned type after indexing.
fn index(&self, index: I) -> &Self::Output
[src]
impl<I: SliceIndex<[u8]>> IndexMut<I> for Buffer
[src]
impl Send for Buffer
[src]
impl Sync for Buffer
[src]
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,