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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//! `ArrayVec` error types.

use core::{
    any::type_name,
    clone::Clone,
    cmp::{Eq, Ord, PartialEq, PartialOrd},
    fmt::{Debug, Display, Formatter},
    hash::Hash,
    marker::Copy,
};

// ---------------------------------------------------------------------------

/// An error returned when there is no enough spare capacity.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct InsufficientCapacityError;

impl Display for InsufficientCapacityError {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f, "arrayvec insufficient capacity")
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for InsufficientCapacityError {}

// ---------------------------------------------------------------------------

/// An error returned with a value when there is no enough spare capacity.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct InsufficientCapacityErrorVal<T>(pub T);

impl<T> Display for InsufficientCapacityErrorVal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f, "arrayvec insufficient capacity")
    }
}

impl<T> Debug for InsufficientCapacityErrorVal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "arrayvec::InsufficientCapacityErrorVal<{}>",
            type_name::<T>()
        )
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T> std::error::Error for InsufficientCapacityErrorVal<T> {}

// ---------------------------------------------------------------------------

/// An error returned from [`try_insert`] method.
///
/// [`try_insert`]: super::ArrayVec::try_insert
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum InsertError {
    /// Requested index is out of bounds
    InvalidIndex,

    /// There is no spare capacity to accommodate a new element.
    InsufficientCapacity,
}

impl core::fmt::Display for InsertError {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let e = match *self {
            InsertError::InvalidIndex => "index is out of bounds",
            InsertError::InsufficientCapacity => "insufficient capacity",
        };
        write!(f, "arrayvec insert error: {}", e)
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for InsertError {}

// ----------------------------------------------------------------------------

/// An error returned from [`try_insert_val`] method.
///
/// [`try_insert_val`]: super::ArrayVec::try_insert_val
#[derive(Copy, Clone)]
pub enum InsertErrorVal<T> {
    /// Requested index is out of bounds
    InvalidIndex(T),

    /// There is no spare capacity to accommodate a new element.
    InsufficientCapacity(T),
}

impl<T> InsertErrorVal<T> {
    /// Returns a reference to the value conveyed by the error.
    #[inline]
    pub fn value(&self) -> &T {
        match self {
            Self::InvalidIndex(v) => v,
            Self::InsufficientCapacity(v) => v,
        }
    }

    /// Returns the value conveyed by the error.
    #[inline]
    pub fn into_value(self) -> T {
        match self {
            Self::InvalidIndex(v) => v,
            Self::InsufficientCapacity(v) => v,
        }
    }
}

impl<T> Display for InsertErrorVal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let e = match self {
            Self::InvalidIndex(_) => "index is out of bounds",
            Self::InsufficientCapacity(_) => "insufficient capacity",
        };
        write!(f, "arrayvec insert error: {}", e)
    }
}

impl<T> Debug for InsertErrorVal<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let name = type_name::<T>();
        match self {
            Self::InvalidIndex(_) => write!(f, "InsertErrorVal<{}>::InvalidIndex", name),
            Self::InsufficientCapacity(_) => {
                write!(f, "InsertErrorVal<{}>::InsufficientCapacity", name)
            }
        }
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T> std::error::Error for InsertErrorVal<T> {}

// ----------------------------------------------------------------------------

#[cfg(all(test, feature = "std"))]
mod testing {
    use super::*;

    #[test]
    fn test_capacity_error_display() {
        let e = InsufficientCapacityError {};
        let s = format!("{}", e);
        assert_eq!(s, "arrayvec insufficient capacity");
    }

    #[test]
    fn test_capacity_error_debug() {
        let e = InsufficientCapacityError {};
        let s = format!("{:?}", e);
        assert_eq!(s, "InsufficientCapacityError");
    }

    #[test]
    fn test_capacity_error_copy() {
        let e = InsufficientCapacityError {};
        let e2 = e;
        format!("{} {}", e, e2);
    }

    #[test]
    fn test_capacity_error_clone() {
        let e = InsufficientCapacityError {};
        let e2 = e.clone();
        format!("{} {}", e, e2);
    }

    #[test]
    fn test_capacity_error_val_display() {
        let e = InsufficientCapacityErrorVal::<u64>(17);
        let s = format!("{}", e);
        assert_eq!(s, "arrayvec insufficient capacity")
    }

    #[test]
    fn test_capacity_error_val_debug() {
        let e = InsufficientCapacityErrorVal::<u64>(717);
        let s = format!("{:?}", e);
        assert_eq!(s, "arrayvec::InsufficientCapacityErrorVal<u64>")
    }

    #[test]
    fn test_capacity_error_val_clone() {
        let e = InsufficientCapacityErrorVal::<String>("-11".into());
        let c = e.clone();
        assert_eq!(e.0, c.0);
        assert_eq!(e.0, "-11");
    }

    #[test]
    fn test_insert_error_display() {
        let e = InsertError::InsufficientCapacity;
        let s = format!("{}", e);
        assert_eq!(s, "arrayvec insert error: insufficient capacity");

        let e = InsertError::InvalidIndex;
        let s = format!("{}", e);
        assert_eq!(s, "arrayvec insert error: index is out of bounds");
    }

    #[test]
    fn test_insert_error_debug() {
        let e = InsertError::InvalidIndex;
        let s = format!("{:?}", e);
        assert_eq!(s, "InvalidIndex");
    }

    #[test]
    fn test_insert_error_val_display() {
        let e = InsertErrorVal::<u64>::InvalidIndex(7);
        let s = format!("{}", e);
        assert_eq!(s, "arrayvec insert error: index is out of bounds");

        let e = InsertErrorVal::<u64>::InsufficientCapacity(17);
        let s = format!("{}", e);
        assert_eq!(s, "arrayvec insert error: insufficient capacity");
    }

    #[test]
    fn test_insert_error_val_debug() {
        let e = InsertErrorVal::<u64>::InvalidIndex(7);
        let s = format!("{:?}", e);
        assert_eq!(s, "InsertErrorVal<u64>::InvalidIndex");

        let e = InsertErrorVal::<u64>::InsufficientCapacity(17);
        let s = format!("{:?}", e);
        assert_eq!(s, "InsertErrorVal<u64>::InsufficientCapacity");
    }

    #[test]
    fn test_insert_error_val_copy() {
        let e = InsertErrorVal::<u64>::InvalidIndex(17);
        let e2 = e;
        assert_eq!(e.value(), e2.value());

        let e = InsertErrorVal::<u64>::InsufficientCapacity(717);
        let e2 = e;
        assert_eq!(e.value(), e2.value());
    }

    #[test]
    fn test_insert_error_val_clone() {
        let e = InsertErrorVal::<String>::InvalidIndex("17".into());
        let e2 = e.clone();
        assert_eq!(e.value(), e2.value());
        assert_eq!(e.value(), "17");

        let e = InsertErrorVal::<String>::InsufficientCapacity("717".into());
        let e2 = e.clone();
        assert_eq!(e.value(), e2.value());
        assert_eq!(e.value(), "717");
    }

    #[test]
    fn test_insert_error_val_into_value() {
        let e = InsertErrorVal::<String>::InvalidIndex("Hello, world!".into());
        let v = e.into_value();
        assert_eq!(v, "Hello, world!");

        let e = InsertErrorVal::<String>::InsufficientCapacity("Hello again!".into());
        let v = e.into_value();
        assert_eq!(v, "Hello again!");
    }
}