//! Extension traits implemented by several HTTP types. use smallvec::{Array, SmallVec}; // TODO: It would be nice if we could somehow have one trait that could give us // either SmallVec or Vec. /// Trait implemented by types that can be converted into a collection. pub trait IntoCollection { /// Converts `self` into a collection. fn into_collection>(self) -> SmallVec; #[doc(hidden)] fn mapped U, A: Array>(self, f: F) -> SmallVec; } impl IntoCollection for T { #[inline] fn into_collection>(self) -> SmallVec { let mut vec = SmallVec::new(); vec.push(self); vec } #[inline(always)] fn mapped U, A: Array>(self, mut f: F) -> SmallVec { f(self).into_collection() } } impl IntoCollection for Vec { #[inline(always)] fn into_collection>(self) -> SmallVec { SmallVec::from_vec(self) } #[inline] fn mapped U, A: Array>(self, f: F) -> SmallVec { self.into_iter().map(f).collect() } } macro_rules! impl_for_slice { ($($size:tt)*) => ( impl<'a, T: Clone> IntoCollection for &'a [T $($size)*] { #[inline(always)] fn into_collection>(self) -> SmallVec { self.iter().cloned().collect() } #[inline] fn mapped>(self, f: F) -> SmallVec where F: FnMut(T) -> U { self.iter().cloned().map(f).collect() } } ) } impl_for_slice!(); impl_for_slice!(; 1); impl_for_slice!(; 2); impl_for_slice!(; 3); impl_for_slice!(; 4); impl_for_slice!(; 5); impl_for_slice!(; 6); impl_for_slice!(; 7); impl_for_slice!(; 8); impl_for_slice!(; 9); impl_for_slice!(; 10); use std::borrow::Cow; /// Trait implemented by types that can be converted into owned versions of /// themselves. pub trait IntoOwned { /// The owned version of the type. type Owned: 'static; /// Converts `self` into an owned version of itself. fn into_owned(self) -> Self::Owned; } impl IntoOwned for Option { type Owned = Option; #[inline(always)] fn into_owned(self) -> Self::Owned { self.map(|inner| inner.into_owned()) } } impl<'a, B: 'static + ToOwned + ?Sized> IntoOwned for Cow<'a, B> { type Owned = Cow<'static, B>; #[inline(always)] fn into_owned(self) -> Self::Owned { Cow::Owned(self.into_owned()) } }