Crate shallowclone

Crate shallowclone 

Source
Expand description

ShallowClone trait to make it easier to work with copy-on-write values efficiently.

This is basically the same as the standard Clone, except that it’s optimized for copy-on-write values so that they’re not cloned. Shallow cloning a Cow will always produce a Borrowed variant, either referencing the original value if it was Owned, or the value that the original value referenced if it already was Borrowed.

This crate also introduces a MakeOwned trait which is the opposite of ShallowClone. It takes any value that implements the trait and returns an equivalent which is 'static - no references, completely self-sufficient.

Additionally this crate introduces two replacements for the standard Cow<'a, T>:

These types are covariant over T, which solves some problems if your T contains references. In most cases you probably won’t need them, standard Cow works perfectly for things like Cow<'a, str> or Cow<'a, [u8]>, but if your T contains references, the standard Cow will not let you subtype them after shallow cloning, and you will end up with 2 different lifetimes. CoCow and CoCowSlice solve this problem.

Enums§

CoCow
Covariant copy-on-write. This is a simpler version of Cow that doesn’t rely on ToOwned trait and is covariant over T.
CoCowSlice
Covariant copy-on-write slice. This is a specialised version of CoCow for slices and allows you to have slices without an underlying allocated type like Vec if you wish.

Traits§

MakeOwned
Takes a value and transforms it to be 'static, cloning parts if necessary
ShallowClone
The same as Clone, but doesnt clone Cow values, instead it just borrows them.

Derive Macros§

MakeOwned
Automatically derives the MakeOwned trait
ShallowClone
Automatically derives the ShallowClone trait