array_ops/
std_types.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use std::collections::VecDeque;
6use std::ops::Index;
7
8use crate::array::{Array, ArrayMut, HasLength};
9
10// VecDeque
11
12impl<A> HasLength for VecDeque<A> {
13    fn len(&self) -> usize {
14        VecDeque::len(self)
15    }
16}
17
18impl<A> Array for VecDeque<A> {
19    fn get(&self, index: usize) -> Option<&<Self as Index<usize>>::Output> {
20        VecDeque::get(self, index)
21    }
22
23    fn contains(&self, target: &<Self as Index<usize>>::Output) -> bool
24    where
25        <Self as Index<usize>>::Output: PartialEq,
26    {
27        VecDeque::contains(self, target)
28    }
29}
30
31impl<A> ArrayMut for VecDeque<A> {
32    fn get_mut(&mut self, index: usize) -> Option<&mut <Self as Index<usize>>::Output> {
33        VecDeque::get_mut(self, index)
34    }
35
36    fn swap(&mut self, index1: usize, index2: usize)
37    where
38        <Self as Index<usize>>::Output: Sized,
39    {
40        VecDeque::swap(self, index1, index2)
41    }
42}
43
44#[cfg(test)]
45mod test {
46    use super::*;
47
48    #[test]
49    fn vec_deque() {
50        let mut vec: VecDeque<_> = vec![3, 2, 1].into();
51        assert_eq!(3, HasLength::len(&vec));
52        assert_eq!(Some(&3), Array::first(&vec));
53        assert_eq!(Some(&1), Array::last(&vec));
54        ArrayMut::sort_unstable(&mut vec);
55        assert_eq!(Some(&1), Array::first(&vec));
56        assert_eq!(Some(&3), Array::last(&vec));
57    }
58}