Polymorph
A set of utilities to better enable polymorphic behavior in Rust.
Ref(Mut)OrOwned
RefOrOwned<T> is an enum over borrowed and owned data. It's similar to std::borrow::Cow. However, while Cow has a ToOwned requirement, RefOrOwned does not.
- There is also a
RefMutOrOwnedversion, for when you need&mut T. into_ownedis available whereT: Clone.- The type implements
From<&T>andFrom<T>, as well asDereftoT
Ref(Mut)OrBox
RefOrBox<T> is an enum over borrowed and boxed data, i.e. &T and Box<T>. It's intended for cases where T is unsized, like when T is a trait object.
RefMutOrBoxis a version ofRefOrBoxwhich uses&mut Tand can be dereferenced to a mutable value.- The type implements
From<&T>andFrom<T>, as well asDereftoT. - Optional support for the dyn-clone crate is provided by the trait-clone feature. If
T: DynClone, aninto_ownedmethod will be made available. More on this later.
Safety
- The library contains no unsafe code
- The library should never panic
Dependency
Add this library to your Cargo.toml:
[]
= "1.0"
trait-clone feature
To enable interoperability with the dyn-clone trait, turn on this feature.
[]
= { = "1.0", = ["trait-clone"]}
This will add a RefOrBox::into_owned method which returns a Box<T>, either by returning the owned box or cloning a borrowed value.
Other crates
Polymorph is well-combined with the dyn-clone and enum-dispatch crates for flexible and effective dynamic programming.
License
Licensed under the Apache License v2.0. See the LICENSE.txt.