#sum-types #adt #either #no-std

no-std orn

A general implementation of the sum type. Meant to be a generic counterpart to tuples.

11 unstable releases (5 breaking)

0.6.1 Oct 30, 2024
0.6.0 Oct 30, 2024
0.5.2 Oct 19, 2024
0.4.3 Sep 23, 2024
0.1.0 Oct 8, 2023

#842 in Data structures

Download history 10/week @ 2024-07-30 1/week @ 2024-08-20 6/week @ 2024-08-27 1/week @ 2024-09-03 9/week @ 2024-09-10 241/week @ 2024-09-17 155/week @ 2024-09-24 361/week @ 2024-10-01 81/week @ 2024-10-08 624/week @ 2024-10-15 68/week @ 2024-10-22 318/week @ 2024-10-29 35/week @ 2024-11-05

1,050 downloads per month
Used in 2 crates

MIT license

18KB
380 lines

orn 0.6.1

A general implementation of the sum type. Meant to be a generic counterpart to tuples.


Cheat Sheet

use orn::*;

/// 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.
pub fn unify_divergent(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)
}

/// 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);
}

fn main() {}

Dependencies

~155KB