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§
Sourcefn remaining(&self) -> usize
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
Sourcefn reserve_n(&mut self, n: usize) -> Result<(), usize>
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.
Sourcefn reserved(&self) -> usize
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
Sourcefn push(&mut self, element: T) -> Result<(), T>
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
Sourcefn push_n(&mut self, n: Vec<T>) -> Result<(), Vec<T>>
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
Sourcefn push_from(&mut self, src: &[T]) -> Result<(), usize>where
T: Clone,
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
Sourcefn push_in_place<E>(
&mut self,
n: usize,
push_fn: impl FnMut(&mut [T]) -> Result<usize, E>,
) -> Result<usize, E>where
T: Default,
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:
ndefault elements are inserted at the endpush_fnis 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
nor 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));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.