// Copyright 2016 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. /// A vector type optimized for cases where this size is usually 0 (c.f. `SmallVector`). /// The `Option>` wrapping allows us to represent a zero sized vector with `None`, /// which uses only a single (null) pointer. #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, 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 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(), } } }