tuplities
Tuple utilities in Rust, fractioned across several crates to improve compile times. There is a main crate tuplities that re-exports traits from subcrates in its prelude module, so that the subcrates may be compiled in parallel cutting down on overall build times.
This library is #[no_std] compatible, making it suitable for embedded systems and other environments without the standard library.
[]
= "0.1.4"
The library provides several traits for working with tuples:
TupleClone: Provides atuple_clone()method to clone tuples. All elements must implementClone.TupleCopy: Provides atuple_copy()method to copy tuples. All elements must implementCopy.TupleDebug: Provides atuple_debug()method that returns a debug string representation of the tuple. All elements must implementDebug.TupleDefault: Provides atuple_default()method to create default instances of tuples. All elements must implementDefault.TupleReplicate<T>: Provides atuple_replicate(value)method to create tuples by replicating a single value across all positions. The value must implementClonefor tuples with 2+ elements, but not for empty tuples or single-element tuples.TupleHash: Provides atuple_hash<H: Hasher>()method to hash tuples with any hasher. All elements must implementHash.TuplePartialEq: Provides atuple_eq()method to compare tuples for partial equality. All elements must implementPartialEq.TupleEq: Provides atuple_eq()method to compare tuples for total equality. All elements must implementEq.TuplePartialOrd: Provides atuple_partial_cmp()method to compare tuples for partial ordering. All elements must implementPartialOrd.TupleOrd: Provides atuple_cmp()method to compare tuples for total ordering. All elements must implementOrd.TupleOption: Provides atranspose()method to transpose a tuple of options into an option of a tuple. All elements must beOption<T>.IntoTupleOption: Provides aninto_options()method to convert a tuple into a tuple of options.NestedTupleOption: Provides atranspose()method to transpose nested tuples of options into options of nested tuples.IntoNestedTupleOption: Provides aninto_options()method to convert nested tuples into nested tuples of options.NestedTupleOptionWith<H>: Providesfirst_none_with,first_some_with, andtranspose_orhelpers which operate with a parallel homogeneous nested tuple ofHused for result/error mapping.NestedTupleTryFrom<T, E>: Provides anested_tuple_try_from()method to fallibly convert nested tuple types element-wise usingTryFromconversions.NestedTupleTryInto<T, E>: Provides anested_tuple_try_into()convenience method to perform fallible conversions usingNestedTupleTryFrom.NestedTupleFrom<T>: Provides an infalliblenested_tuple_from()method to convert nested tuples usingFromelement-wise.NestedTupleInto<T>: Provides an infalliblenested_tuple_into()convenience method to perform conversions usingNestedTupleFrom.NestedTupleOptionFrom<T>: Provides anested_tuple_option_from()method to convert nested tuples ofOptions usingFromelement-wise.NestedTupleOptionInto<T>: Provides anested_tuple_option_into()convenience method to perform conversions usingNestedTupleOptionFrom.NestedTupleOptionTryFrom<T, E>: Provides anested_tuple_option_try_from()method to fallibly convert nested tuples ofOptions usingTryFromconversions.NestedTupleOptionTryInto<T, E>: Provides anested_tuple_option_try_into()convenience method to perform fallible conversions usingNestedTupleOptionTryFrom.NestedTupleReplicate<T>: Provides anested_tuple_replicate(value)method to create nested tuples by replicating a single value across all positions.TupleRef: Provides atuple_ref()method to get references to each element in the tuple.TupleMut: Provides atuple_mut()method to get mutable references to each element in the tuple.TupleRefMap: Provides atuple_ref_map()method to applyTupleRefto each element of a tuple of tuples, returning a tuple of tuples of references.TupleMutMap: Provides atuple_mut_map()method to applyTupleMutto each element of a tuple of tuples, returning a tuple of tuples of mutable references.TuplePopFront: Provides apop_front()method to remove and return the first element of the tuple along with the remaining elements as a new tuple.TuplePopBack: Provides apop_back()method to remove and return the last element of the tuple along with the remaining elements as a new tuple.TuplePushFront<T>: Provides apush_front(element)method to add an element to the front of the tuple, returning a new tuple.TuplePushBack<T>: Provides apush_back(element)method to add an element to the back of the tuple, returning a new tuple.TupleRemove<Idx>: Provides aremove()method to remove and return the element at the specified indextypenum'sIdxof the tuple along with the remaining elements as a new tuple.TupleInsert<Idx, T>: Provides aninsert()method to insert an element at the specified indextypenum'sIdxinto the tuple, returning the tuple with the element inserted.TupleSplit<Idx>: Provides asplit()method to split a tuple at the specified compile-time indextypenum'sIdx, returning two tuples containing the elements before and at/after the index.TupleReverse: Provides areverse()method to reverse the order of elements in a tuple.TupleTryFrom<T>: Provides atuple_try_from()method to fallibly convert from other types into tuples.TupleTryInto<T>: Provides atuple_try_into()method to fallibly convert tuples into other types.TupleFrom<T>: Provides atuple_from()method to infallibly convert from other types into tuples.TupleInto<T>: Provides atuple_into()method to infallibly convert tuples into other types.FlattenNestedTuple: Provides aflatten()method to convert nested tuples like(A, (B, (C,)))into flat tuples like(A, B, C).NestTuple: Provides anest()method to convert flat tuples like(A, B, C)into nested tuples like(A, (B, (C,))).NestedTupleIndex<Idx>: Provides anested_index()method to access elements at flat indices in nested tuples usingtypenum'sIdx.NestedTupleIndexMut<Idx>: Provides anested_index_mut()method to access mutable elements at flat indices in nested tuples usingtypenum'sIdx.NestedTupleRef: Provides anested_tuple_ref()method to get nested references to each leaf value of a nested tuple.NestedTupleMut: Provides anested_tuple_mut()method to get nested mutable references to each leaf value of a nested tuple.NestedTuplePopFront: Provides anested_pop_front()method to pop the front element of nested tuples.NestedTuplePopBack: Provides anested_pop_back()method to pop the back element of nested tuples.NestedTuplePushFront<T>: Provides anested_push_front(element)method to push an element to the front of a nested tuple.NestedTuplePushBack<T>: Provides anested_push_back(element)method to push an element to the back of a nested tuple.NestedTupleRow<Idx>: Provides anested_tuple_row()method to access elements at the specified index across a nested tuple-of-tuples, returning a nested row tuple by value.NestedTupleRow<Idx>: Provides anested_tuple_row()method to access elements at the specified index across a nested tuple-of-tuples, returning a nested row tuple of references (analogous toTupleRow).NestedTupleRowMut<Idx>: Providesnested_tuple_row_mut()for mutable nested row access.NestedTupleStartsWith<Other>: A marker trait for compile-time verification that a nested tuple starts with the same types as another nested tuple. For example,(A, (B, (C,)))implementsNestedTupleStartsWith<(A,)>andNestedTupleStartsWith<(A, (B,))>.NestTupleMatrix: Provides anest_matrix()method to convert flat tuples of flat tuples like((A, B), (C, D))into nested tuples of nested tuples like((A, (B,)), ((C, (D,)),)).FlattenNestedTupleMatrix: Provides aflatten_matrix()method to convert nested tuples of nested tuples back into flat tuples of flat tuples.TupleLen: Provides the length of the tuple as a compile-timetypenum::Unsignedtype.UnitTuple: A marker trait implemented for empty tuples()withTupleLen<Len = U0>.SingletonTuple: A marker trait implemented for single-element tuples(T,)withTupleLen<Len = U1>.PairTuple: A marker trait implemented for two-element tuples(T1, T2)withTupleLen<Len = U2>.TupleIndex<Idx>: Provides anindex()method to access the element at the specified indextypenum'sIdxof the tuple.TupleIndexMut<Idx>: Provides anindex_mut()method to access a mutable reference to the element at the specified indextypenum'sIdxof the tuple.FirstTupleIndex: A convenience trait providing afirst_tuple_index()method to access the first element of a tuple.LastTupleIndex: A convenience trait providing alast_tuple_index()method to access the last element of a tuple.TupleRow<Idx>: Provides atuple_row()method to access elements at the specified index across all tuples in a tuple of tuples (row-wise indexing).TupleRowMut<Idx>: Provides atuple_row_mut()method to access mutable elements at the specified index across all tuples in a tuple of tuples (mutable row-wise indexing).FirstTupleRow: A convenience trait providing afirst_tuple_row()method to access the first element of each tuple in a tuple of tuples.LastTupleRow: A convenience trait providing alast_tuple_row()method to access the last element of each tuple in a tuple of tuples.
Features
The crate provides optional features to enable specific traits. All features are enabled by default for convenience, but can be selectively disabled to reduce compile time and binary size.
Trait Features
The following features enable individual trait crates:
clone: EnablesTupleClonetraitcopy: EnablesTupleCopytraitdebug: EnablesTupleDebugtraittuple-default: EnablesTupleDefaulttraiteq: EnablesTupleEqtraitflatten-nest: EnablesFlattenNestedTuple,NestTuple,NestedTupleIndex,NestedTupleIndexMut,NestedTupleTryFrom,NestedTupleTryInto,NestTupleMatrix,FlattenNestedTupleMatrix,NestedTupleOption,IntoNestedTupleOption, andNestedTupleOptionWithtraitsfrom: EnablesTupleFromandTupleIntotraitshash: EnablesTupleHashtraitmut: EnablesTupleMutandTupleMutMaptraitsoption: EnablesTupleOptionandIntoTupleOptiontraitsord: EnablesTupleOrdtraitpartial-eq: EnablesTuplePartialEqtraitpartial-ord: EnablesTuplePartialOrdtraitremove: EnablesTupleRemovetraitinsert: EnablesTupleInserttraitlen: EnablesTupleLen,UnitTuple,SingletonTuple, andPairTupletraitsindex: EnablesTupleIndex,TupleIndexMut,FirstTupleIndex, andLastTupleIndextraitspop-front: EnablesTuplePopFront,TupleRefFront, andTupleMutFronttraitspop-back: EnablesTuplePopBack,TupleRefBack, andTupleMutBacktraitspush-front: EnablesTuplePushFronttraitpush-back: EnablesTuplePushBacktraitref: EnablesTupleRefandTupleRefMaptraitsreplicate: EnablesTupleReplicatetraitreverse: EnablesTupleReversetraitrow: EnablesTupleRow,TupleRowMut,FirstTupleRow, andLastTupleRowtraitssplit: EnablesTupleSplittraittry-from: EnablesTupleTryFromandTupleTryIntotraits
Size Features
Additionally, the crate provides features to generate trait implementations for tuples up to different sizes: 8 (default), 16, 32, 48, 64, 96, or 128 elements. Use the size-XX features to enable larger tuple support.
[]
= { = "0.1.4", = false, = ["clone", "index", "size-32"] }
Performance
Compile times scale with tuple size due to code generation. Below are measured build times for different maximum tuple sizes captured via the project's measure_compile_times.sh script on a typical development machine. Values are approximate and represent wall-clock real times. If you only plan to use variadic nested tuples, you may prefer to use only the tuplities-flatten-nest crate, which has significantly lower compile times.
tuplities (root crate)
| Max Tuple Size | Compile Time |
|---|---|
| 8 (default) | ~3.37s |
| 16 | ~2.71s |
| 32 | ~3.27s |
| 48 | ~4.55s |
| 64 | ~6.82s |
| 96 | ~15.60s |
| 128 | ~32.54s |
tuplities-flatten-nest (flatten-nest crate only)
| Max Tuple Size | Compile Time |
|---|---|
| 8 (default) | ~3.34s |
| 16 | ~2.39s |
| 32 | ~2.43s |
| 48 | ~2.56s |
| 64 | ~2.77s |
| 96 | ~3.35s |
| 128 | ~4.52s |
Architecture
The project is split into multiple crates for improved compile times:
tuplities/: Main crate that re-exports traits from subcratestuplities-{trait_name}/: Individual crates providing specific traitstuplities-derive/: Procedural macro crate that generates trait implementations
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contribution
Contributions are welcome! Please open issues or pull requests on the GitHub repository.