Crate tuplez

source ·
Expand description

Tuples represented in recursive form rather than parallel form.

§Motivation

The primitive tuple types are represented in parallel form, like:

(a, b, c, d ...)

Since Rust doesn’t support variadic generics, we cannot add methods to primitive tuples with any number of elements.

Currently, most tuple crates use declarative macros to implement methods for tuples whose number of elements is less than a certain limit (usually 32).

To solve this, tuplez introduces a Tuple type represented in recursive form, like:

Tuple(a, Tuple(b, Tuple(c, Tuple(d, ...))))

The advantage of this representation is that you can implement methods recursively for all tuples, and there is no longer a limit on the number of tuple’s elements. And, in almost all cases, the Tuple takes no more memory than the primitive tuples.

§Functionality

§Optional features

  • any_array: Use Rust’s unstable feature to implement conversion from/to primitive arrays on tuples with any number of elements. This feature requires compiling with rustc released to nightly channel.
  • serde: Derive Serialize and Deserialize for tuples.
  • unwrap (by default): Allows converting a tuple whose elements are all wrappers into a tuple of the values those wrappers contain. See unwrap().

§Examples

// Enable Rust's `generic_const_exprs` feature if you enable tuplez's `any_array` feature.
#![cfg_attr(feature = "any_array", feature(generic_const_exprs))]

use tuplez::*;

let tup = tuple!(1, "hello".to_string(), 3.14);
let tup2 = Tuple::from((2, " world", -3.14));
let tup3 = tup + tup2;
assert_eq!(tup3, tuple!(3, "hello world".to_string(), 0.0));

let tup4 = tup3.push(Some([1, 2, 3]));
let (tup5, popped) = tup4.pop_front();
assert_eq!(
    tup5,
    tuple!("hello world".to_string(), 0.0, Some([1, 2, 3]))
);
assert_eq!(popped, 3);

let tup6 = tup5.rev();
assert_eq!(
    tup6,
    tuple!(Some([1, 2, 3]), 0.0, "hello world".to_string())
);
let tup7 = tup6.rot_l();
assert_eq!(
    tup7,
    tuple!(0.0, "hello world".to_string(), Some([1, 2, 3]))
);

let tup8 = tup7.foreach(mapper! {
    |x: f64| -> String { x.to_string() }
    |x: Option<[i32; 3]>| -> String { format!("{:?}", x.unwrap()) }
    |x: String| { x }
});
let arr = tup8.to_array();
assert_eq!(
    arr,
    [
        "0".to_string(),
        "hello world".to_string(),
        "[1, 2, 3]".to_string()
    ]
);

let tup9 = tuple!(1, "2", 3.0);
let result = tup9.fold(
    tuple!(
        |acc, x| (acc + x) as f64,
        |acc: f64, x: &str| acc.to_string() + x,
        |acc: String, x| acc.parse::<i32>().unwrap() + x as i32,
    ),
    0,
);
assert_eq!(result, 15);

Please check Tuple’s documentation page for detailed usage.

Modules§

  • Provides the ability to fold tuples.
  • Provides the ability to traverse tuples.
  • Provides some operations between two tuples.
  • Provides the ability to test tuples.
  • Provides the ability to search tuple elements by type.
  • Provide operations on tuples consisting of MaybeUninit elements.
  • unwrapunwrap
    Provides the ability to unwrap elements of tuples.

Macros§

  • Pass the elements of a tuple as arguments to a function or method.
  • Provides a simple way to build a folder that implements Folder.
  • Get the element at a specific index of the tuple.
  • Provides a simple way to build a mapper that implements Mapper.
  • Pick some elements from a tuple.
  • Split the tuple into two tuples at a specific index.
  • Swap two parts of a tuple.
  • Take the element at a specific index of the tuple and get the remainder.
  • Generate a tuple from a list of expressions.
  • Generate patterns for tuples.
  • Generate the complete type signature for tuples.
  • Provides a simple way to build a unary predicate that implements UnaryPredicate.

Structs§

  • The type used to represent tuples containing at least one element.
  • The unit type that represents tuples of zero elements.

Traits§

Derive Macros§