rocket_http_community/
ext.rs

1//! Extension traits implemented by several HTTP types.
2
3use std::borrow::Cow;
4
5use state::InitCell;
6
7/// Trait implemented by types that can be converted into owned versions of
8/// themselves.
9pub trait IntoOwned {
10    /// The owned version of the type.
11    type Owned: 'static;
12
13    /// Converts `self` into an owned version of itself.
14    fn into_owned(self) -> Self::Owned;
15}
16
17impl<T: IntoOwned> IntoOwned for Option<T> {
18    type Owned = Option<T::Owned>;
19
20    #[inline(always)]
21    fn into_owned(self) -> Self::Owned {
22        self.map(|inner| inner.into_owned())
23    }
24}
25
26impl<T: IntoOwned> IntoOwned for Vec<T> {
27    type Owned = Vec<T::Owned>;
28
29    #[inline(always)]
30    fn into_owned(self) -> Self::Owned {
31        self.into_iter().map(|inner| inner.into_owned()).collect()
32    }
33}
34
35impl<T: IntoOwned + Send + Sync> IntoOwned for InitCell<T>
36where
37    T::Owned: Send + Sync,
38{
39    type Owned = InitCell<T::Owned>;
40
41    #[inline(always)]
42    fn into_owned(self) -> Self::Owned {
43        self.map(|inner| inner.into_owned())
44    }
45}
46
47impl<A: IntoOwned, B: IntoOwned> IntoOwned for (A, B) {
48    type Owned = (A::Owned, B::Owned);
49
50    #[inline(always)]
51    fn into_owned(self) -> Self::Owned {
52        (self.0.into_owned(), self.1.into_owned())
53    }
54}
55
56impl<B: 'static + ToOwned + ?Sized> IntoOwned for Cow<'_, B> {
57    type Owned = Cow<'static, B>;
58
59    #[inline(always)]
60    fn into_owned(self) -> <Self as IntoOwned>::Owned {
61        Cow::Owned(self.into_owned())
62    }
63}
64
65macro_rules! impl_into_owned_self {
66    ($($T:ty),*) => ($(
67        impl IntoOwned for $T {
68            type Owned = Self;
69
70            #[inline(always)]
71            fn into_owned(self) -> <Self as IntoOwned>::Owned {
72                self
73            }
74        }
75    )*)
76}
77
78impl_into_owned_self!(bool);
79impl_into_owned_self!(u8, u16, u32, u64, usize);
80impl_into_owned_self!(i8, i16, i32, i64, isize);
81
82use std::path::Path;
83
84// Outside of http, this is used by a test.
85#[doc(hidden)]
86pub trait Normalize {
87    fn normalized_str(&self) -> Cow<'_, str>;
88}
89
90impl<T: AsRef<Path>> Normalize for T {
91    #[cfg(windows)]
92    fn normalized_str(&self) -> Cow<'_, str> {
93        self.as_ref().to_string_lossy().replace('\\', "/").into()
94    }
95
96    #[cfg(not(windows))]
97    fn normalized_str(&self) -> Cow<'_, str> {
98        self.as_ref().to_string_lossy()
99    }
100}