generic_std/
vec.rs

1//! A contiguous growable array type with heap-allocated contents, written
2//! `Vec<T>`.
3
4use crate::{
5    plug::{PlugLifetime, PlugType},
6    slice::TypedH1Iter,
7    Sequence, SequenceMut, StreamingIterator, WithCapacity,
8};
9use std::vec::Vec;
10
11/// HKT `Vec` with a type slot.
12pub struct H1Vec;
13
14impl<T> PlugType<T> for H1Vec {
15    type T = Vec<T>;
16}
17
18impl<T> WithCapacity for Vec<T> {
19    fn with_capacity(capacity: usize) -> Self {
20        Vec::<T>::with_capacity(capacity)
21    }
22}
23
24impl<T> Sequence<T> for Vec<T>
25where
26    T: 'static,
27{
28    type H1Iterator = TypedH1Iter<T>;
29
30    fn len(&self) -> usize {
31        Vec::<T>::len(self)
32    }
33
34    fn is_empty(&self) -> bool {
35        Vec::<T>::is_empty(self)
36    }
37
38    fn contains<'a>(&'a self, x: &T) -> bool
39    where
40        T: PartialEq,
41    {
42        <[T]>::contains(self, x)
43    }
44
45    fn get(&self, index: usize) -> Option<&T> {
46        <[T]>::get(self, index)
47    }
48
49    fn first(&self) -> Option<&T> {
50        <[T]>::first(self)
51    }
52
53    fn last(&self) -> Option<&T> {
54        <[T]>::last(self)
55    }
56
57    fn iter<'a>(&'a self) -> <Self::H1Iterator as PlugLifetime<'a>>::T
58    where
59        <Self::H1Iterator as PlugLifetime<'a>>::T: StreamingIterator,
60    {
61        <[T]>::iter(self)
62    }
63}
64
65impl<T> SequenceMut<T> for Vec<T> {
66    fn capacity(&self) -> usize {
67        Vec::<T>::capacity(self)
68    }
69
70    fn clear(&mut self) {
71        Vec::<T>::clear(self)
72    }
73
74    fn reserve(&mut self, additional: usize) {
75        Vec::<T>::reserve(self, additional)
76    }
77
78    fn reserve_exact(&mut self, additional: usize) {
79        Vec::<T>::reserve_exact(self, additional)
80    }
81
82    fn shrink_to_fit(&mut self) {
83        Vec::<T>::shrink_to_fit(self)
84    }
85
86    fn push(&mut self, x: T) {
87        Vec::<T>::push(self, x)
88    }
89
90    fn pop(&mut self) -> Option<T> {
91        Vec::<T>::pop(self)
92    }
93
94    fn insert(&mut self, index: usize, x: T) {
95        Vec::<T>::insert(self, index, x)
96    }
97
98    fn remove(&mut self, index: usize) -> T {
99        Vec::<T>::remove(self, index)
100    }
101}