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.
- Push element to a tuple or pop element from a tuple.
- Join two tuples or split a tuple into two parts.
- Reverse, left rotate, or right rotate a tuple.
- 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.
- 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.unwrap
(by default): Allows converting a tuple whose elements are all wrappers into a tuple of the values those wrappers contain. SeeUnwrap
andUnwrapOrDefault
.
§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_once(mapper_once! {
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()
]
);
Please check Tuple
’s documentation page for detailed usage.
Macros§
- Get the element at a specific index of the tuple.
- Provides a simple way to create a functor that implements
Mapper
. - Provides a simple way to create a functor that implements
MapperMut
. - Provides a simple way to create a functor that implements
MapperOnce
. - Split the tuple into two tuples at a specific index.
- 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§
- Traverse the tuple by immutable references to its elements.
- Traverse the tuple by mutable references to its elements.
- Traverse the tuple by taking its elements.
- Define functors for traversing the tuple by immutable references to tuple’s elements.
- Define functors for traversing the tuple by mutable references to tuple’s elements.
- Define functors for traversing the tuple by taking its elements.
- The
Popable
trait allows popping elements from the front and back of the tuple. - The
Rotatable
trait allows you to rotate the elements of the tuple. - The
ToArray
trait allows you to convert tuples to primitive arrays, if and only if all elements of the tuple are of the same type. - The
ToPrimitive
trait allows you to convert tuplez’s tuples to primitive tuples. - The
TupleLike
trait defines the basic methods of tuples. - Indicate that a type is a wrapper of a value and can be unwrapped into it.
- Indicate that a type is a wrapper of a value and can be unwrapped into it or the default value.