use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::num::NonZeroUsize;
use std::ops::Deref;
use crate::*;
#[cfg(feature = "singlethreaded")]
use std::rc::Rc;
#[cfg(not(feature = "singlethreaded"))]
use std::sync::Arc;
#[cfg(not(feature = "singlethreaded"))]
pub(crate) type Rc<T> = Arc<T>;
#[derive(Debug, Clone)]
pub enum CowStr {
Static(&'static str),
Shared(Rc<String>),
}
impl CowStr {
#[must_use]
pub fn new() -> Self {
CowStr::Shared(Rc::new(String::new()))
}
#[must_use]
pub fn from_static(s: &'static str) -> Self {
CowStr::Static(s)
}
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn to_mut(&mut self) -> &mut String {
match self {
Self::Static(s) => {
*self = CowStr::Shared(Rc::new(String::from(*s)));
}
Self::Shared(r) => {
Rc::make_mut(r);
}
}
if let CowStr::Shared(rc) = self {
Rc::get_mut(rc).unwrap()
} else {
unsafe { std::hint::unreachable_unchecked() }
}
}
#[must_use]
pub fn into_substr(self) -> SubStr {
let substr = &self[..] as *const str;
SubStr {
string: self,
substr,
}
}
pub fn into_range(self, start: usize, end: usize) -> Result<SubStr, RangeError> {
if let Some(substr) = self.get(start..end) {
let substr = substr as *const str;
Ok(SubStr {
string: self,
substr,
})
} else {
Err(RangeError)
}
}
pub fn deref_static(&self) -> Result<&'static str, &str> {
match self {
Self::Static(s) => Ok(s),
Self::Shared(s) => Err(s),
}
}
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn strong_count(&self) -> Option<NonZeroUsize> {
match self {
Self::Static(_) => None,
Self::Shared(r) => Some(NonZeroUsize::new(Rc::strong_count(r)).unwrap()),
}
}
}
impl Default for CowStr {
fn default() -> Self {
Self::new()
}
}
impl<T> From<T> for CowStr
where
String: From<T>,
{
fn from(source: T) -> Self {
CowStr::Shared(Rc::new(String::from(source)))
}
}
impl fmt::Write for CowStr {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.to_mut().write_str(s)
}
fn write_char(&mut self, c: char) -> fmt::Result {
self.to_mut().write_char(c)
}
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
self.to_mut().write_fmt(args)
}
}
impl Deref for CowStr {
type Target = str;
fn deref(&self) -> &Self::Target {
match self {
Self::Static(s) => s,
Self::Shared(r) => r.as_ref(),
}
}
}
impl AsRef<str> for CowStr {
fn as_ref(&self) -> &str {
match self {
Self::Static(s) => s,
Self::Shared(r) => r.as_ref(),
}
}
}
impl Borrow<str> for CowStr {
fn borrow(&self) -> &str {
match self {
Self::Static(s) => s,
Self::Shared(r) => r.as_ref(),
}
}
}
impl PartialEq for CowStr {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}
impl Eq for CowStr {}
impl PartialOrd for CowStr {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.as_ref().partial_cmp(other.as_ref())
}
}
impl Ord for CowStr {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_ref().cmp(other.as_ref())
}
}
impl Hash for CowStr {
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}
impl fmt::Display for CowStr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&**self, f)
}
}
#[test]
fn smoke() {
let _empty_string = CowStr::new();
let _static_string = CowStr::from_static("test string");
let _dynamic_string = CowStr::from("test string");
}
#[test]
fn push() {
let mut my_string = CowStr::from_static("test string");
my_string.to_mut().push(' ');
my_string.to_mut().push_str("foobar");
assert_eq!(&*my_string, "test string foobar");
}
#[test]
#[ignore]
fn print() {
let my_string = CowStr::from_static("test string");
println!("my_string = {my_string}");
}