A set of utilities to better enable polymorphic behavior in Rust

Overview

Polymorph

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

A set of utilities to better enable polymorphic behavior in Rust.

Introduction

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.

  • Want to decide whether to return an owned value or a borrowed reference? Now you can do so, without having to change your function's return type.
  • Want to use trait objects but don't like the fact that Box<dyn MyTrait> requires ownership? Now you can choose whether to return a borrowed or an owned trait object.

Features

Ref(Mut)OrOwned

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.
  • There is also a RefMutOrOwned version, for when you need &mut T.
  • into_owned is available where T: Clone.
  • The type also implements From<&T> and From<T>, so that you can use Into<RefOrOwned<T>> to create highly-flexible function parameter.

Ref(Mut)OrBox

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.
  • 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.

RefMutOrBox is a version of RefOrBox which uses &mut T and can be dereferenced to a mutable value.

Safety

  • The library contains no unsafe code
  • The library should never panic

Dependency

Add this library to your Cargo.toml:

[dependencies]
polymorph = "0.1"

Features

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.

Other Information

Composability

Polymorph is well-combined with the dyn-clone and enum-dispatch crates for flexible and effective dynamic programming.

Notes

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.

Licensing

Licensed under the Apache License v2.0. See the LICENSE.txt.

You might also like...
Fast and scalable phylogenomic utilities 🐱 .

ogcat Fast and scalable phylogenomic utilities 🐱 . Installation Prebuilt binaries See releases. The musl binary for Linux should be the most compatib

 Creating CLI's just got a whole lot better.
Creating CLI's just got a whole lot better.

Staq Creating CLI's just got a whole lot better. Don't worry about CLI colouring, networking, Size of Executables, Speed ever again Have any doubts? R

Better error messages for axum framework.

axum-debug This is a debugging crate that provides better error messages for axum framework. axum is a great framework for developing web applications

Make the github cli even better with fuzzy finding
Make the github cli even better with fuzzy finding

github-repo-clone (grc) Github Repo Clone is a command line utility written in rust that leverages the power of fuzzy finding with the github cli Usag

Better touch. Efficient file creation with templates.

touchx Better touch. Efficient file creation with templates. If you save files such as LICENSE or .editorconfig, you can create files with similar con

A lean, minimal, and stable set of types for color interoperation between crates in Rust.

This library provides a lean, minimal, and stable set of types for color interoperation between crates in Rust. Its goal is to serve the same function that mint provides for (linear algebra) math types.

A set of bison skeleton files that can be used to generate a Bison grammar that is written in Rust.

rust-bison-skeleton A set of bison skeleton files that can be used to generate a Bison grammar that is written in Rust. Technically it's more like a B

Rusty Armor Builds - Monster Hunter Rise Armor Set Creation Tool
Rusty Armor Builds - Monster Hunter Rise Armor Set Creation Tool

RAB Rusty Armor Builds - Monster Hunter Rise Armor Set Creation Tool Armor files used by RAB

Femtorinth is a library to interact with a sub-set of the Modrinth API.

Femtorinth Femtorinth is a rust library to interact with a sub-set of the Modrinth api, it only includes the api calls that don't need auth (a.k.a onl

Owner
Software artist
null
Utilities to gather data out of roms. Written in Rust. It (should) support all types.

snesutilities Utilities to gather data out of roms. Written in Rust. It (should) support all types. How Have a look at main.rs: use snesutilities::Sne

Layle | Luca 5 Oct 12, 2022
Common utilities code used across Fulcrum Genomics Rust projects

fgoxide Common utilities code used across Fulcrum Genomics Rust projects. Why? There are many helper functions that are used repeatedly across project

Fulcrum Genomics 2 Nov 2, 2022
ffizz is a library of utilities for exporting Rust libs for use in other languages

ffizz ffizz is a library of utilities for exporting Rust libs for use in other languages. FFI generally requires a lot of unsafe code, which in turn r

Dustin J. Mitchell 2 Aug 29, 2022
Build and deploy cross platform bioinformatic utilities with Rust.

The Bioinformatics Toolkit RUST-backed utilities for bioinformatic data processing. Get started The fastest way to get started it to download the appl

null 5 Sep 8, 2023
Utilities for integrating Datadog with opentelemetry + tracing in rust

Non-official datadog tracing and log correlation for Rust services. This crate contains the necessary glue to bridge the gap between OpenTelemetry, tr

willbank 5 Oct 31, 2023
Utilities and tools based around Amazon S3 to provide convenience APIs in a CLI

s3-utils Utilities and tools based around Amazon S3 to provide convenience APIs in a CLI. This tool contains a small set of command line utilities for

Isaac Whitfield 47 Dec 15, 2022
Utilities for interacting with the Behringer X-Touch Mini MIDI controller

xtouchmini Collection of utilities for interacting with the Behringer X-Touch Mini MIDI controller, including reading button/knob/fader inputs and sen

null 5 Nov 11, 2021
CLI & Utilities for fractional.art

fractional-rs CLI & Utilities for fractional.art CLI Usage The CLI uses Flashbots' relay to submit the transactions. No bribe is required as you pay v

Georgios Konstantopoulos 20 Dec 27, 2022
An asynchronous IO utilities crate powered by tokio.

An asynchronous IO utilities crate powered by tokio.

Harry 2 Aug 18, 2022
A box full of utilities, a unworthy replacement for coreutils / busybox / toybox.

Gearbox A box full of utilities, a unworthy replacement for coreutils / busybox / toybox. List of content How to setup Systems Ubuntu Arch How to buil

Gemlock 5 Jul 21, 2022