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
- Create tuples with any number of elements.
- Access elements in a tuple at any index, or by their types.
- Push element to a tuple or pop element from a tuple.
- Join two tuples or split a tuple into two parts.
- Rich tuple operations, e.g.: reverse, left rotate, zip.
- If all element types implement a
Trait
(e.g.Eq
,Add
), then theTuple
also implement thatTrait
. See which traits are supported and learn how to implement your custom traits forTuple
. - Traverse all elements of a tuple, or fold a tuple.
- When the number of elements of a tuple doesn’t exceed 32, then it can be converted from/to a primitive tuple or a primitive array.
§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
: DeriveSerialize
andDeserialize
for tuples.unwrap
(by default): Allows converting a tuple whose elements are all wrappers into a tuple of the values those wrappers contain. Seeunwrap()
.
§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(
seq_folder!(
|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 search tuple elements by type.
- Provides the ability to unwrap elements of tuples.
Macros§
- 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 functor that implements
Mapper
. - Provides a simple way to build a folder that implements
Folder
. - Split the tuple into two tuples at a specific index.
- 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.
Structs§
- The type used to represent tuples containing at least one element.
- The unit type that represents tuples of zero elements.
Traits§
- Convert tuples to primitive arrays, if all elements of the tuple are of the same type.
- Convert tuples to primitive tuples.
- A Marker trait to indicate that two tuple types have the same number of elements.
- The
TupleLike
trait defines the basic methods of tuples.