value_trait/
array.rs

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::slice::SliceIndex;

/// Trait for indexing into an array
pub trait Indexed<T> {
    /// Elements of the array
    type Element: ?Sized;

    /// Gets a ref to a value based on n index, returns `None` if the
    /// current Value isn't an Array or doesn't contain the index
    /// it was asked for.
    #[must_use]
    fn get(&self, i: T) -> Option<&Self::Element>;
}
/// A trait for the minimal common functionality of a vale array
pub trait Array {
    /// Elements of the array
    type Element;

    /// Iterates over the values paris
    #[must_use]
    fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = &Self::Element> + 'i>;

    /// Number of key/value pairs
    #[must_use]
    fn len(&self) -> usize;

    /// Returns if the array is empty
    #[must_use]
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

/// Trait for indexing into an array
pub trait IndexedMut<T> {
    /// Elements of the array
    type Element: ?Sized;

    /// Gets a ref to a value based on n index, returns `None` if the
    /// current Value isn't an Array or doesn't contain the index
    /// it was asked for.
    #[must_use]
    fn get_mut(&mut self, i: T) -> Option<&mut Self::Element>;
}
/// Mutability functions for a value array
pub trait ArrayMut {
    /// Elements of the array
    type Element;

    /// Returns the last element of the array or `None`
    #[must_use]
    fn pop(&mut self) -> Option<Self::Element>;

    /// Appends e to the end of the `Array`
    fn push(&mut self, e: Self::Element);
}

impl<T, I> Indexed<I> for Vec<T>
where
    I: SliceIndex<[T]>,
{
    type Element = <I as SliceIndex<[T]>>::Output;
    #[inline]
    fn get(&self, i: I) -> Option<&Self::Element> {
        <[T]>::get(self, i)
    }
}

impl<T> Array for Vec<T> {
    type Element = T;

    fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = &T> + 'i> {
        Box::new(<[T]>::iter(self))
    }

    #[inline]
    fn len(&self) -> usize {
        self.len()
    }
}

impl<T, I> IndexedMut<I> for Vec<T>
where
    I: SliceIndex<[T]>,
{
    type Element = <I as SliceIndex<[T]>>::Output;
    #[inline]
    fn get_mut(&mut self, i: I) -> Option<&mut Self::Element> {
        <[T]>::get_mut(self, i)
    }
}

impl<T> ArrayMut for Vec<T> {
    type Element = T;

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

    #[inline]
    fn push(&mut self, e: T) {
        Vec::push(self, e);
    }
}

#[cfg(feature = "c-abi")]
mod cabi {
    use super::{Array, ArrayMut, Indexed, IndexedMut, SliceIndex};
    use abi_stable::std_types::RVec;

    impl<T, I> Indexed<I> for RVec<T>
    where
        I: SliceIndex<[T]>,
    {
        type Element = <I as SliceIndex<[T]>>::Output;
        #[inline]
        fn get(&self, i: I) -> Option<&Self::Element> {
            <[T]>::get(self, i)
        }
    }

    impl<T> Array for RVec<T> {
        type Element = T;

        fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = &T> + 'i> {
            Box::new(<[T]>::iter(self))
        }

        #[inline]
        fn len(&self) -> usize {
            self.len()
        }
    }

    impl<T, I> IndexedMut<I> for RVec<T>
    where
        I: SliceIndex<[T]>,
    {
        type Element = <I as SliceIndex<[T]>>::Output;
        #[inline]
        fn get_mut(&mut self, i: I) -> Option<&mut Self::Element> {
            <[T]>::get_mut(self, i)
        }
    }

    #[cfg(feature = "c-abi")]
    impl<T> ArrayMut for RVec<T> {
        type Element = T;

        #[inline]
        fn pop(&mut self) -> Option<T> {
            abi_stable::std_types::RVec::pop(self)
        }

        #[inline]
        fn push(&mut self, e: T) {
            abi_stable::std_types::RVec::push(self, e);
        }
    }
}