#tuple #options #combinator #traits #helper #dealing

tuple-combinator

Convenience methods for dealing with Option tuples

4 releases

0.2.1 Aug 28, 2019
0.2.0 Aug 21, 2019
0.1.1 Aug 17, 2019
0.1.0 Aug 16, 2019

#2844 in Rust patterns

Download history 15/week @ 2024-04-01

1,427 downloads per month

MIT license

25KB
267 lines

tuple-combinator

Build Status Crates.io docs.rs

This crate provides a helper trait that implements several convenience method for tuples (up to 10-elements tuple). Implementing these methods directly for tuple Options can simplify your code a lot when working with many Options at once.

License

MIT


lib.rs:

A helper trait to improve the ergonomics when working with multiple Options. After importing TupleCombinator, you can treat a tuple of Options as one Option.

Example

use tuple_combinator::TupleCombinator;

fn main() {
    let tuples = (Some(1), Some(2), Some(3));

    assert_eq!(tuples.map(|(a,b,c)| a + b + c), Some(6));
    assert_eq!(tuples.and_then(|(a,b,c)| Some(a + b - c)), Some(0));
    assert_eq!(tuples.transpose(), Some((1,2,3)));
    assert_eq!((Some(1), None).map(|(a, b): (i32, i32)| 100), None);
}

No runtime deps