StaticCow

Trait StaticCow 

Source
pub trait StaticCow<B>: Deref<Target = B> + IntoOwning
where B: ToOwned + ?Sized,
{ // 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§

Source

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.

Source

fn is_owned(&self) -> bool

Returns true iff the data is owned, i.e. if self.into_owning() would be a no-op.

Source

fn into_cow<'a>(self) -> Cow<'a, B>
where Self: 'a, Self::Owning: 'a,

Converts self into its dynamic equivalent as a Cow.

Source

fn into_owned(self) -> B::Owned

Converts self into a B::Owned, cloning only if necessary.

Provided Methods§

Source

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.

Implementations on Foreign Types§

Source§

impl<'a, B> StaticCow<B> for Cow<'a, B>
where B: 'a + ToOwned + ?Sized,

Source§

fn is_owned(&self) -> bool

Source§

fn mut_if_owned(&mut self) -> MutIfOwned<'_, B>

Source§

fn into_cow<'b>(self) -> Cow<'b, B>
where Self: 'b,

Source§

fn into_owned(self) -> <B as ToOwned>::Owned

Implementors§

Source§

impl<'b, B> StaticCow<B> for Borrowed<'b, B>
where B: ToOwned + ?Sized,

Source§

impl<B> StaticCow<B> for Owned<B>
where B: ToOwned + ?Sized,