Crate orn

Source
Expand description

orn 0.8.2

A generic implementation of a sum type (or discriminated union). It provides enum Or<T1, T2, ..., N> types as a counterpart to tuples.


§Features

  • has #![no_std] and #![forbid(unsafe_code)]
  • supports the applicable core traits
  • features = ["iter"] (default): supports the Into/Iterator traits
  • features = ["future"] (default): supports the Into/Future traits
  • features = ["serde"]: supports the Serialize and Deserialize traits
  • features = ["rayon"]: supports the ParallelIterator family of traits
  • features = ["or16"]: for up to Or16
  • features = ["or32"]: for up to Or32

§Cheat Sheet

use orn::*;

/// Using the `tn` methods, items of an `Or` value can be retrieved.
pub fn retrieve_dynamically(input: Or3<u8, usize, [char; 1]>) {
    let _a: Option<u8> = input.t0();
    let _b: Option<usize> = input.t1();
    let _c: Option<[char; 1]> = input.t2();
}

/// Using the `At<I>` trait, items of an `Or` value can be retrieved
/// generically.
pub fn retrieve_statically(input: Or4<char, bool, isize, u32>) {
    let _a: Option<char> = At::<0>::at(input);
    let _b: Option<bool> = At::<1>::at(input);
    let _c: Option<isize> = At::<2>::at(input);
    let _d: Option<u32> = At::<3>::at(input);
}

/// Often, the type of iterator is conditional to some input value. Typically,
/// to unify the return type, one would need to implement a custom iterator, but
/// here `orn` types are used instead.
#[cfg(feature = "iter")]
pub fn unify_divergent_iterators(array_or_range: bool) -> impl Iterator<Item = u8> {
    if array_or_range {
        Or2::T0([1u8, 2u8])
    } else {
        Or2::T1(0u8..10u8)
    }
    .into_iter()
    // The item of the `Or` iterator is `Or<u8, u8>`. `Or::into` collapses an `Or` value into a
    // specified type.
    .map(Or2::into)
}

#[cfg(feature = "rayon")]
use rayon::prelude::*;
/// With `features = ["rayon"]`, `rayon`'s `ParallelIterator` family of traits
/// can be used similarly to the `Iterator` trait.
#[cfg(feature = "rayon")]
pub fn unify_divergent_parallel_iterators(
    array_or_range: bool,
) -> impl ParallelIterator<Item = u8> {
    if array_or_range {
        Or2::T0([1u8, 2u8])
    } else {
        Or2::T1(0u8..10u8)
    }
    .into_par_iter()
    // The item of the `Or` iterator is `Or<u8, u8>`. `Or::into` collapses an `Or` value into a
    // specified type.
    .map(Or2::into)
}

fn main() {}

Modules§

or0
or1
or2
or3
or4
or5
or6
or7
or8

Traits§

At
A trait for accessing a type at a specific index.
Count
A trait for getting the number of type arguments in a type.
Is
A trait for checking if an Or value is of a certain variant by index.

Type Aliases§

Or0
A type alias for a union of 0 types. This type is uninhabited.
Or1
An enum of 1 variants.
Or2
An enum of 2 variants.
Or3
An enum of 3 variants.
Or4
An enum of 4 variants.
Or5
An enum of 5 variants.
Or6
An enum of 6 variants.
Or7
An enum of 7 variants.
Or8
An enum of 8 variants.