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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::color::Color;
use crate::{Badge, BadgeKind};
use std::borrow::Cow;

/// Converts a type to an owned version
pub trait AsOwned: private::AsOwnedSealed {
    /// The owned type
    type Owned: 'static;
    /// Get an owned version
    fn as_owned(&self) -> Self::Owned;
}

/// Helper to reborrow a Cow without moving it
pub trait Reborrow<'a>: private::ReborrowSealed
where
    Self: 'a,
{
    /// The borrowed output
    type Output: 'a;
    /// Reborrow self.
    ///
    /// This is intended for internal use.
    fn reborrow(&self) -> Self::Output;
}

impl<'a> Reborrow<'a> for &'a Cow<'a, str> {
    type Output = Cow<'a, str>;
    fn reborrow(&self) -> Self::Output {
        match self {
            Cow::Borrowed(s) => Cow::Borrowed(*s),
            Cow::Owned(s) => Cow::Borrowed(s.as_ref()),
        }
    }
}

impl<'a> Reborrow<'a> for &'a Option<Cow<'a, str>> {
    type Output = Option<Cow<'a, str>>;
    fn reborrow(&self) -> Self::Output {
        self.as_ref().map(|s| s.reborrow())
    }
}

impl<'a> Reborrow<'a> for Option<&'a Cow<'a, str>> {
    type Output = Option<Cow<'a, str>>;
    fn reborrow(&self) -> Self::Output {
        self.map(|s| s.reborrow())
    }
}

mod private {
    pub trait AsOwnedSealed {}
    impl<T> AsOwnedSealed for T where T: crate::AsOwned {}

    pub trait ReborrowSealed {}
    impl<'a, T> ReborrowSealed for T where T: crate::Reborrow<'a> {}
}

impl AsOwned for bool {
    type Owned = Self;
    fn as_owned(&self) -> Self::Owned {
        *self
    }
}

impl AsOwned for usize {
    type Owned = Self;
    fn as_owned(&self) -> Self::Owned {
        *self
    }
}

impl<'a> AsOwned for Cow<'a, str> {
    type Owned = Cow<'static, str>;
    fn as_owned(&self) -> <Self as AsOwned>::Owned {
        match self {
            Cow::Borrowed(r) => Cow::Owned(r.to_owned().into()),
            Cow::Owned(s) => Cow::Owned(s.to_owned()),
        }
    }
}

impl<T: AsOwned + Clone> AsOwned for Option<T> {
    type Owned = Option<T::Owned>;
    fn as_owned(&self) -> Self::Owned {
        self.clone().map(|s| s.as_owned())
    }
}

impl<T: AsOwned + Clone> AsOwned for Vec<T> {
    type Owned = Vec<T::Owned>;
    fn as_owned(&self) -> Self::Owned {
        self.iter().cloned().map(|s| s.as_owned()).collect()
    }
}

impl AsOwned for Color {
    type Owned = Self;
    fn as_owned(&self) -> Self::Owned {
        *self
    }
}

impl<'t> AsOwned for BadgeKind<'t> {
    type Owned = BadgeKind<'static>;
    fn as_owned(&self) -> Self::Owned {
        match self {
            BadgeKind::Admin => BadgeKind::Admin,
            BadgeKind::Bits => BadgeKind::Bits,
            BadgeKind::Broadcaster => BadgeKind::Broadcaster,
            BadgeKind::GlobalMod => BadgeKind::GlobalMod,
            BadgeKind::Moderator => BadgeKind::Moderator,
            BadgeKind::Subscriber => BadgeKind::Subscriber,
            BadgeKind::Staff => BadgeKind::Staff,
            BadgeKind::Turbo => BadgeKind::Turbo,
            BadgeKind::Premium => BadgeKind::Premium,
            BadgeKind::VIP => BadgeKind::VIP,
            BadgeKind::Partner => BadgeKind::Partner,
            BadgeKind::Unknown(s) => BadgeKind::Unknown(s.as_owned()),
        }
    }
}

impl<'t> AsOwned for Badge<'t> {
    type Owned = Badge<'static>;
    fn as_owned(&self) -> Self::Owned {
        Badge {
            kind: self.kind.as_owned(),
            data: self.data.as_owned(),
        }
    }
}

impl<'a> AsOwned for crate::Tags<'a> {
    type Owned = crate::Tags<'static>;
    fn as_owned(&self) -> Self::Owned {
        let map = self.0.iter().map(|(k, v)| (k.as_owned(), v.as_owned()));
        crate::Tags(map.collect())
    }
}

impl<'t> AsOwned for crate::decode::Prefix<'t> {
    type Owned = crate::decode::Prefix<'static>;
    fn as_owned(&self) -> Self::Owned {
        match self {
            crate::decode::Prefix::User { nick } => crate::decode::Prefix::User {
                nick: nick.as_owned(),
            },
            crate::decode::Prefix::Server { host } => crate::decode::Prefix::Server {
                host: host.as_owned(),
            },
        }
    }
}

impl<'t> AsOwned for crate::decode::Message<'t> {
    type Owned = crate::decode::Message<'static>;
    fn as_owned(&self) -> Self::Owned {
        crate::decode::Message {
            raw: self.raw.as_owned(),
            tags: self.tags.as_owned(),
            prefix: self.prefix.as_owned(),
            command: self.command.as_owned(),
            args: self.args.as_owned(),
            data: self.data.as_owned(),
        }
    }
}