Idempotent

Trait Idempotent 

Source
pub trait Idempotent<T>: IntoOwning<Owning = Keep<T>>
where T: ToOwning, T::Owning: ToOwning<Owning = T::Owning>,
{ // Required methods fn to_ref(&self) -> IdemRef<'_, T>; fn to_mut(&mut self) -> IdemMut<'_, T>; // Provided method fn into_kept(self) -> T::Owning { ... } }
Expand description

A trait which guarantees Self::Owning::Owning = Self::Owning.

Using Idempotent as a bound allows you to be generic over types that implement IntoOwning but not ToOwned.

StaticCow<B> has Deref<Target=B> as a supertrait, so you can do anything with a StaticCow<B> that you can do with a &B. However, in order to provide this supertrait, its implementations require that B : ToOwned so that they can rely on having B::Owned : Borrow<B>.

Idempotent has weaker requirements, so its capabilities are necessarily weaker as well, and it does not inherit from Deref. ToOwning places no constraints on Owning which means that as far as the type system is concerned, .into_owning() is just a completely arbitrary conversion. So, you can’t do anything useful with a type that might be T or might be T::Owning but you don’t know which, because they don’t promise to have any traits in common.

Idempotent puts back just enough information that it can be a useful bound:

  1. It can give you either a T or a T::Owning, and tells you which.

  2. It constrains T such that T::Owning::Owning = T::Owning. This means that you can call into_owning() on it as many times as you please and it can still give you either a T or a T::Owning.

Idempotent<T> is implemented by Change<T>, which holds a T; Keep<T>, which holds a T::Owning; and by ChangeOrKeep<T> which might hold either, determined at runtime. Calling .to_owning() or .into_owning() on an Idempotent<T> always gives a Keep<T>.

Required Methods§

Source

fn to_ref(&self) -> IdemRef<'_, T>

Get a reference to either a T or a T::Owning.

Source

fn to_mut(&mut self) -> IdemMut<'_, T>

Get a mutable reference to either a T or a T::Owning.

Provided Methods§

Source

fn into_kept(self) -> T::Owning

Converts self into a T::Owning; equivalent to into_owning().0.

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.

Implementors§

Source§

impl<T> Idempotent<T> for ChangeOrKeep<T>
where T: IntoOwning, T::Owning: ToOwning<Owning = T::Owning>,

Source§

impl<T> Idempotent<T> for Change<T>
where T: IntoOwning, T::Owning: ToOwning<Owning = T::Owning>,

Source§

impl<T> Idempotent<T> for Keep<T>
where T: IntoOwning, T::Owning: ToOwning<Owning = T::Owning>,