polymorph

Crates.iopolymorph
lib.rspolymorph
version1.0.0
created_at2021-10-28 17:13:59.898367+00
updated_at2026-01-23 07:17:40.67868+00
descriptionA few utilities to better enable polymorphic behavior in Rust.
homepagehttps://github.com/A248/polymorph-rs
repositoryhttps://github.com/A248/polymorph-rs
max_upload_size
id473635
size47,464
A248 (A248)

documentation

README

Polymorph

crates.io version apache2 license docs.rs docs unsafe forbidden

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 RefMutOrOwned version, for when you need &mut T.
  • into_owned is available where T: Clone.
  • The type implements From<&T> and From<T>, as well as Deref to T

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.

  • RefMutOrBox is a version of RefOrBox which uses &mut T and can be dereferenced to a mutable value.
  • The type implements From<&T> and From<T>, as well as Deref to T.
  • Optional support for the dyn-clone crate is provided by the trait-clone feature. If T: DynClone, an into_owned method 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:

[dependencies]
polymorph = "1.0"

trait-clone feature

To enable interoperability with the dyn-clone trait, turn on this feature.

[dependencies]
polymorph = { version = "1.0", features = ["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.

Commit count: 7

cargo fmt