Crate compost

source ·
Expand description

Overview

This library exposes decompose!, a macro to decompose tuples into tuples containing a subset of their values.

use compost::decompose;

let mut cx = (&1u32, &mut 2i32, 'c', "d", Box::new(5u8), 6i8);

fn function_taking_subset(cx: (&u32, &i32, &mut u8)) {
    dbg!(cx);
}

function_taking_subset(decompose!(cx));

decompose!(cx => cx_rest & {
    value_1: &str,
    value_2: &mut char,
});

dbg!((value_1, value_2));

decompose!(cx_rest => { value_3: &u32, value_4: &mut i8 });

dbg!((value_3, value_4));

function_taking_subset(decompose!(cx_rest));
function_taking_subset(decompose!(cx));

See decompose!’s documentation for more details on the precise semantics and limitations of the macro.

Features

Yes, this library…

  • Supports reborrowing (i.e. decompose! does not consume its input. Once you’re done with the borrow, you can reuse the original tuple).
  • Produces (admittedly pretty ugly) errors at compile time if the tuple cannot be decomposed.
  • Supports borrowing mutable, immutable, owned, and smart-pointer wrapped (so long as they implement Borrow) components.
  • Supports borrowing generic elements without failing spuriously on monomorphization.
  • Relies on type inference rather than TypeId, allowing the macro to operate on non-'static types.
  • Supports no_std environments.
  • Has zero runtime dependencies.

Macros

This macro takes a mutable reference to a tuple and decomposes it into a sub-tuple (i.e. a tuple containing a subset of the values contained in the parent tuple).