pub trait Idempotent<T>: IntoOwning<Owning = Keep<T>>{
// 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:
-
It can give you either a
Tor aT::Owning, and tells you which. -
It constrains
Tsuch thatT::Owning::Owning = T::Owning. This means that you can callinto_owning()on it as many times as you please and it can still give you either aTor aT::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§
Provided Methods§
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.