1 | use core::fmt; |
2 | use core::iter::FusedIterator; |
3 | use core::marker::PhantomData; |
4 | use core::mem::MaybeUninit; |
5 | use core::{ptr, slice}; |
6 | |
7 | /// A fixed capacity double-ended queue. |
8 | /// |
9 | /// # Examples |
10 | /// |
11 | /// ``` |
12 | /// use heapless::Deque; |
13 | /// |
14 | /// // A deque with a fixed capacity of 8 elements allocated on the stack |
15 | /// let mut deque = Deque::<_, 8>::new(); |
16 | /// |
17 | /// // You can use it as a good old FIFO queue. |
18 | /// deque.push_back(1); |
19 | /// deque.push_back(2); |
20 | /// assert_eq!(deque.len(), 2); |
21 | /// |
22 | /// assert_eq!(deque.pop_front(), Some(1)); |
23 | /// assert_eq!(deque.pop_front(), Some(2)); |
24 | /// assert_eq!(deque.len(), 0); |
25 | /// |
26 | /// // Deque is double-ended, you can push and pop from the front and back. |
27 | /// deque.push_back(1); |
28 | /// deque.push_front(2); |
29 | /// deque.push_back(3); |
30 | /// deque.push_front(4); |
31 | /// assert_eq!(deque.pop_front(), Some(4)); |
32 | /// assert_eq!(deque.pop_front(), Some(2)); |
33 | /// assert_eq!(deque.pop_front(), Some(1)); |
34 | /// assert_eq!(deque.pop_front(), Some(3)); |
35 | /// |
36 | /// // You can iterate it, yielding all the elements front-to-back. |
37 | /// for x in &deque { |
38 | /// println!("{}" , x); |
39 | /// } |
40 | /// ``` |
41 | pub struct Deque<T, const N: usize> { |
42 | buffer: [MaybeUninit<T>; N], |
43 | |
44 | /// Front index. Always 0..=(N-1) |
45 | front: usize, |
46 | /// Back index. Always 0..=(N-1). |
47 | back: usize, |
48 | |
49 | /// Used to distinguish "empty" and "full" cases when `front == back`. |
50 | /// May only be `true` if `front == back`, always `false` otherwise. |
51 | full: bool, |
52 | } |
53 | |
54 | impl<T, const N: usize> Deque<T, N> { |
55 | const INIT: MaybeUninit<T> = MaybeUninit::uninit(); |
56 | |
57 | /// Constructs a new, empty deque with a fixed capacity of `N` |
58 | /// |
59 | /// # Examples |
60 | /// |
61 | /// ``` |
62 | /// use heapless::Deque; |
63 | /// |
64 | /// // allocate the deque on the stack |
65 | /// let mut x: Deque<u8, 16> = Deque::new(); |
66 | /// |
67 | /// // allocate the deque in a static variable |
68 | /// static mut X: Deque<u8, 16> = Deque::new(); |
69 | /// ``` |
70 | pub const fn new() -> Self { |
71 | // Const assert N > 0 |
72 | crate::sealed::greater_than_0::<N>(); |
73 | |
74 | Self { |
75 | buffer: [Self::INIT; N], |
76 | front: 0, |
77 | back: 0, |
78 | full: false, |
79 | } |
80 | } |
81 | |
82 | fn increment(i: usize) -> usize { |
83 | if i + 1 == N { |
84 | 0 |
85 | } else { |
86 | i + 1 |
87 | } |
88 | } |
89 | |
90 | fn decrement(i: usize) -> usize { |
91 | if i == 0 { |
92 | N - 1 |
93 | } else { |
94 | i - 1 |
95 | } |
96 | } |
97 | |
98 | /// Returns the maximum number of elements the deque can hold. |
99 | pub const fn capacity(&self) -> usize { |
100 | N |
101 | } |
102 | |
103 | /// Returns the number of elements currently in the deque. |
104 | pub const fn len(&self) -> usize { |
105 | if self.full { |
106 | N |
107 | } else if self.back < self.front { |
108 | self.back + N - self.front |
109 | } else { |
110 | self.back - self.front |
111 | } |
112 | } |
113 | |
114 | /// Clears the deque, removing all values. |
115 | pub fn clear(&mut self) { |
116 | // safety: we're immediately setting a consistent empty state. |
117 | unsafe { self.drop_contents() } |
118 | self.front = 0; |
119 | self.back = 0; |
120 | self.full = false; |
121 | } |
122 | |
123 | /// Drop all items in the `Deque`, leaving the state `back/front/full` unmodified. |
124 | /// |
125 | /// safety: leaves the `Deque` in an inconsistent state, so can cause duplicate drops. |
126 | unsafe fn drop_contents(&mut self) { |
127 | // We drop each element used in the deque by turning into a &mut[T] |
128 | let (a, b) = self.as_mut_slices(); |
129 | ptr::drop_in_place(a); |
130 | ptr::drop_in_place(b); |
131 | } |
132 | |
133 | /// Returns whether the deque is empty. |
134 | pub fn is_empty(&self) -> bool { |
135 | self.front == self.back && !self.full |
136 | } |
137 | |
138 | /// Returns whether the deque is full (i.e. if `len() == capacity()`. |
139 | pub fn is_full(&self) -> bool { |
140 | self.full |
141 | } |
142 | |
143 | /// Returns a pair of slices which contain, in order, the contents of the `Deque`. |
144 | pub fn as_slices(&self) -> (&[T], &[T]) { |
145 | // NOTE(unsafe) avoid bound checks in the slicing operation |
146 | unsafe { |
147 | if self.is_empty() { |
148 | (&[], &[]) |
149 | } else if self.back <= self.front { |
150 | ( |
151 | slice::from_raw_parts( |
152 | self.buffer.as_ptr().add(self.front) as *const T, |
153 | N - self.front, |
154 | ), |
155 | slice::from_raw_parts(self.buffer.as_ptr() as *const T, self.back), |
156 | ) |
157 | } else { |
158 | ( |
159 | slice::from_raw_parts( |
160 | self.buffer.as_ptr().add(self.front) as *const T, |
161 | self.back - self.front, |
162 | ), |
163 | &[], |
164 | ) |
165 | } |
166 | } |
167 | } |
168 | |
169 | /// Returns a pair of mutable slices which contain, in order, the contents of the `Deque`. |
170 | pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) { |
171 | let ptr = self.buffer.as_mut_ptr(); |
172 | |
173 | // NOTE(unsafe) avoid bound checks in the slicing operation |
174 | unsafe { |
175 | if self.is_empty() { |
176 | (&mut [], &mut []) |
177 | } else if self.back <= self.front { |
178 | ( |
179 | slice::from_raw_parts_mut(ptr.add(self.front) as *mut T, N - self.front), |
180 | slice::from_raw_parts_mut(ptr as *mut T, self.back), |
181 | ) |
182 | } else { |
183 | ( |
184 | slice::from_raw_parts_mut( |
185 | ptr.add(self.front) as *mut T, |
186 | self.back - self.front, |
187 | ), |
188 | &mut [], |
189 | ) |
190 | } |
191 | } |
192 | } |
193 | |
194 | /// Provides a reference to the front element, or None if the `Deque` is empty. |
195 | pub fn front(&self) -> Option<&T> { |
196 | if self.is_empty() { |
197 | None |
198 | } else { |
199 | Some(unsafe { &*self.buffer.get_unchecked(self.front).as_ptr() }) |
200 | } |
201 | } |
202 | |
203 | /// Provides a mutable reference to the front element, or None if the `Deque` is empty. |
204 | pub fn front_mut(&mut self) -> Option<&mut T> { |
205 | if self.is_empty() { |
206 | None |
207 | } else { |
208 | Some(unsafe { &mut *self.buffer.get_unchecked_mut(self.front).as_mut_ptr() }) |
209 | } |
210 | } |
211 | |
212 | /// Provides a reference to the back element, or None if the `Deque` is empty. |
213 | pub fn back(&self) -> Option<&T> { |
214 | if self.is_empty() { |
215 | None |
216 | } else { |
217 | let index = Self::decrement(self.back); |
218 | Some(unsafe { &*self.buffer.get_unchecked(index).as_ptr() }) |
219 | } |
220 | } |
221 | |
222 | /// Provides a mutable reference to the back element, or None if the `Deque` is empty. |
223 | pub fn back_mut(&mut self) -> Option<&mut T> { |
224 | if self.is_empty() { |
225 | None |
226 | } else { |
227 | let index = Self::decrement(self.back); |
228 | Some(unsafe { &mut *self.buffer.get_unchecked_mut(index).as_mut_ptr() }) |
229 | } |
230 | } |
231 | |
232 | /// Removes the item from the front of the deque and returns it, or `None` if it's empty |
233 | pub fn pop_front(&mut self) -> Option<T> { |
234 | if self.is_empty() { |
235 | None |
236 | } else { |
237 | Some(unsafe { self.pop_front_unchecked() }) |
238 | } |
239 | } |
240 | |
241 | /// Removes the item from the back of the deque and returns it, or `None` if it's empty |
242 | pub fn pop_back(&mut self) -> Option<T> { |
243 | if self.is_empty() { |
244 | None |
245 | } else { |
246 | Some(unsafe { self.pop_back_unchecked() }) |
247 | } |
248 | } |
249 | |
250 | /// Appends an `item` to the front of the deque |
251 | /// |
252 | /// Returns back the `item` if the deque is full |
253 | pub fn push_front(&mut self, item: T) -> Result<(), T> { |
254 | if self.is_full() { |
255 | Err(item) |
256 | } else { |
257 | unsafe { self.push_front_unchecked(item) } |
258 | Ok(()) |
259 | } |
260 | } |
261 | |
262 | /// Appends an `item` to the back of the deque |
263 | /// |
264 | /// Returns back the `item` if the deque is full |
265 | pub fn push_back(&mut self, item: T) -> Result<(), T> { |
266 | if self.is_full() { |
267 | Err(item) |
268 | } else { |
269 | unsafe { self.push_back_unchecked(item) } |
270 | Ok(()) |
271 | } |
272 | } |
273 | |
274 | /// Removes an item from the front of the deque and returns it, without checking that the deque |
275 | /// is not empty |
276 | /// |
277 | /// # Safety |
278 | /// |
279 | /// It's undefined behavior to call this on an empty deque |
280 | pub unsafe fn pop_front_unchecked(&mut self) -> T { |
281 | debug_assert!(!self.is_empty()); |
282 | |
283 | let index = self.front; |
284 | self.full = false; |
285 | self.front = Self::increment(self.front); |
286 | (self.buffer.get_unchecked_mut(index).as_ptr() as *const T).read() |
287 | } |
288 | |
289 | /// Removes an item from the back of the deque and returns it, without checking that the deque |
290 | /// is not empty |
291 | /// |
292 | /// # Safety |
293 | /// |
294 | /// It's undefined behavior to call this on an empty deque |
295 | pub unsafe fn pop_back_unchecked(&mut self) -> T { |
296 | debug_assert!(!self.is_empty()); |
297 | |
298 | self.full = false; |
299 | self.back = Self::decrement(self.back); |
300 | (self.buffer.get_unchecked_mut(self.back).as_ptr() as *const T).read() |
301 | } |
302 | |
303 | /// Appends an `item` to the front of the deque |
304 | /// |
305 | /// # Safety |
306 | /// |
307 | /// This assumes the deque is not full. |
308 | pub unsafe fn push_front_unchecked(&mut self, item: T) { |
309 | debug_assert!(!self.is_full()); |
310 | |
311 | let index = Self::decrement(self.front); |
312 | // NOTE: the memory slot that we are about to write to is uninitialized. We assign |
313 | // a `MaybeUninit` to avoid running `T`'s destructor on the uninitialized memory |
314 | *self.buffer.get_unchecked_mut(index) = MaybeUninit::new(item); |
315 | self.front = index; |
316 | if self.front == self.back { |
317 | self.full = true; |
318 | } |
319 | } |
320 | |
321 | /// Appends an `item` to the back of the deque |
322 | /// |
323 | /// # Safety |
324 | /// |
325 | /// This assumes the deque is not full. |
326 | pub unsafe fn push_back_unchecked(&mut self, item: T) { |
327 | debug_assert!(!self.is_full()); |
328 | |
329 | // NOTE: the memory slot that we are about to write to is uninitialized. We assign |
330 | // a `MaybeUninit` to avoid running `T`'s destructor on the uninitialized memory |
331 | *self.buffer.get_unchecked_mut(self.back) = MaybeUninit::new(item); |
332 | self.back = Self::increment(self.back); |
333 | if self.front == self.back { |
334 | self.full = true; |
335 | } |
336 | } |
337 | |
338 | /// Returns an iterator over the deque. |
339 | pub fn iter(&self) -> Iter<'_, T, N> { |
340 | let done = self.is_empty(); |
341 | Iter { |
342 | _phantom: PhantomData, |
343 | buffer: &self.buffer as *const MaybeUninit<T>, |
344 | front: self.front, |
345 | back: self.back, |
346 | done, |
347 | } |
348 | } |
349 | |
350 | /// Returns an iterator that allows modifying each value. |
351 | pub fn iter_mut(&mut self) -> IterMut<'_, T, N> { |
352 | let done = self.is_empty(); |
353 | IterMut { |
354 | _phantom: PhantomData, |
355 | buffer: &mut self.buffer as *mut _ as *mut MaybeUninit<T>, |
356 | front: self.front, |
357 | back: self.back, |
358 | done, |
359 | } |
360 | } |
361 | } |
362 | |
363 | // Trait implementations |
364 | |
365 | impl<T, const N: usize> Default for Deque<T, N> { |
366 | fn default() -> Self { |
367 | Self::new() |
368 | } |
369 | } |
370 | |
371 | impl<T, const N: usize> Drop for Deque<T, N> { |
372 | fn drop(&mut self) { |
373 | // safety: `self` is left in an inconsistent state but it doesn't matter since |
374 | // it's getting dropped. Nothing should be able to observe `self` after drop. |
375 | unsafe { self.drop_contents() } |
376 | } |
377 | } |
378 | |
379 | impl<T: fmt::Debug, const N: usize> fmt::Debug for Deque<T, N> { |
380 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
381 | f.debug_list().entries(self).finish() |
382 | } |
383 | } |
384 | |
385 | /// An iterator that moves out of a [`Deque`]. |
386 | /// |
387 | /// This struct is created by calling the `into_iter` method. |
388 | /// |
389 | #[derive (Clone)] |
390 | pub struct IntoIter<T, const N: usize> { |
391 | deque: Deque<T, N>, |
392 | } |
393 | |
394 | impl<T, const N: usize> Iterator for IntoIter<T, N> { |
395 | type Item = T; |
396 | fn next(&mut self) -> Option<Self::Item> { |
397 | self.deque.pop_front() |
398 | } |
399 | } |
400 | |
401 | impl<T, const N: usize> IntoIterator for Deque<T, N> { |
402 | type Item = T; |
403 | type IntoIter = IntoIter<T, N>; |
404 | |
405 | fn into_iter(self) -> Self::IntoIter { |
406 | IntoIter { deque: self } |
407 | } |
408 | } |
409 | |
410 | /// An iterator over the elements of a [`Deque`]. |
411 | /// |
412 | /// This struct is created by calling the `iter` method. |
413 | #[derive (Clone)] |
414 | pub struct Iter<'a, T, const N: usize> { |
415 | buffer: *const MaybeUninit<T>, |
416 | _phantom: PhantomData<&'a T>, |
417 | front: usize, |
418 | back: usize, |
419 | done: bool, |
420 | } |
421 | |
422 | impl<'a, T, const N: usize> Iterator for Iter<'a, T, N> { |
423 | type Item = &'a T; |
424 | fn next(&mut self) -> Option<Self::Item> { |
425 | if self.done { |
426 | None |
427 | } else { |
428 | let index = self.front; |
429 | self.front = Deque::<T, N>::increment(self.front); |
430 | if self.front == self.back { |
431 | self.done = true; |
432 | } |
433 | Some(unsafe { &*(self.buffer.add(index) as *const T) }) |
434 | } |
435 | } |
436 | |
437 | fn size_hint(&self) -> (usize, Option<usize>) { |
438 | let len = if self.done { |
439 | 0 |
440 | } else if self.back <= self.front { |
441 | self.back + N - self.front |
442 | } else { |
443 | self.back - self.front |
444 | }; |
445 | |
446 | (len, Some(len)) |
447 | } |
448 | } |
449 | |
450 | impl<'a, T, const N: usize> DoubleEndedIterator for Iter<'a, T, N> { |
451 | fn next_back(&mut self) -> Option<Self::Item> { |
452 | if self.done { |
453 | None |
454 | } else { |
455 | self.back = Deque::<T, N>::decrement(self.back); |
456 | if self.front == self.back { |
457 | self.done = true; |
458 | } |
459 | Some(unsafe { &*(self.buffer.add(self.back) as *const T) }) |
460 | } |
461 | } |
462 | } |
463 | |
464 | impl<'a, T, const N: usize> ExactSizeIterator for Iter<'a, T, N> {} |
465 | impl<'a, T, const N: usize> FusedIterator for Iter<'a, T, N> {} |
466 | |
467 | /// An iterator over the elements of a [`Deque`]. |
468 | /// |
469 | /// This struct is created by calling the `iter` method. |
470 | pub struct IterMut<'a, T, const N: usize> { |
471 | buffer: *mut MaybeUninit<T>, |
472 | _phantom: PhantomData<&'a mut T>, |
473 | front: usize, |
474 | back: usize, |
475 | done: bool, |
476 | } |
477 | |
478 | impl<'a, T, const N: usize> Iterator for IterMut<'a, T, N> { |
479 | type Item = &'a mut T; |
480 | fn next(&mut self) -> Option<Self::Item> { |
481 | if self.done { |
482 | None |
483 | } else { |
484 | let index = self.front; |
485 | self.front = Deque::<T, N>::increment(self.front); |
486 | if self.front == self.back { |
487 | self.done = true; |
488 | } |
489 | Some(unsafe { &mut *(self.buffer.add(index) as *mut T) }) |
490 | } |
491 | } |
492 | |
493 | fn size_hint(&self) -> (usize, Option<usize>) { |
494 | let len = if self.done { |
495 | 0 |
496 | } else if self.back <= self.front { |
497 | self.back + N - self.front |
498 | } else { |
499 | self.back - self.front |
500 | }; |
501 | |
502 | (len, Some(len)) |
503 | } |
504 | } |
505 | |
506 | impl<'a, T, const N: usize> DoubleEndedIterator for IterMut<'a, T, N> { |
507 | fn next_back(&mut self) -> Option<Self::Item> { |
508 | if self.done { |
509 | None |
510 | } else { |
511 | self.back = Deque::<T, N>::decrement(self.back); |
512 | if self.front == self.back { |
513 | self.done = true; |
514 | } |
515 | Some(unsafe { &mut *(self.buffer.add(self.back) as *mut T) }) |
516 | } |
517 | } |
518 | } |
519 | |
520 | impl<'a, T, const N: usize> ExactSizeIterator for IterMut<'a, T, N> {} |
521 | impl<'a, T, const N: usize> FusedIterator for IterMut<'a, T, N> {} |
522 | |
523 | impl<'a, T, const N: usize> IntoIterator for &'a Deque<T, N> { |
524 | type Item = &'a T; |
525 | type IntoIter = Iter<'a, T, N>; |
526 | |
527 | fn into_iter(self) -> Self::IntoIter { |
528 | self.iter() |
529 | } |
530 | } |
531 | |
532 | impl<'a, T, const N: usize> IntoIterator for &'a mut Deque<T, N> { |
533 | type Item = &'a mut T; |
534 | type IntoIter = IterMut<'a, T, N>; |
535 | |
536 | fn into_iter(self) -> Self::IntoIter { |
537 | self.iter_mut() |
538 | } |
539 | } |
540 | |
541 | impl<T, const N: usize> Clone for Deque<T, N> |
542 | where |
543 | T: Clone, |
544 | { |
545 | fn clone(&self) -> Self { |
546 | let mut res: Deque = Deque::new(); |
547 | for i: &T in self { |
548 | // safety: the original and new deques have the same capacity, so it can |
549 | // not become full. |
550 | unsafe { res.push_back_unchecked(item:i.clone()) } |
551 | } |
552 | res |
553 | } |
554 | } |
555 | |
556 | #[cfg (test)] |
557 | mod tests { |
558 | use crate::Deque; |
559 | |
560 | #[test ] |
561 | fn static_new() { |
562 | static mut _V: Deque<i32, 4> = Deque::new(); |
563 | } |
564 | |
565 | #[test ] |
566 | fn stack_new() { |
567 | let mut _v: Deque<i32, 4> = Deque::new(); |
568 | } |
569 | |
570 | #[test ] |
571 | fn drop() { |
572 | droppable!(); |
573 | |
574 | { |
575 | let mut v: Deque<Droppable, 2> = Deque::new(); |
576 | v.push_back(Droppable::new()).ok().unwrap(); |
577 | v.push_back(Droppable::new()).ok().unwrap(); |
578 | v.pop_front().unwrap(); |
579 | } |
580 | |
581 | assert_eq!(Droppable::count(), 0); |
582 | |
583 | { |
584 | let mut v: Deque<Droppable, 2> = Deque::new(); |
585 | v.push_back(Droppable::new()).ok().unwrap(); |
586 | v.push_back(Droppable::new()).ok().unwrap(); |
587 | } |
588 | |
589 | assert_eq!(Droppable::count(), 0); |
590 | { |
591 | let mut v: Deque<Droppable, 2> = Deque::new(); |
592 | v.push_front(Droppable::new()).ok().unwrap(); |
593 | v.push_front(Droppable::new()).ok().unwrap(); |
594 | } |
595 | |
596 | assert_eq!(Droppable::count(), 0); |
597 | } |
598 | |
599 | #[test ] |
600 | fn full() { |
601 | let mut v: Deque<i32, 4> = Deque::new(); |
602 | |
603 | v.push_back(0).unwrap(); |
604 | v.push_front(1).unwrap(); |
605 | v.push_back(2).unwrap(); |
606 | v.push_back(3).unwrap(); |
607 | |
608 | assert!(v.push_front(4).is_err()); |
609 | assert!(v.push_back(4).is_err()); |
610 | assert!(v.is_full()); |
611 | } |
612 | |
613 | #[test ] |
614 | fn empty() { |
615 | let mut v: Deque<i32, 4> = Deque::new(); |
616 | assert!(v.is_empty()); |
617 | |
618 | v.push_back(0).unwrap(); |
619 | assert!(!v.is_empty()); |
620 | |
621 | v.push_front(1).unwrap(); |
622 | assert!(!v.is_empty()); |
623 | |
624 | v.pop_front().unwrap(); |
625 | v.pop_front().unwrap(); |
626 | |
627 | assert!(v.pop_front().is_none()); |
628 | assert!(v.pop_back().is_none()); |
629 | assert!(v.is_empty()); |
630 | } |
631 | |
632 | #[test ] |
633 | fn front_back() { |
634 | let mut v: Deque<i32, 4> = Deque::new(); |
635 | assert_eq!(v.front(), None); |
636 | assert_eq!(v.front_mut(), None); |
637 | assert_eq!(v.back(), None); |
638 | assert_eq!(v.back_mut(), None); |
639 | |
640 | v.push_back(4).unwrap(); |
641 | assert_eq!(v.front(), Some(&4)); |
642 | assert_eq!(v.front_mut(), Some(&mut 4)); |
643 | assert_eq!(v.back(), Some(&4)); |
644 | assert_eq!(v.back_mut(), Some(&mut 4)); |
645 | |
646 | v.push_front(3).unwrap(); |
647 | assert_eq!(v.front(), Some(&3)); |
648 | assert_eq!(v.front_mut(), Some(&mut 3)); |
649 | assert_eq!(v.back(), Some(&4)); |
650 | assert_eq!(v.back_mut(), Some(&mut 4)); |
651 | |
652 | v.pop_back().unwrap(); |
653 | assert_eq!(v.front(), Some(&3)); |
654 | assert_eq!(v.front_mut(), Some(&mut 3)); |
655 | assert_eq!(v.back(), Some(&3)); |
656 | assert_eq!(v.back_mut(), Some(&mut 3)); |
657 | |
658 | v.pop_front().unwrap(); |
659 | assert_eq!(v.front(), None); |
660 | assert_eq!(v.front_mut(), None); |
661 | assert_eq!(v.back(), None); |
662 | assert_eq!(v.back_mut(), None); |
663 | } |
664 | |
665 | #[test ] |
666 | fn iter() { |
667 | let mut v: Deque<i32, 4> = Deque::new(); |
668 | |
669 | v.push_back(0).unwrap(); |
670 | v.push_back(1).unwrap(); |
671 | v.push_front(2).unwrap(); |
672 | v.push_front(3).unwrap(); |
673 | v.pop_back().unwrap(); |
674 | v.push_front(4).unwrap(); |
675 | |
676 | let mut items = v.iter(); |
677 | |
678 | assert_eq!(items.next(), Some(&4)); |
679 | assert_eq!(items.next(), Some(&3)); |
680 | assert_eq!(items.next(), Some(&2)); |
681 | assert_eq!(items.next(), Some(&0)); |
682 | assert_eq!(items.next(), None); |
683 | } |
684 | |
685 | #[test ] |
686 | fn iter_mut() { |
687 | let mut v: Deque<i32, 4> = Deque::new(); |
688 | |
689 | v.push_back(0).unwrap(); |
690 | v.push_back(1).unwrap(); |
691 | v.push_front(2).unwrap(); |
692 | v.push_front(3).unwrap(); |
693 | v.pop_back().unwrap(); |
694 | v.push_front(4).unwrap(); |
695 | |
696 | let mut items = v.iter_mut(); |
697 | |
698 | assert_eq!(items.next(), Some(&mut 4)); |
699 | assert_eq!(items.next(), Some(&mut 3)); |
700 | assert_eq!(items.next(), Some(&mut 2)); |
701 | assert_eq!(items.next(), Some(&mut 0)); |
702 | assert_eq!(items.next(), None); |
703 | } |
704 | |
705 | #[test ] |
706 | fn iter_move() { |
707 | let mut v: Deque<i32, 4> = Deque::new(); |
708 | v.push_back(0).unwrap(); |
709 | v.push_back(1).unwrap(); |
710 | v.push_back(2).unwrap(); |
711 | v.push_back(3).unwrap(); |
712 | |
713 | let mut items = v.into_iter(); |
714 | |
715 | assert_eq!(items.next(), Some(0)); |
716 | assert_eq!(items.next(), Some(1)); |
717 | assert_eq!(items.next(), Some(2)); |
718 | assert_eq!(items.next(), Some(3)); |
719 | assert_eq!(items.next(), None); |
720 | } |
721 | |
722 | #[test ] |
723 | fn iter_move_drop() { |
724 | droppable!(); |
725 | |
726 | { |
727 | let mut deque: Deque<Droppable, 2> = Deque::new(); |
728 | deque.push_back(Droppable::new()).ok().unwrap(); |
729 | deque.push_back(Droppable::new()).ok().unwrap(); |
730 | let mut items = deque.into_iter(); |
731 | // Move all |
732 | let _ = items.next(); |
733 | let _ = items.next(); |
734 | } |
735 | |
736 | assert_eq!(Droppable::count(), 0); |
737 | |
738 | { |
739 | let mut deque: Deque<Droppable, 2> = Deque::new(); |
740 | deque.push_back(Droppable::new()).ok().unwrap(); |
741 | deque.push_back(Droppable::new()).ok().unwrap(); |
742 | let _items = deque.into_iter(); |
743 | // Move none |
744 | } |
745 | |
746 | assert_eq!(Droppable::count(), 0); |
747 | |
748 | { |
749 | let mut deque: Deque<Droppable, 2> = Deque::new(); |
750 | deque.push_back(Droppable::new()).ok().unwrap(); |
751 | deque.push_back(Droppable::new()).ok().unwrap(); |
752 | let mut items = deque.into_iter(); |
753 | let _ = items.next(); // Move partly |
754 | } |
755 | |
756 | assert_eq!(Droppable::count(), 0); |
757 | } |
758 | |
759 | #[test ] |
760 | fn push_and_pop() { |
761 | let mut q: Deque<i32, 4> = Deque::new(); |
762 | assert_eq!(q.len(), 0); |
763 | |
764 | assert_eq!(q.pop_front(), None); |
765 | assert_eq!(q.pop_back(), None); |
766 | assert_eq!(q.len(), 0); |
767 | |
768 | q.push_back(0).unwrap(); |
769 | assert_eq!(q.len(), 1); |
770 | |
771 | assert_eq!(q.pop_back(), Some(0)); |
772 | assert_eq!(q.len(), 0); |
773 | |
774 | q.push_back(0).unwrap(); |
775 | q.push_back(1).unwrap(); |
776 | q.push_front(2).unwrap(); |
777 | q.push_front(3).unwrap(); |
778 | assert_eq!(q.len(), 4); |
779 | |
780 | // deque contains: 3 2 0 1 |
781 | assert_eq!(q.pop_front(), Some(3)); |
782 | assert_eq!(q.len(), 3); |
783 | assert_eq!(q.pop_front(), Some(2)); |
784 | assert_eq!(q.len(), 2); |
785 | assert_eq!(q.pop_back(), Some(1)); |
786 | assert_eq!(q.len(), 1); |
787 | assert_eq!(q.pop_front(), Some(0)); |
788 | assert_eq!(q.len(), 0); |
789 | |
790 | // deque is now empty |
791 | assert_eq!(q.pop_front(), None); |
792 | assert_eq!(q.pop_back(), None); |
793 | assert_eq!(q.len(), 0); |
794 | } |
795 | |
796 | #[test ] |
797 | fn as_slices() { |
798 | let mut q: Deque<i32, 4> = Deque::new(); |
799 | assert_eq!(q.len(), 0); |
800 | |
801 | q.push_back(0).unwrap(); |
802 | q.push_back(1).unwrap(); |
803 | q.push_back(2).unwrap(); |
804 | q.push_back(3).unwrap(); |
805 | assert_eq!(q.as_slices(), (&[0, 1, 2, 3][..], &[][..])); |
806 | |
807 | q.pop_front().unwrap(); |
808 | assert_eq!(q.as_slices(), (&[1, 2, 3][..], &[][..])); |
809 | |
810 | q.push_back(4).unwrap(); |
811 | assert_eq!(q.as_slices(), (&[1, 2, 3][..], &[4][..])); |
812 | } |
813 | |
814 | #[test ] |
815 | fn clear() { |
816 | let mut q: Deque<i32, 4> = Deque::new(); |
817 | assert_eq!(q.len(), 0); |
818 | |
819 | q.push_back(0).unwrap(); |
820 | q.push_back(1).unwrap(); |
821 | q.push_back(2).unwrap(); |
822 | q.push_back(3).unwrap(); |
823 | assert_eq!(q.len(), 4); |
824 | |
825 | q.clear(); |
826 | assert_eq!(q.len(), 0); |
827 | |
828 | q.push_back(0).unwrap(); |
829 | assert_eq!(q.len(), 1); |
830 | } |
831 | } |
832 | |