//! `Vec1` represents a non-empty `Vec`. use std::{ error, fmt::{self, Debug}, ops, }; /// `Vec1` represents a non-empty `Vec`. It derefs to `Vec` /// directly. #[derive(Clone, PartialEq)] pub struct Vec1(Vec) where T: Debug; /// Represents the only error that can be emitted by `Vec1`, i.e. when /// the number of items is zero. #[derive(Debug)] pub struct EmptyVec; impl error::Error for EmptyVec {} impl fmt::Display for EmptyVec { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { write!(formatter, "Vec1 must as least contain one item, zero given") } } impl Vec1 where T: Debug, { /// Creates a new non-empty vector, based on an inner `Vec`. If /// the inner vector is empty, a `EmptyVec` error is returned. pub fn new(items: Vec) -> Result { if items.len() == 0 { Err(EmptyVec) } else { Ok(Self(items)) } } } impl fmt::Debug for Vec1 where T: Debug, { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { write!(formatter, "{:?}", self.0) } } impl ops::Deref for Vec1 where T: Debug, { type Target = Vec; fn deref(&self) -> &Self::Target { &self.0 } }