1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
//! A contiguous growable array type with heap-allocated contents, written
//! `Vec<T>`.

use crate::{
    plug::{PlugLifetime, PlugType},
    slice::TypedH1Iter,
    Sequence, SequenceMut, StreamingIterator, WithCapacity,
};
use std::vec::Vec;

/// HKT `Vec` with a type slot.
pub struct H1Vec;

impl<T> PlugType<T> for H1Vec {
    type T = Vec<T>;
}

impl<T> WithCapacity for Vec<T> {
    fn with_capacity(capacity: usize) -> Self {
        Vec::<T>::with_capacity(capacity)
    }
}

impl<T> Sequence<T> for Vec<T>
where
    T: 'static,
{
    type H1Iterator = TypedH1Iter<T>;

    fn len(&self) -> usize {
        Vec::<T>::len(self)
    }

    fn is_empty(&self) -> bool {
        Vec::<T>::is_empty(self)
    }

    fn contains<'a>(&'a self, x: &T) -> bool
    where
        T: PartialEq,
    {
        <[T]>::contains(self, x)
    }

    fn get(&self, index: usize) -> Option<&T> {
        <[T]>::get(self, index)
    }

    fn first(&self) -> Option<&T> {
        <[T]>::first(self)
    }

    fn last(&self) -> Option<&T> {
        <[T]>::last(self)
    }

    fn iter<'a>(&'a self) -> <Self::H1Iterator as PlugLifetime<'a>>::T
    where
        <Self::H1Iterator as PlugLifetime<'a>>::T: StreamingIterator,
    {
        <[T]>::iter(self)
    }
}

impl<T> SequenceMut<T> for Vec<T> {
    fn capacity(&self) -> usize {
        Vec::<T>::capacity(self)
    }

    fn clear(&mut self) {
        Vec::<T>::clear(self)
    }

    fn reserve(&mut self, additional: usize) {
        Vec::<T>::reserve(self, additional)
    }

    fn reserve_exact(&mut self, additional: usize) {
        Vec::<T>::reserve_exact(self, additional)
    }

    fn shrink_to_fit(&mut self) {
        Vec::<T>::shrink_to_fit(self)
    }

    fn push(&mut self, x: T) {
        Vec::<T>::push(self, x)
    }

    fn pop(&mut self) -> Option<T> {
        Vec::<T>::pop(self)
    }

    fn insert(&mut self, index: usize, x: T) {
        Vec::<T>::insert(self, index, x)
    }

    fn remove(&mut self, index: usize) -> T {
        Vec::<T>::remove(self, index)
    }
}