Trait timeout_io::WriteableSliceQueue [−][src]
pub trait WriteableSliceQueue<T> { fn remaining(&self) -> usize; fn reserve_n(&mut self, n: usize) -> Result<(), usize>; fn reserved(&self) -> usize; fn push(&mut self, element: T) -> Result<(), T>; fn push_n(&mut self, n: Vec<T>) -> Result<(), Vec<T>>; fn push_from(&mut self, src: &[T]) -> Result<(), usize>
where
T: Clone; fn push_in_place<E, impl FnMut(&mut [T]) -> Result>(
&mut self,
n: usize,
push_fn: impl FnMut(&mut [T]) -> Result
) -> Result<usize, E>
where
T: Default,
impl FnMut(&mut [T]) -> Result: FnMut(&mut [T]) -> Result<usize, E> ; }
Required Methods
fn remaining(&self) -> usize
The amount of space remaining until self.limit
is reached
Returns the amount of space remaining in self
until self.limit
is reached
fn reserve_n(&mut self, n: usize) -> Result<(), usize>
Reserves an additional amount of memory to append n
elements without reallocating
Does nothing if self.reserved
is greater or equal n
Parameters:
n
: The amount of elements that we should be able to append without reallocating
Returns either nothing if the space for n
elements could be reserved or the amount of
elements reserved if n
was greater than self.remaining
.
fn reserved(&self) -> usize
The amount of elements that can be appended with out reallocating
Returns the amount of elements that can be appended with out reallocating
fn push(&mut self, element: T) -> Result<(), T>
Appends element
at the end
Parameters:
element
: The element to append at the end
Returns either Ok(())
if the element was pushed successfully or Err(element)
if
element
was not appended because self.limit
would have been exceeded
fn push_n(&mut self, n: Vec<T>) -> Result<(), Vec<T>>
Appends n
at the end
Parameters:
n
: The n elements to append at the end
Returns either Ok(())
if n
was appended completely or Err(remaining_elements)
if n
was only appended partially because self.limit
would have been exceeded
fn push_from(&mut self, src: &[T]) -> Result<(), usize> where
T: Clone,
T: Clone,
Clones and appends the elements in src
at the end
Parameters:
src
: A slice containing the elements to clone and append
Returns either Ok(())
if src
was appended completely or
Err(remaining_element_count)
if src
was only appended partially because self.limit
would have been exceeded
fn push_in_place<E, impl FnMut(&mut [T]) -> Result>(
&mut self,
n: usize,
push_fn: impl FnMut(&mut [T]) -> Result
) -> Result<usize, E> where
T: Default,
impl FnMut(&mut [T]) -> Result: FnMut(&mut [T]) -> Result<usize, E>,
&mut self,
n: usize,
push_fn: impl FnMut(&mut [T]) -> Result
) -> Result<usize, E> where
T: Default,
impl FnMut(&mut [T]) -> Result
Calls push_fn
to push up to n
elements in place
Warning: This function panics if self.limit
is exceeded
The function works like this:
n
default elements are inserted at the endpush_fn
is called with a mutable slice referencing the new elements and returns either the amount of elements pushed or an error- If the amount of elements pushed is smaller than
n
or an error occurred, the unused default elements are removed again
Parameters:
n
: The amount of bytes to reservepush_fn
: The pushing callback
Returns either the amount of elements pushed or the error push_fn
returned
Example:
let mut slice_queue = SliceQueue::new(); // Successful push slice_queue.push_in_place(7, |buffer: &mut[usize]| -> Result<usize, ()> { (0..4).for_each(|i| buffer[i] = i); Ok(4) }); assert_eq!(slice_queue.len(), 4); (0..4).for_each(|i| assert_eq!(slice_queue[i], i)); // Failed push slice_queue.push_in_place(7, |buffer: &mut[usize]| -> Result<usize, ()> { (0..4).for_each(|i| buffer[i] = i + 7); Err(()) }); assert_eq!(slice_queue.len(), 4); (0..4).for_each(|i| assert_eq!(slice_queue[i], i));
Implementors
impl<T> WriteableSliceQueue<T> for SliceQueue<T>