Trait WriteableSliceQueue

Source
pub trait WriteableSliceQueue<T> {
    // Required methods
    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>(
        &mut self,
        n: usize,
        push_fn: impl FnMut(&mut [T]) -> Result<usize, E>,
    ) -> Result<usize, E>
       where T: Default;
}

Required Methods§

Source

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

Source

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.

Source

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

Source

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

Source

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

Source

fn push_from(&mut self, src: &[T]) -> Result<(), usize>
where 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

Source

fn push_in_place<E>( &mut self, n: usize, push_fn: impl FnMut(&mut [T]) -> Result<usize, E>, ) -> Result<usize, E>
where T: Default,

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:

  1. n default elements are inserted at the end
  2. push_fn is called with a mutable slice referencing the new elements and returns either the amount of elements pushed or an error
  3. 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 reserve
  • push_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));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§