Element wise operations on tuples!
This crate allows to generalize operations to tuples using macros.
rust
extern crate tuple;
use tuple::*;
```rust 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.)); ```
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>
.
```rust assert_eq!(T3(1, 2, 3)[2], 3);
asserteq!(T2(7, 8).get(1), Some(8)); asserteq!(T2(7, 8).get(2), None); ```
```rust
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 macrorules! implring { // This line is defined by this crate and can't be changed ($($Tuple:ident { $($idx:tt -> $T:ident),* } )*) => ($(
// 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! impltuple!(implring); ```