1 // SPDX-License-Identifier: GPL-2.0 2 3 //! Errors for the [`Vec`] type. 4 5 use crate::{ 6 fmt, 7 prelude::*, // 8 }; 9 10 /// Error type for [`Vec::push_within_capacity`]. 11 pub struct PushError<T>(pub T); 12 13 impl<T> fmt::Debug for PushError<T> { 14 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 15 write!(f, "Not enough capacity") 16 } 17 } 18 19 impl<T> From<PushError<T>> for Error { 20 #[inline] 21 fn from(_: PushError<T>) -> Error { 22 // Returning ENOMEM isn't appropriate because the system is not out of memory. The vector 23 // is just full and we are refusing to resize it. 24 EINVAL 25 } 26 } 27 28 /// Error type for [`Vec::remove`]. 29 pub struct RemoveError; 30 31 impl fmt::Debug for RemoveError { 32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 33 write!(f, "Index out of bounds") 34 } 35 } 36 37 impl From<RemoveError> for Error { 38 #[inline] 39 fn from(_: RemoveError) -> Error { 40 EINVAL 41 } 42 } 43 44 /// Error type for [`Vec::insert_within_capacity`]. 45 pub enum InsertError<T> { 46 /// The value could not be inserted because the index is out of bounds. 47 IndexOutOfBounds(T), 48 /// The value could not be inserted because the vector is out of capacity. 49 OutOfCapacity(T), 50 } 51 52 impl<T> fmt::Debug for InsertError<T> { 53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 54 match self { 55 InsertError::IndexOutOfBounds(_) => write!(f, "Index out of bounds"), 56 InsertError::OutOfCapacity(_) => write!(f, "Not enough capacity"), 57 } 58 } 59 } 60 61 impl<T> From<InsertError<T>> for Error { 62 #[inline] 63 fn from(_: InsertError<T>) -> Error { 64 EINVAL 65 } 66 } 67