[−][src]Struct ublox_cellular::sockets::RingBuffer
A ring buffer.
This ring buffer implementation provides many ways to interact with it:
- Enqueueing or dequeueing one element from corresponding side of the buffer;
- Enqueueing or dequeueing a slice of elements from corresponding side of the buffer;
- Accessing allocated and unallocated areas directly.
This implementation is suitable for both simple uses such as a FIFO queue of UDP packets, and advanced ones such as a TCP reassembly buffer.
Implementations
impl<T: Default + Clone, N: ArrayLength<T>> RingBuffer<T, N>
[src]
pub fn new() -> RingBuffer<T, N>
[src]
Create a ring buffer with the given storage.
During creation, every element in storage
is reset.
pub fn clear(&mut self)
[src]
Clear the ring buffer.
pub fn capacity(&self) -> usize
[src]
Return the maximum number of elements in the ring buffer.
pub fn reset(&mut self) where
T: Resettable,
[src]
T: Resettable,
Clear the ring buffer, and reset every element.
pub fn len(&self) -> usize
[src]
Return the current number of elements in the ring buffer.
pub fn window(&self) -> usize
[src]
Return the number of elements that can be added to the ring buffer.
pub fn contiguous_window(&self) -> usize
[src]
Return the largest number of elements that can be added to the buffer
without wrapping around (i.e. in a single enqueue_many
call).
pub fn is_empty(&self) -> bool
[src]
Query whether the buffer is empty.
pub fn is_full(&self) -> bool
[src]
Query whether the buffer is full.
impl<T: Default + Clone, N: ArrayLength<T>> RingBuffer<T, N>
[src]
This is the "discrete" ring buffer interface: it operates with single elements, and boundary conditions (empty/full) are errors.
pub fn enqueue_one_with<'b, R, F>(&'b mut self, f: F) -> Result<R, Error> where
F: FnOnce(&'b mut T) -> Result<R, Error>,
[src]
F: FnOnce(&'b mut T) -> Result<R, Error>,
Call f
with a single buffer element, and enqueue the element if f
returns successfully, or return Err(Error::Exhausted)
if the buffer is full.
pub fn enqueue_one<'b>(&'b mut self) -> Result<&'b mut T, Error>
[src]
Enqueue a single element into the buffer, and return a reference to it,
or return Err(Error::Exhausted)
if the buffer is full.
This function is a shortcut for ring_buf.enqueue_one_with(Ok)
.
pub fn dequeue_one_with<'b, R, F>(&'b mut self, f: F) -> Result<R, Error> where
F: FnOnce(&'b mut T) -> Result<R, Error>,
[src]
F: FnOnce(&'b mut T) -> Result<R, Error>,
Call f
with a single buffer element, and dequeue the element if f
returns successfully, or return Err(Error::Exhausted)
if the buffer is empty.
pub fn dequeue_one(&mut self) -> Result<&mut T, Error>
[src]
Dequeue an element from the buffer, and return a reference to it,
or return Err(Error::Exhausted)
if the buffer is empty.
This function is a shortcut for ring_buf.dequeue_one_with(Ok)
.
impl<T: Default + Debug + Clone, N: ArrayLength<T>> RingBuffer<T, N>
[src]
This is the "continuous" ring buffer interface: it operates with element slices, and boundary conditions (empty/full) simply result in empty slices.
pub fn enqueue_many_with<'b, R, F>(&'b mut self, f: F) -> (usize, R) where
F: FnOnce(&'b mut [T]) -> (usize, R),
[src]
F: FnOnce(&'b mut [T]) -> (usize, R),
Call f
with the largest contiguous slice of unallocated buffer elements,
and enqueue the amount of elements returned by f
.
Panics
This function panics if the amount of elements returned by f
is larger
than the size of the slice passed into it.
pub fn enqueue_many(&mut self, size: usize) -> &mut [T]
[src]
Enqueue a slice of elements up to the given size into the buffer, and return a reference to them.
This function may return a slice smaller than the given size if the free space in the buffer is not contiguous.
pub fn enqueue_slice(&mut self, data: &[T]) -> usize where
T: Copy,
[src]
T: Copy,
Enqueue as many elements from the given slice into the buffer as possible, and return the amount of elements that could fit.
pub fn dequeue_many_with<'b, R, F>(&'b mut self, f: F) -> (usize, R) where
F: FnOnce(&'b mut [T]) -> (usize, R),
[src]
F: FnOnce(&'b mut [T]) -> (usize, R),
Call f
with the largest contiguous slice of allocated buffer elements,
and dequeue the amount of elements returned by f
.
Panics
This function panics if the amount of elements returned by f
is larger
than the size of the slice passed into it.
pub fn dequeue_many_with_wrapping<'b, R, F>(&'b mut self, f: F) -> (usize, R) where
F: FnOnce(&'b [T], Option<&'b [T]>) -> (usize, R),
[src]
F: FnOnce(&'b [T], Option<&'b [T]>) -> (usize, R),
pub fn dequeue_many(&mut self, size: usize) -> &mut [T]
[src]
Dequeue a slice of elements up to the given size from the buffer, and return a reference to them.
This function may return a slice smaller than the given size if the allocated space in the buffer is not contiguous.
pub fn dequeue_slice(&mut self, data: &mut [T]) -> usize where
T: Copy,
[src]
T: Copy,
Dequeue as many elements from the buffer into the given slice as possible, and return the amount of elements that could fit.
impl<T: Default + Clone, N: ArrayLength<T>> RingBuffer<T, N>
[src]
This is the "random access" ring buffer interface: it operates with element slices, and allows to access elements of the buffer that are not adjacent to its head or tail.
pub fn get_unallocated(&mut self, offset: usize, size: usize) -> &mut [T]
[src]
Return the largest contiguous slice of unallocated buffer elements starting at the given offset past the last allocated element, and up to the given size.
pub fn write_unallocated(&mut self, offset: usize, data: &[T]) -> usize where
T: Copy,
[src]
T: Copy,
Write as many elements from the given slice into unallocated buffer elements starting at the given offset past the last allocated element, and return the amount written.
pub fn enqueue_unallocated(&mut self, count: usize)
[src]
Enqueue the given number of unallocated buffer elements.
Panics
Panics if the number of elements given exceeds the number of unallocated elements.
pub fn get_allocated(&self, offset: usize, size: usize) -> &[T]
[src]
Return the largest contiguous slice of allocated buffer elements starting at the given offset past the first allocated element, and up to the given size.
pub fn read_allocated(&mut self, offset: usize, data: &mut [T]) -> usize where
T: Copy,
[src]
T: Copy,
Read as many elements from allocated buffer elements into the given slice starting at the given offset past the first allocated element, and return the amount read.
pub fn dequeue_allocated(&mut self, count: usize)
[src]
Dequeue the given number of allocated buffer elements.
Panics
Panics if the number of elements given exceeds the number of allocated elements.
Trait Implementations
impl<T: Debug, N: Debug + ArrayLength<T>> Debug for RingBuffer<T, N>
[src]
impl<T: Default, N: Default + ArrayLength<T>> Default for RingBuffer<T, N>
[src]
pub fn default() -> RingBuffer<T, N>
[src]
impl<T: Default + Debug + Copy, N: ArrayLength<T>> From<Vec<T, N>> for RingBuffer<T, N>
[src]
pub fn from(slice: Vec<T, N>) -> RingBuffer<T, N>
[src]
Auto Trait Implementations
impl<T, N> RefUnwindSafe for RingBuffer<T, N> where
<N as ArrayLength<T>>::ArrayType: RefUnwindSafe,
<N as ArrayLength<T>>::ArrayType: RefUnwindSafe,
impl<T, N> Send for RingBuffer<T, N> where
T: Send,
T: Send,
impl<T, N> Sync for RingBuffer<T, N> where
T: Sync,
T: Sync,
impl<T, N> Unpin for RingBuffer<T, N> where
<N as ArrayLength<T>>::ArrayType: Unpin,
<N as ArrayLength<T>>::ArrayType: Unpin,
impl<T, N> UnwindSafe for RingBuffer<T, N> where
<N as ArrayLength<T>>::ArrayType: UnwindSafe,
<N as ArrayLength<T>>::ArrayType: UnwindSafe,
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,
pub 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> Same<T> for T
[src]
type Output = T
Should always be Self
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.
pub 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>,