use std::borrow::Cow;
use std::cmp::Ordering;
use std::fmt;
use std::hash::Hash;
use std::ops::Deref;
#[derive(Clone, Copy)]
pub enum RefStr<'a> {
Borrowed(&'a str),
Static(&'static str),
}
impl fmt::Debug for RefStr<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.get(), f)
}
}
impl fmt::Display for RefStr<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.get(), f)
}
}
impl<'a> RefStr<'a> {
pub const fn get(&self) -> &'a str {
match self {
RefStr::Borrowed(s) => s,
RefStr::Static(s) => s,
}
}
pub const fn get_static(&self) -> Option<&'static str> {
match self {
RefStr::Borrowed(_) => None,
RefStr::Static(s) => Some(s),
}
}
pub fn into_cow_static(self) -> Cow<'static, str> {
match self {
RefStr::Borrowed(s) => Cow::Owned(ToOwned::to_owned(s)),
RefStr::Static(s) => Cow::Borrowed(s),
}
}
}
impl Deref for RefStr<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.get()
}
}
impl PartialEq for RefStr<'_> {
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(self.get(), other.get())
}
}
impl Eq for RefStr<'_> {}
impl PartialOrd for RefStr<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for RefStr<'_> {
fn cmp(&self, other: &Self) -> Ordering {
Ord::cmp(self.get(), other.get())
}
}
impl Hash for RefStr<'_> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Hash::hash(self.get(), state)
}
}