pub trait StaticCow<B>: Deref<Target = B> + IntoOwning{
// Required methods
fn mut_if_owned(&mut self) -> MutIfOwned<'_, B>;
fn is_owned(&self) -> bool;
fn into_cow<'a>(self) -> Cow<'a, B>
where Self: 'a,
Self::Owning: 'a;
fn into_owned(self) -> B::Owned;
// Provided method
fn is_borrowed(&self) -> bool { ... }
}
Expand description
Trait for Cow
-like types whose owned-ness might be known at
compile-time.
StaticCow
is std::borrow::Cow
lifted to the type level. While
Cow
is an enum, StaticCow
is a trait. While Cow::Borrowed
and
Cow::Owned
are enum variants, this crate’s Borrowed
and Owned
are
tuple structs which implement StaticCow
(so also does Cow
). oS instead
of having a struct with a field field: Cow<'a, B>
, you can declare that
field as field: S
and let S
be a generic parameter S: StaticCow<B>
Required Methods§
Sourcefn mut_if_owned(&mut self) -> MutIfOwned<'_, B>
fn mut_if_owned(&mut self) -> MutIfOwned<'_, B>
Returns either an immutable reference to an object that is borrowed, or a mutable reference to one which is owned.
This method is useful if you are implementing an object that does not need to mutate its contents, but can implement optimizations if allowed to.
Borrowed
::mut_if_owned()
always returns MutIfOwned::Const(_)
,
Owned
::mut_if_owned()
always returns MutIfOwned::Mut(_)
, and
both of these method implementations are compiled with
#[inline(always)]
. Therefore, if you have code that is generic over
StaticCow
, there is zero cost to calling .mut_if_owned()
and
matching on the result, because the dead branch will reliably be
optimized out.
Sourcefn is_owned(&self) -> bool
fn is_owned(&self) -> bool
Returns true iff the data is owned, i.e. if self.into_owning()
would
be a no-op.
Sourcefn into_cow<'a>(self) -> Cow<'a, B>where
Self: 'a,
Self::Owning: 'a,
fn into_cow<'a>(self) -> Cow<'a, B>where
Self: 'a,
Self::Owning: 'a,
Converts self
into its dynamic equivalent as a Cow
.
Sourcefn into_owned(self) -> B::Owned
fn into_owned(self) -> B::Owned
Converts self
into a B::Owned
, cloning only if necessary.
Provided Methods§
Sourcefn is_borrowed(&self) -> bool
fn is_borrowed(&self) -> bool
Returns true iff the data is borrowed, i.e. if self.into_owning()
would clone it.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.