Crate tuple_split

Source
Expand description

This crate an extension for the tupleops crate.

tupleops contains many useful features for manipulating tuples andusing tuples in generic code. However, it does not support any kind of splitting of tuples. This crate adds that feature.

§Examples

#![feature(generic_const_exprs)]

let t = (32, 0.707, "test");

// Splitting tuples by index
let (l, r) = tuple_split::split_tuple_at::<0, _>(t);
assert_eq!(t, tupleops::concat_tuples(l, r));

let (l, r) = tuple_split::split_tuple_at::<1, _>(t);
assert_eq!(t, tupleops::concat_tuples(l, r));

let (l, r) = tuple_split::split_tuple_at::<2, _>(t);
assert_eq!(t, tupleops::concat_tuples(l, r));

let (l, r) = tuple_split::split_tuple_at::<3, _>(t);
assert_eq!(t, tupleops::concat_tuples(l, r));

// Splitting tuples given a left side
let (l, r) = tuple_split::split_tuple_into_left::<(u8, f32), _>(t);
assert_eq!(t, tupleops::concat_tuples(l, r));

// Splitting tuples given a right side
let (l, r) = tuple_split::split_tuple_into_right::<(&str,), _>(t);
assert_eq!(t, tupleops::concat_tuples(l, r));

// Splitting tuples given both sides
let (l, r) = tuple_split::split_tuple_into::<(u8, f32), (&str,)>(t);
assert_eq!(t, tupleops::concat_tuples(l, r));

§Split by index

Tuples can be split by a const-generic index. To use this feature, put #![feature(generic_const_exprs)] on the top of your lib.rs or main.rs.

§Example

#![feature(generic_const_exprs)]

let t = (1, 1.0, "test");

let (l, r) = tuple_split::split_tuple_at::<2, _>(t);

assert_eq!(t, tupleops::concat_tuples(l, r));

§Split by type

The type of tuple you want from the split operation can be used instead of an index. This does not require #![feature(generic_const_exprs)]. Either the left, right or both can be provided as a generic type.

§Examples

§Left

let t = (1, 1.0, "test");

let (l, r) = tuple_split::split_tuple_into_left::<(u8, f32), _>(t);

assert_eq!(t, tupleops::concat_tuples(l, r));
let t = (1, 1.0, "test");

let (l, r) = tuple_split::split_tuple_into_right::<(&str,), _>(t);

assert_eq!(t, tupleops::concat_tuples(l, r));

§Both

let t = (1, 1.0, "test");

let (l, r) = tuple_split::split_tuple_into::<(u8, f32), (&str,)>(t);

assert_eq!(t, tupleops::concat_tuples(l, r));

§Tuple sizes

By default, this crate operates with tuples of up to 16 elements, just like the tupleops crate. If you want to use differently sized tuples, use the features 8, 16, 32, 64, 96, 128, 160, 192, 224 or 256 to set the maximum supported tuple size.

The dont_hurt_yourself_by_using_all_features is there to prevent usage of tuples bigger than 8 if cargo is ran with the flag --all-features. Using a tuple size above 16 is highly discouraged as it will make compilation time unbearably long. Compilation time will increase exponentially. You have been warned.

Traits§

TupleSplitAt
Tuples which may be split at index MIDDLE have the trait TupleSplitAt, which, when split, returns TupleSplitAt::Left, TupleSplitAt::Right.
TupleSplitInto
A trait for splitting a tuple up into two parts given a specified left part L and right part R. L and R must be the left and right part of Self.
TupleSplitIntoLeft
A trait for splitting a tuple up into two parts given a specified left part L. L must be a leftmost segment of Self.
TupleSplitIntoRight
A trait for splitting a tuple up into two parts given a specified right part R. R must be a rightmost segment of Self.

Functions§

split_tuple_at
Splits tuple at a given index.
split_tuple_into
A trait for splitting a tuple up into two parts given a specified left part L and right part R. L and R must be the left and right part of Self.
split_tuple_into_left
Splits a tuple up into two parts given a specified left part L. L must be a leftmost segment of Self.
split_tuple_into_right
Splits a tuple up into two parts given a specified right part R. R must be a rightmost segment of Self.

Type Aliases§

Left
Type alias Left equals TupleSplit::Left for any tuple which implements TupleSplit at the given MIDDLE.
Right
Type alias Right equals TupleSplit::Right for any tuple which implements TupleSplit at the given MIDDLE.