mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-30 22:34:13 +00:00
54 lines
1.1 KiB
Rust
54 lines
1.1 KiB
Rust
|
use std::sync::Arc;
|
||
|
|
||
|
pub enum ArcCow<'a, T: ?Sized> {
|
||
|
Borrowed(&'a T),
|
||
|
Owned(Arc<T>),
|
||
|
}
|
||
|
|
||
|
impl<'a, T: ?Sized> Clone for ArcCow<'a, T> {
|
||
|
fn clone(&self) -> Self {
|
||
|
match self {
|
||
|
Self::Borrowed(borrowed) => Self::Borrowed(borrowed),
|
||
|
Self::Owned(owned) => Self::Owned(owned.clone()),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a, T: ?Sized> From<&'a T> for ArcCow<'a, T> {
|
||
|
fn from(s: &'a T) -> Self {
|
||
|
Self::Borrowed(s)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T> From<Arc<T>> for ArcCow<'_, T> {
|
||
|
fn from(s: Arc<T>) -> Self {
|
||
|
Self::Owned(s)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<String> for ArcCow<'_, str> {
|
||
|
fn from(value: String) -> Self {
|
||
|
Self::Owned(value.into())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T: ?Sized> std::ops::Deref for ArcCow<'_, T> {
|
||
|
type Target = T;
|
||
|
|
||
|
fn deref(&self) -> &Self::Target {
|
||
|
match self {
|
||
|
ArcCow::Borrowed(s) => s,
|
||
|
ArcCow::Owned(s) => s.as_ref(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<T: ?Sized> AsRef<T> for ArcCow<'_, T> {
|
||
|
fn as_ref(&self) -> &T {
|
||
|
match self {
|
||
|
ArcCow::Borrowed(borrowed) => borrowed,
|
||
|
ArcCow::Owned(owned) => owned.as_ref(),
|
||
|
}
|
||
|
}
|
||
|
}
|