Crate tuple

source ·
Expand description

Feature flags

  • impl_num add support for traits from the num crate. (default on)
  • impl_simd add support for simd types. (default off)
  • impl_serde impl Serialize and Deserialize from serde

Examples

extern crate tuple;
use tuple::*;

All following operations are defined on the T1 .. Tn type of this crate, as well for the normal tuple types.

Element-wise operations

let a = T2(3, 4) + T2(5, 4);
assert_eq!(a, T2(8, 8));

let b = T2(3u32, 4.0f32) * T2(7, 3.0);
assert_eq!(b, T2(21, 12.));

assert_eq!(T3(1, 2, 3).map(|x| x * 2), T3(2, 4, 6));

Indexing

This is implemented in the TupleElements trait.

Indexing works as expected and panics when out of bounds. There are also get and get_mut functions that return Option<&T> and Option<&mut T>.

assert_eq!(T3(1, 2, 3)[2], 3);

assert_eq!(T2(7, 8).get(1), Some(&8));
assert_eq!(T2(7, 8).get(2), None);

Iterate over the elements of a tuple

for i in T2(1, 2).elements() {
    println!("{}", i);
}

let mut b = T3(3, 4, 5);
for i in b.elements_mut() {
    *i += 1;
}
assert_eq!(b.elements().sum::<u32>(), 15);

Consume a tuple and iterate over the elements

for i in T2(String::from("hello"), String::from("world")).into_elements() {
    let s: String = i; // it's really a String
    println!("{}", s);
}

Conversions

// slice to tuple
assert_eq!(T3::from_slice(&[1u8, 2, 3, 4, 5][..]), Some(T3(1, 2, 3)));

// tuple to and from array
let t = T3(1, 2, 3);
let a: [u8; 3] = t.into();
let t: T3<_, _, _> = a.into();

assert_eq!(T2(Some(1), Some(2)).collect(), Some(T2(1, 2)));

Joining two tuples

let a = T2(1, 2);
let b = T3(3, 4, 5);
assert_eq!(a.join(b), T5(1, 2, 3, 4, 5));

Splitting a tuple in two parts

let a = T4(1, 2, 3, 4);
let (b, c): (T1<_>, _) = a.split(); // split needs a type hint for the left side
assert_eq!(b, T1(1));
assert_eq!(c, T3(2, 3, 4));

Rotate and Reverse

let a = T4((), 2, 3, true);
assert_eq!(a.rot_l(),   T4(2, 3, true, ())); // rotate left
assert_eq!(a.rot_r(),   T4(true, (), 2, 3)); // rotate right
assert_eq!(a.reverse(), T4(true, 3, 2, ())); // reverse

Adding a Trait

#[macro_use]
extern crate tuple;
extern crate num_traits;

use tuple::*;
use num_traits::Zero;
use std::ops::{Add, Sub, Mul};
use std::fmt::Debug;

trait Ring: Add + Sub + Mul + Zero + Debug + Sized {}

// The name is up to you
macro_rules! impl_ring {
    // This line is defined by this crate and can't be changed
    ($($Tuple:ident $Arr:ident { $($T:ident . $t:ident . $idx:tt),* } )*) => ($(

        // This is expanded for every Tuple type
        impl<$($T),*> Ring for $Tuple<$($T),*> where Self: Zero, $( $T: Ring ),* {}

    // this has to match again
    )*)
}

// actually implement it!
impl_tuple!(impl_ring);

Macros

Structs

Enums

Traits

Functions

Type Aliases