Crates.io | polymorph |
lib.rs | polymorph |
version | 0.1.0 |
source | src |
created_at | 2021-10-28 17:13:59.898367 |
updated_at | 2021-10-28 17:13:59.898367 |
description | A set of utilities to better enable polymorphic behavior in Rust. |
homepage | https://github.com/A248/polymorph-rs |
repository | https://github.com/A248/polymorph-rs |
max_upload_size | |
id | 473635 |
size | 46,869 |
A set of utilities to better enable polymorphic behavior in Rust.
Rust is a wonderful language, with a strong emphasis on fast, static dispatch and statically-determined memory management. This is amazing, but it is hard to ease into from the perspective of a dynamic language.
This crate seeks to enable more dynamic programming while adhering to the core principles of Rust: only pay for what you need, and incur little overhead when you do so.
Box<dyn MyTrait>
requires ownership? Now you can choose whether to return a borrowed or an owned trait object.RefOrOwned<T>
is an enum over &T
and T
. This is similar to Cow
in that it abstracts over ownership and borrowing. However, while Cow
has a ToOwned
requirement, RefOrOwned
does not.
RefOrOwned<T>
implements Deref
to T
, as well as a broader family of standard Rust traits, so that you can work with it ergonomically and painlessly.RefMutOrOwned
version, for when you need &mut T
.into_owned
is available where T: Clone
.From<&T>
and From<T>
, so that you can use Into<RefOrOwned<T>>
to create highly-flexible function parameter.RefOrBox<T>
is an enum over &T
and Box<T>
. While similar to RefOrOwned
, RefOrBox
is intended for cases where T is unsized, most notably when T is a trait object.
RefOrBox<T>
implements standard traits where possible, including Deref
to T
.T: DynClone
, an into_owned
method will be made available. More on this later.RefMutOrBox
is a version of RefOrBox
which uses &mut T
and can be dereferenced to a mutable value.
Add this library to your Cargo.toml:
[dependencies]
polymorph = "0.1"
All of the available Cargo features provided by this crate. Each of these features must be enabled independently if it is desired.
Trait-Clone
To enable interoperability with the dyn-clone trait, turn on this feature.
[dependencies]
polymorph = { version = "0.1", 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.
Polymorph is well-combined with the dyn-clone
and enum-dispatch
crates for flexible and effective dynamic programming.
Polymorph's structs and enums are paid close attention to ensure they implement standard traits. Code interoperability and reuse is best achieved when standard traits are correctly implemented.
If you ever happen to notice a place where a standard trait could be implemented, please open an issue in this repository.
Licensed under the Apache License v2.0. See the LICENSE.txt.