typed_tuple 0.1.0

Type based operations on primitive tuple elements.
Documentation
  • Coverage
  • 100%
    5 out of 5 items documented4 out of 5 items with examples
  • Size
  • Source code size: 8.4 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.31 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 17s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • cedtwo/typed_tuple
    1 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • cedtwo

typed_tuple

Typed Tuple

Type based operations on primitive tuple elements.

Functionality

typed_tuple allows for type safe operations on primitive tuple elements without specifying an index. The main purpose of this crate is to simplfy small arbitrary operations on heterogenous sequences. In the example below, elements of a tuple are assigned and retrieved irrespective of indices:

let mut tuple: (usize, Option<Type0>, Option<Type1>, Option<Type2>) = Default::default();

// Mutate the `usize` element.
tuple.map(|el: usize| el + 10);;
// Assign the `Type` prefixed elements.
*tuple.get_mut() = Some(Type0);
*tuple.get_mut() = Some(Type2);

// Pass elements to their respective consumers.
if let Some(element) = tuple.get() { std::hint::black_box::<Type0>(*element); }
if let Some(element) = tuple.get() { std::hint::black_box::<Type1>(*element); }
if let Some(element) = tuple.get() { std::hint::black_box::<Type2>(*element); }

assert_eq!(tuple, (10, Some(Type0), None, Some(Type2)));

Limitations

  • Fields of the same type must still specify a constant index. This can be specified with, for example, TypedTuple::<1, _>::get(&tuple) where 1 is the element index, however this offers no advantage over simply calling tuple.1.
  • typed_tuple can impact readability. Types should be explicit if not immediately obvious. Prefer let a: usize = tuple.get() over let a = tuple.get().
  • TypedTuple is implemented on tuples of up to 12 elements in length. This was chosen as it is the limit of many tuple trait implementations (PartialEq, Eq, etc.), however can be extended to support a higher number of elements if needed.

License: MIT OR Apache-2.0