1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
pub use torn_api_macros::IntoOwned;

pub trait IntoOwned {
    type Owned;

    fn into_owned(self) -> Self::Owned;
}

impl<T> IntoOwned for Option<T>
where
    T: IntoOwned,
{
    type Owned = Option<T::Owned>;

    fn into_owned(self) -> Self::Owned {
        self.map(IntoOwned::into_owned)
    }
}

impl<T> IntoOwned for Vec<T> where T: IntoOwned {
    type Owned = Vec<<T as IntoOwned>::Owned>;

    fn into_owned(self) -> Self::Owned {
        let mut owned = Vec::with_capacity(self.len());
        for elem in self {
            owned.push(elem.into_owned());
        }
        owned
    }
} 

impl<K, V> IntoOwned for std::collections::HashMap<K, V> where V: IntoOwned, K: Eq + std::hash::Hash {
    type Owned = std::collections::HashMap<K, <V as IntoOwned>::Owned>;

    fn into_owned(self) -> Self::Owned {
        self.into_iter().map(|(k, v)| (k, v.into_owned())).collect()
    }
}

impl<K, V> IntoOwned for std::collections::BTreeMap<K, V> where V: IntoOwned, K: Eq + Ord + std::hash::Hash  {
    type Owned = std::collections::BTreeMap<K, <V as IntoOwned>::Owned>;

    fn into_owned(self) -> Self::Owned {
        self.into_iter().map(|(k, v)| (k, v.into_owned())).collect()
    }
}

impl<Z> IntoOwned for chrono::DateTime<Z> where Z: chrono::TimeZone {
    type Owned = Self;

    fn into_owned(self) -> Self::Owned {
        self
    }
}

impl<'a> IntoOwned for &'a str {
    type Owned = String;

    fn into_owned(self) -> Self::Owned {
        self.to_owned()
    }
}

macro_rules! impl_ident {
    ($name:path) => {
        impl IntoOwned for $name {
            type Owned = $name;
            fn into_owned(self) -> Self::Owned {
                self
            }
        }
    };
}

impl_ident!(i64);
impl_ident!(i32);
impl_ident!(i16);
impl_ident!(i8);
impl_ident!(String);