Crate tuplemagic

Crate tuplemagic 

Source
Expand description

§tuplemagic

TupleMagic is a Rust library that provides utilities for manipulating tuples through various operations like mapping, filtering, nesting, and reducing. This library supports both value-level and type-level operations on tuples.

§Details

TupleMagic transforms tuples into a nested form which allows for varargs-like processing of tuple types. An input tuple of type (A, B, C) becomes the nested form (A, (B, (C, EOT))).

Turning tuples into nested types simplifies filtering and mapping operations because it leverages recursive type structures, which are inherently more adaptable to recursive traits.

Note that some features of this library are only available in macro form as the non-macro form of the operation may be unergonomic (ie: filter and map operations).

§Features

  • Nesting and Unnesting: Transform tuples into nested structures and vice versa.
let a = (1, 2, 3).nest();
  • Mapping: Apply transformations to each element of a tuple based on a type mapper.
type T = (Option<u8>, Option<u16>, Option<()>);
struct RemoveOption {}
impl<T> TypeMap<RemoveOption> for Option<T> {
    type Mapped = T;
}
type U = tuple_mapper!(RemoveOption::map(T));
  • Filtering: Include or exclude elements from a tuple based on a predicate.
type T = (u8, u16, Option<()>);
tuple_filter_predicate!(P = { include = (u8, Vec<u8>), exclude = (u16, u32, ~ <T> Option<T>)});
type U = tuple_filter!(P::filter_type(T));
  • Reducing: Reduce a tuple to a single value using a reducer.
struct TupleReducerSum;
impl<T> TupleReducer<usize, T> for TupleReducerSum where T: TryInto<usize>,
{
    fn reduce_one(collect: usize, from: T) -> usize {
        collect + from.try_into().unwrap_or_default()
    }
}
let out = TupleReducerSum::reduce((1_u8, 2_u16, 3_u32), 0);

§Operations

Features of this crate may be available as macros, traits or both based on ergonomic concerns. The goal of the crate will be to eventually move to a pure trait-based system, but this does not appear possible at this time.

The underlying mechanisms of the macros are subject to change and are not considered stable at this time.

OperationValue LevelType LevelAPIs
NestingYesYesTupleNest, TupleUnnest nest!
UnnestingYesYessee above
MappingYestuple_mapper! TypeMap
FilteringYesYestuple_filter! tuple_filter_predicate!
ReducingYesTupleReducer, TupleReducerCapable

§Installation

Add this to your Cargo.toml:

[dependencies]
tuplemagic = "0.x.y"

Macros§

nest
Convert a tuple type or value from the form (A, B, C, ...) to the form (A, (B, (C, ..., EOT))).
tuple_filter
Perform a filtering operation on a tuple using the given predicate type (a TypeMap that returns TupleFilterInclude or TupleFilterExclude).
tuple_filter_predicate
Define a tuple filter predicate (a TypeMap that returns TupleFilterInclude or TupleFilterExclude) that includes and excludes the given types. All types that may be potentially encountered in the provided tuples must be specified.
tuple_mapper
Perform a mapping operation on a tuple using the given predicate type (a TypeMap that returns a mapped type).

Structs§

EOT
A marker struct (end of tuple) for the end of a nested tuple list. As a marker, EOT is both a value and a type.
TupleFilterExclude
Exclude this tuple item from the final result.
TupleFilterInclude
Include this tuple item in the final result.

Traits§

TupleNest
Perform a nesting operation on a tuple. Given a tuple of type (A,B,C), returns (A, (B, (C, ())).
TupleReducer
Perform a reduce operation given the current accumulator and the input type F.
TupleReducerCapable
A trait which gives the ability for a reducer to reduce a given tuple. This trait is available for all reducers, for all tuples containing types supported by that reducer.
TupleUnnest
Perform an unnesting operation on a tuple. Given a tuple of type (A, (B, (C, ())), returns (A,B,C).
TypeMap
Represents a type mapping operation. The type parameter P represents the operation.