Option and Either types with variants known at compile time.

Last update: May 5, 2022

Const Either

Some types to allow deciding at compile time if an option contains a value or which variant from the either type is active. This might be useful when you have some const generic type that should decide whether to use one datastructure or another, or no datastructure at all.

Syntax

let _definetly_none = ConstOption::<String, false>::new();
let definetly_some = ConstOption::<String, true>::new("hello, world".to_string());

// When there is definitely some value, the deref trait can be used.
println!("{}", &*definitely_some);

// Obtain the string inside.
let contained_string = definitely_some.into_inner();


struct Container<T, const IS_UNIQUE: bool> {
    data: ConstEither<Vec<T>, HashSet<T>, UNIQUE>,
}

impl<T> Container<T, false> {
    fn insert(&mut self, val: T) {
        /* ... */
    }
}

impl<T: Eq + Hash> Container<T, true> {
    fn insert(&mut self, val: T) -> Result<(), T> {
        /* ... */
    }
}

Drawbacks

Because of the current state of rust, the type ConstEither<L, R> will have the size and alignment of the largest from L and R.

GitHub

https://github.com/GabrielDertoni/const-either
You might also like...

Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when developing Radix blueprints.

Scrypto Static Types Use explicit container types with Scrypto! Leverage the Rust compiler's type checking to increase security and productivity when

Apr 23, 2022

A rust program to try and detect some types of Hardware Keyloggers.

A rust program to try and detect some types of Hardware Keyloggers.

Hardware Keylogger Detection Warning: Certain Types of Hardware keyloggers can not be detected by this program, Passive Hardware Keyloggers are imposs

Apr 12, 2022

Traits for inspecting memory usage of Rust types

memuse This crate contains traits for measuring the dynamic memory usage of Rust types. About Memory-tracking is a common activity in large applicatio

Nov 26, 2021

ūü™£ Types for a `Vec`'s raw parts

raw-parts A wrapper around the decomposed parts of a VecT. This struct contains the Vec's internal pointer, length, and allocated capacity. RawParts

Apr 18, 2022

Annoyed that Rust has many string types? Well it doesn't have to

generic-str The one true string type in Rust! This project intends to be a proof-of-concept for an idea I had a few months back. There is lots of unsa

Apr 9, 2022

An unsafe botched job that doesn't rely on types being 'static lifetime.

An unsafe botched job that doesn't rely on types being 'static lifetime. Will panic if provided a 0 field struct. I will fix this when I figure out how.

Feb 4, 2022

Rust types for the OASIS Common Alerting Protocol (CAP)

Rust types for the OASIS Common Alerting Protocol (CAP)

Feb 24, 2022

A repository full of manually generated hand curated JSON files, which contain the API Types that the Discord API returns.

Discord API Types A repository full of manually generated hand curated JSON files, which contain the API Types that the Discord API returns. Also did

Apr 28, 2022

Application that simulates a large grid of Pokémon types fighting each other.

Application that simulates a large grid of Pokémon types fighting each other.

poke-fighting-rust Rust project that simulates a grid of Pokémon fighting with each other. Each Pokémon type is a pixel on the grid and is represented

May 10, 2022
A list of known SS58 account types as an enum.

A list of known SS58 account types as an enum.

Apr 27, 2022
The cumulative sibling of `Result` and `Either`.

validated The cumulative sibling of Result and Either. The Validated type has special FromIterator instances that enable all errors in a sequence to b

May 16, 2022
`fugit` provides a comprehensive library of `Duration` and `Instant` for the handling of time in embedded systems, doing all it can at compile time.

fugit fugit provides a comprehensive library of Duration and Instant for the handling of time in embedded systems, doing all it can at compile time. T

Apr 27, 2022
Provides two APIs for easily cancelling futures, with the option to fallback to a timeout cancellation

tokio-context Provides two different methods for cancelling futures with a provided handle for cancelling all related futures, with a fallback timeout

Apr 17, 2022
Unofficial Bitwarden compatible server written in Rust, formerly known as bitwarden_rs

Alternative implementation of the Bitwarden server API written in Rust and compatible with upstream Bitwarden clients*, perfect for self-hosted deploy

May 22, 2022
constduck: compile-time duck typing and reflection

constduck provides a procmacro that can enable compile time duck typing and reflection on arbitrary struct types.

Nov 15, 2021
This crate defines a single macro that is a brainfunct compile-time interpreter.

Compile Protection This crate defines a single macro that is a brainfunct compile-time interpreter. One example is as follows #![recursion_limit = "18

Nov 29, 2021
Allocate memory at compile time!

const-alloc Link to the docs! Allocate memory at compile time! Currently, in stable rust there is no way to dynamically allocate or deallocate memory

Feb 5, 2022
A Rust proc-macro crate which derives functions to compile and parse back enums and structs to and from a bytecode representation

Bytecode A simple way to derive bytecode for you Enums and Structs. What is this This is a crate that provides a proc macro which will derive bytecode

Apr 8, 2022
little brother of gnu-copypasta-maker To compile, use make.

UWU Maker little brother of gnu-copypasta-maker To compile, use make. To install, use sudo make install or if you are root make install To uninstall,

Jan 12, 2022