use crate::stable_hasher::{StableHasher, HashStable}; /// A vector type optimized for cases where this size is usually 0 (cf. `SmallVector`). /// The `Option>` wrapping allows us to represent a zero sized vector with `None`, /// which uses only a single (null) pointer. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct ThinVec(Option>>); impl ThinVec { pub fn new() -> Self { ThinVec(None) } } impl From> for ThinVec { fn from(vec: Vec) -> Self { if vec.is_empty() { ThinVec(None) } else { ThinVec(Some(Box::new(vec))) } } } impl Into> for ThinVec { fn into(self) -> Vec { match self { ThinVec(None) => Vec::new(), ThinVec(Some(vec)) => *vec, } } } impl ::std::ops::Deref for ThinVec { type Target = [T]; fn deref(&self) -> &[T] { match *self { ThinVec(None) => &[], ThinVec(Some(ref vec)) => vec, } } } impl ::std::ops::DerefMut for ThinVec { fn deref_mut(&mut self) -> &mut [T] { match *self { ThinVec(None) => &mut [], ThinVec(Some(ref mut vec)) => vec, } } } impl Extend for ThinVec { fn extend>(&mut self, iter: I) { match *self { ThinVec(Some(ref mut vec)) => vec.extend(iter), ThinVec(None) => *self = iter.into_iter().collect::>().into(), } } } impl, CTX> HashStable for ThinVec { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { (**self).hash_stable(hcx, hasher) } } impl Default for ThinVec { fn default() -> Self { Self(None) } }