pub enum CowArc<'a, T: ?Sized + 'static> {
Borrowed(&'a T),
Static(&'static T),
Owned(Arc<T>),
}
Expand description
Much like a Cow
, but owned values are Arc
-ed to make clones cheap.
This should be used for values that are cloned for use across threads and change rarely (if
ever).
This also makes an opinionated tradeoff by adding a CowArc::Static
and implementing
From<&'static T>
instead of From<'a T>
. This preserves the static context and prevents
conversion to CowArc::Owned
in cases where a reference is known to be static. This is an
optimization that prevents allocations and atomic ref-counting.
This means that static references should prefer CowArc::from
or CowArc::Static
and
non-static references must use CowArc::Borrowed
.
Variants§
Borrowed(&'a T)
A borrowed value.
Static(&'static T)
A static value reference.
This exists to avoid conversion to CowArc::Owned
in cases where a reference is
known to be static. This is an optimization that prevents allocations and atomic
ref-counting.
Owned(Arc<T>)
An owned Arc
-ed value.
Implementations§
source§impl<T: ?Sized + 'static> CowArc<'static, T>
impl<T: ?Sized + 'static> CowArc<'static, T>
sourcepub fn new_owned(value: T) -> Selfwhere
T: Sized,
pub fn new_owned(value: T) -> Selfwhere
T: Sized,
Creates a new CowArc::Owned
from a value.
This is simply a convenience method;
the value will be wrapped in an Arc
.
Note that T
must be Sized
: use the enum constructor directly if T
is unsized.
sourcepub fn new_owned_from_arc(value: impl Into<Arc<T>>) -> Self
pub fn new_owned_from_arc(value: impl Into<Arc<T>>) -> Self
Creates a new CowArc::Owned
from an Arc
-like value.
source§impl<'a, T: ?Sized> CowArc<'a, T>
impl<'a, T: ?Sized> CowArc<'a, T>
sourcepub fn into_owned(self) -> CowArc<'static, T>
pub fn into_owned(self) -> CowArc<'static, T>
Converts this into an “owned” value.
If internally a value is borrowed, it will be cloned into an “owned Arc
”.
If it is already a CowArc::Owned
or a CowArc::Static
, it will remain unchanged.
sourcepub fn clone_owned(&self) -> CowArc<'static, T>
pub fn clone_owned(&self) -> CowArc<'static, T>
Clones into an owned [CowArc<'static>
].
If internally a value is borrowed, it will be cloned into an “owned Arc
”.
If it is already a CowArc::Owned
or CowArc::Static
, the value will be cloned.
This is equivalent to .clone().into_owned()
.
Trait Implementations§
source§impl Default for CowArc<'static, Path>
impl Default for CowArc<'static, Path>
source§fn default() -> Self
fn default() -> Self
Returns an empty Path
, wrapped in CowArc::Static
.
This is equivalent to CowArc::Static(Path::new(""))
.
source§impl<T: ?Sized> From<&'static T> for CowArc<'static, T>
impl<T: ?Sized> From<&'static T> for CowArc<'static, T>
source§fn from(value: &'static T) -> Self
fn from(value: &'static T) -> Self
source§impl<'a, T: Ord + ?Sized> Ord for CowArc<'a, T>
impl<'a, T: Ord + ?Sized> Ord for CowArc<'a, T>
source§impl<'a, T: PartialOrd + ?Sized> PartialOrd for CowArc<'a, T>
impl<'a, T: PartialOrd + ?Sized> PartialOrd for CowArc<'a, T>
impl<'a, T: PartialEq + ?Sized> Eq for CowArc<'a, T>
Auto Trait Implementations§
impl<'a, T> Freeze for CowArc<'a, T>where
T: ?Sized,
impl<'a, T> RefUnwindSafe for CowArc<'a, T>where
T: RefUnwindSafe + ?Sized,
impl<'a, T> Send for CowArc<'a, T>
impl<'a, T> Sync for CowArc<'a, T>
impl<'a, T> Unpin for CowArc<'a, T>where
T: ?Sized,
impl<'a, T> UnwindSafe for CowArc<'a, T>where
T: RefUnwindSafe + ?Sized,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)