use std::borrow::Borrow;
use std::hash::Hash;
use std::ops::Deref;
use std::sync::Arc;
use allocative::Allocative;
use dupe::Dupe;
use crate::util::arc_or_static::ArcOrStatic;
#[derive(
Clone,
Dupe,
Eq,
PartialEq,
Hash,
Ord,
PartialOrd,
Debug,
derive_more::Display,
Allocative
)]
#[display("{}", &**self)]
pub struct ArcStr(ArcOrStatic<str>);
impl ArcStr {
pub fn new_static(s: &'static str) -> ArcStr {
ArcStr(ArcOrStatic::new_static(s))
}
pub fn as_str(&self) -> &str {
self
}
}
impl Deref for ArcStr {
type Target = str;
fn deref(&self) -> &str {
self.0.deref()
}
}
impl Borrow<str> for ArcStr {
fn borrow(&self) -> &str {
self
}
}
impl<'a> From<&'a str> for ArcStr {
fn from(s: &'a str) -> Self {
if s.is_empty() {
ArcStr(ArcOrStatic::new_static(""))
} else {
ArcStr(ArcOrStatic::new_arc(Arc::from(s)))
}
}
}