use std::{
borrow::Borrow,
fmt::{Debug, Display},
hash::Hash,
ops::Deref,
path::{Path, PathBuf},
sync::Arc,
};
pub enum CowArc<'a, T: ?Sized + 'static> {
Borrowed(&'a T),
Static(&'static T),
Owned(Arc<T>),
}
impl<'a, T: ?Sized> Deref for CowArc<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
match self {
CowArc::Borrowed(v) | CowArc::Static(v) => v,
CowArc::Owned(v) => v,
}
}
}
impl<'a, T: ?Sized> Borrow<T> for CowArc<'a, T> {
#[inline]
fn borrow(&self) -> &T {
self
}
}
impl<'a, T: ?Sized> AsRef<T> for CowArc<'a, T> {
#[inline]
fn as_ref(&self) -> &T {
self
}
}
impl<'a, T: ?Sized> CowArc<'a, T>
where
&'a T: Into<Arc<T>>,
{
#[inline]
pub fn into_owned(self) -> CowArc<'static, T> {
match self {
CowArc::Borrowed(value) => CowArc::Owned(value.into()),
CowArc::Static(value) => CowArc::Static(value),
CowArc::Owned(value) => CowArc::Owned(value),
}
}
#[inline]
pub fn clone_owned(&self) -> CowArc<'static, T> {
self.clone().into_owned()
}
}
impl<'a, T: ?Sized> Clone for CowArc<'a, T> {
#[inline]
fn clone(&self) -> Self {
match self {
Self::Borrowed(value) => Self::Borrowed(value),
Self::Static(value) => Self::Static(value),
Self::Owned(value) => Self::Owned(value.clone()),
}
}
}
impl<'a, T: PartialEq + ?Sized> PartialEq for CowArc<'a, T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.deref().eq(other.deref())
}
}
impl<'a, T: PartialEq + ?Sized> Eq for CowArc<'a, T> {}
impl<'a, T: Hash + ?Sized> Hash for CowArc<'a, T> {
#[inline]
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.deref().hash(state);
}
}
impl<'a, T: Debug + ?Sized> Debug for CowArc<'a, T> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self.deref(), f)
}
}
impl<'a, T: Display + ?Sized> Display for CowArc<'a, T> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.deref(), f)
}
}
impl<'a, T: PartialOrd + ?Sized> PartialOrd for CowArc<'a, T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.deref().partial_cmp(other.deref())
}
}
impl Default for CowArc<'static, str> {
fn default() -> Self {
CowArc::Static(Default::default())
}
}
impl Default for CowArc<'static, Path> {
fn default() -> Self {
CowArc::Static(Path::new(""))
}
}
impl<'a, T: Ord + ?Sized> Ord for CowArc<'a, T> {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.deref().cmp(other.deref())
}
}
impl From<PathBuf> for CowArc<'static, Path> {
#[inline]
fn from(value: PathBuf) -> Self {
CowArc::Owned(value.into())
}
}
impl From<&'static str> for CowArc<'static, Path> {
#[inline]
fn from(value: &'static str) -> Self {
CowArc::Static(Path::new(value))
}
}
impl From<String> for CowArc<'static, str> {
#[inline]
fn from(value: String) -> Self {
CowArc::Owned(value.into())
}
}
impl<'a> From<&'a String> for CowArc<'a, str> {
#[inline]
fn from(value: &'a String) -> Self {
CowArc::Borrowed(value)
}
}
impl<T: ?Sized> From<&'static T> for CowArc<'static, T> {
#[inline]
fn from(value: &'static T) -> Self {
CowArc::Static(value)
}
}