slice_queue/
traits.rs

1pub trait ReadableSliceQueue<T> {
2	/// The amount of elements stored
3	///
4	/// Returns __the amount of elements stored in `self`__
5	fn len(&self) -> usize;
6	/// Checks if there are __no__ elements stored
7	///
8	/// Returns either __`true`__ if `self` is empty or __`false`__ otherwise
9	fn is_empty(&self) -> bool;
10	
11	/// Take a look at the first element __without__ consuming it
12	///
13	/// Returns either _`Some(element_ref)`_ if we have a first element or _`None`_ otherwise
14	fn peek(&self) -> Option<&T>;
15	/// Take a look at the first `n` elements __without__ consuming them
16	///
17	/// Parameters:
18	///  - `n`: The amount of elements to peek at
19	///
20	/// Returns either __`Ok(element_refs)`__ if there were `n` elements avaliable to peek at or
21	/// __`Err(element_refs)`__ if less elements were available
22	fn peek_n(&self, n: usize) -> Result<&[T], &[T]>;
23	
24	/// Consumes the first element and returns it
25	///
26	/// Returns either __`Ok(element)`__ if there was an element to consume or __`Err(())`__
27	/// otherwise
28	fn pop(&mut self) -> Result<T, ()>;
29	/// Consumes the first `n` elements and returns them
30	///
31	/// Parameters:
32	///  - `n`: The amount of elements to consume
33	///
34	/// Returns either __`Ok(elements)`__ if there were `n` elements avaliable to consume or
35	/// __`Err(elements)`__ if less elements were available
36	fn pop_n(&mut self, n: usize) -> Result<Vec<T>, Vec<T>>;
37	/// Consumes the first `dst.len()` and moves them into `dst`
38	///
39	/// Parameters:
40	///  - `dst`: The target to move the elements into
41	///
42	/// Returns either __`Ok(())`__ if `dst` was filled completely or __`Err(element_count)`__ if
43	/// only `element_count` elements were moved
44	fn pop_into(&mut self, dst: &mut[T]) -> Result<(), usize>;
45	
46	/// Discards the first `n` elements
47	///
48	/// Parameters:
49	///  - `n`: The amount of elements to discard
50	///
51	/// Returns either __`Ok(())`__ if `n` elements were discarded or __`Err(element_count)`__ if
52	/// only `element_count` elements were discarded
53	fn drop_n(&mut self, n: usize) -> Result<(), usize>;
54}
55
56
57pub trait WriteableSliceQueue<T> {
58	/// The amount of space remaining until `self.limit` is reached
59	///
60	/// Returns __the amount of space remaining in `self` until `self.limit` is reached__
61	fn remaining(&self) -> usize;
62	
63	/// Reserves an additional amount of memory to append `n` elements without reallocating
64	///
65	/// Does nothing if `self.reserved` is greater or equal `n`
66	///
67	/// Parameters:
68	///  - `n`: The amount of elements that we should be able to append without reallocating
69	///
70	/// Returns either _nothing_ if the space for `n` elements could be reserved or _the amount of
71	/// elements reserved_ if `n` was greater than `self.remaining`.
72	fn reserve_n(&mut self, n: usize) -> Result<(), usize>;
73	/// The amount of elements that can be appended with out reallocating
74	///
75	/// Returns __the amount of elements that can be appended with out reallocating__
76	fn reserved(&self) -> usize;
77	
78	/// Appends `element` at the end
79	///
80	/// Parameters:
81	///  - `element`: The element to append at the end
82	///
83	/// Returns either __`Ok(())`__ if the element was pushed successfully or __`Err(element)`__ if
84	/// `element` was not appended because `self.limit` would have been exceeded
85	fn push(&mut self, element: T) -> Result<(), T>;
86	/// Appends `n` at the end
87	///
88	/// Parameters:
89	///  - `n`: The n elements to append at the end
90	///
91	/// Returns either __`Ok(())`__ if `n` was appended completely or __`Err(remaining_elements)`__
92	/// if `n` was only appended partially because `self.limit` would have been exceeded
93	fn push_n(&mut self, n: Vec<T>) -> Result<(), Vec<T>>;
94	/// Clones and appends the elements in `src` at the end
95	///
96	/// Parameters:
97	///  - `src`: A slice containing the elements to clone and append
98	///
99	/// Returns either __`Ok(())`__ if `src` was appended completely or
100	/// __`Err(remaining_element_count)`__ if `src` was only appended partially because `self.limit`
101	/// would have been exceeded
102	fn push_from(&mut self, src: &[T]) -> Result<(), usize> where T: Clone;
103	/// Calls `push_fn` to push up to `n` elements in place
104	///
105	/// __Warning: This function panics if `self.limit` is exceeded__
106	///
107	/// The function works like this:
108	///  1. `n` default elements are inserted at the end
109	///  2. `push_fn` is called with a mutable slice referencing the new elements and returns either
110	///     the amount of elements pushed or an error
111	///  3. If the amount of elements pushed is smaller than `n` or an error occurred, the unused
112	///     default elements are removed again
113	///
114	/// Parameters:
115	///  - `n`: The amount of bytes to reserve
116	///  - `push_fn`: The pushing callback
117	///
118	/// Returns either _the amount of elements pushed_ or _the error `push_fn` returned_
119	///
120	/// Example:
121	/// ```
122	/// # extern crate slice_queue;
123	/// # use slice_queue::*;
124	///	let mut slice_queue = SliceQueue::new();
125	///
126	/// // Successful push
127	///	slice_queue.push_in_place(7, |buffer: &mut[usize]| -> Result<usize, ()> {
128	/// 	(0..4).for_each(|i| buffer[i] = i);
129	/// 	Ok(4)
130	/// });
131	/// assert_eq!(slice_queue.len(), 4);
132	/// (0..4).for_each(|i| assert_eq!(slice_queue[i], i));
133	///
134	/// // Failed push
135	/// slice_queue.push_in_place(7, |buffer: &mut[usize]| -> Result<usize, ()> {
136	/// 	(0..4).for_each(|i| buffer[i] = i + 7);
137	/// 	Err(())
138	/// });
139	/// assert_eq!(slice_queue.len(), 4);
140	/// (0..4).for_each(|i| assert_eq!(slice_queue[i], i));
141	///	```
142	fn push_in_place<E>(&mut self, n: usize, push_fn: impl FnMut(&mut[T]) -> Result<usize, E>) -> Result<usize, E> where T: Default;
143}