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
#![forbid(unsafe_code)]
#![deny(
    clippy::dbg_macro,
    missing_copy_implementations,
    rustdoc::missing_crate_level_docs,
    missing_debug_implementations,
    nonstandard_style,
    unused_qualifications
)]
#![warn(missing_docs)]

/*!
This crate offers a small utility type, [`SmartCow`] that holds either
a [`&str`] or a [`smartstring::SmartString`] and offers various
convenience trait implementations.
*/
use smartstring::{LazyCompact, SmartString};
use std::{
    borrow::Cow,
    fmt::{Debug, Display, Formatter, Result},
    hash::{Hash, Hasher},
    ops::Deref,
};

/// The primary type for this crate, which holds either a borrowed str
/// or a [`SmartString`]
#[derive(Clone)]
pub enum SmartCow<'a> {
    /// a [`std::str`] borrow
    Borrowed(&'a str),
    /// a [`smartstring::SmartString`]
    Owned(SmartString<LazyCompact>),
}

impl SmartCow<'_> {
    /// Build a 'static duplicate of this SmartCow by cloning a
    /// reference. If you own the SmartCow, [`SmartCow::into_owned`]
    /// is preferable, as it will not allocate an unnecesssary owned
    /// value.
    ///
    /// This is identical to calling `self.clone().into_owned()`,
    /// which first allocates in the case of an owned variant by
    /// cloning, and then allocates in the case of a borrowed variant,
    /// with into_owned.
    pub fn to_owned(&self) -> SmartCow<'static> {
        self.clone().into_owned()
    }

    /// Build a 'static duplicate of this SmartCow by value, by
    /// allocating a SmartString if self is a SmartCow::Borrowed
    /// variant. If you do not have ownership of a SmartCow and need
    /// to create a 'static SmartCow from it by reference, use
    /// [`SmartCow::to_owned`]
    pub fn into_owned(self) -> SmartCow<'static> {
        match self {
            SmartCow::Borrowed(b) => SmartCow::Owned(SmartString::from(b)),
            SmartCow::Owned(o) => SmartCow::Owned(o),
        }
    }
}

impl Default for SmartCow<'_> {
    fn default() -> Self {
        Self::Borrowed("")
    }
}

impl PartialEq<SmartCow<'_>> for SmartCow<'_> {
    fn eq(&self, other: &SmartCow<'_>) -> bool {
        **self == **other
    }
}

impl Eq for SmartCow<'_> {}

impl PartialEq<&str> for SmartCow<'_> {
    fn eq(&self, other: &&str) -> bool {
        &**self == *other
    }
}

impl PartialEq<String> for SmartCow<'_> {
    fn eq(&self, other: &String) -> bool {
        **self == **other
    }
}

impl PartialEq<&String> for SmartCow<'_> {
    fn eq(&self, other: &&String) -> bool {
        **self == **other
    }
}

impl Debug for SmartCow<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        Debug::fmt(&**self, f)
    }
}

impl Display for SmartCow<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        f.write_str(&**self)
    }
}

impl AsRef<str> for SmartCow<'_> {
    fn as_ref(&self) -> &str {
        &*self
    }
}

impl AsRef<[u8]> for SmartCow<'_> {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl<'a> From<&'a str> for SmartCow<'a> {
    fn from(s: &'a str) -> Self {
        Self::Borrowed(s)
    }
}

impl<'a> From<Cow<'a, str>> for SmartCow<'a> {
    fn from(s: Cow<'a, str>) -> Self {
        match s {
            Cow::Owned(string) => Self::Owned(SmartString::from(string)),
            Cow::Borrowed(s) => Self::Borrowed(s),
        }
    }
}

impl From<String> for SmartCow<'_> {
    fn from(s: String) -> Self {
        Self::Owned(SmartString::from(s))
    }
}

impl From<SmartString<LazyCompact>> for SmartCow<'_> {
    fn from(s: SmartString<LazyCompact>) -> Self {
        Self::Owned(s)
    }
}

impl Deref for SmartCow<'_> {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Borrowed(b) => *b,
            Self::Owned(o) => o,
        }
    }
}

impl Hash for SmartCow<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.deref().hash(state)
    }
}