#zip #options #macro #single #option-t #instance #arguments

macro zips

Macros for wrapping up a bunch of Option<T>'s/Result<T, E>'s into a single Option<(T [, T...])>

8 releases

0.1.7 Sep 4, 2024
0.1.6 Sep 1, 2024
0.1.5 Aug 29, 2024
0.1.2 Jul 19, 2024

#962 in Procedural macros

Download history 96/week @ 2024-09-18 292/week @ 2024-09-25 386/week @ 2024-10-02 250/week @ 2024-10-09 257/week @ 2024-10-16 200/week @ 2024-10-23 208/week @ 2024-10-30 167/week @ 2024-11-06 182/week @ 2024-11-13 256/week @ 2024-11-20 417/week @ 2024-11-27 276/week @ 2024-12-04 438/week @ 2024-12-11 345/week @ 2024-12-18 91/week @ 2024-12-25 152/week @ 2025-01-01

1,075 downloads per month
Used in 2 crates

MIT license

6KB
58 lines

zips GitHub Actions Workflow Status Crates.io Total Downloads

Just because I was annoyed that I couldn't use Option<T>::zip with more than 2 arguments...

Zips provides 2 proc-macros that accept any number of either Option<T>s or Result<T, E>s as arguments and produces an Option<(T [, T...])>.

Installation

$> cargo add zips

Usage

With Option<T>:

use zips::zip;

fn main() -> () {
  let i: Option<i32> = Some(0);
  let j: Option<usize> = Some(1usize);
  let k: Option<()> = None;
 
  //  zipped_some: Option<(i32, usize)>
  let zipped_some = zip!(i, j);
  assert_eq!(zipped_some, Some((0, 1usize)));
 
  //  zipped_none: Option<((i32, usize), ())>
  let zipped_none = zip!(zipped_some, k);
  assert_eq!(zipped_none, None);
}

With Result<T, E>:

use zips::zip_result;

fn main() -> () {
  let m: Result<i32, ()> = Ok(1);
  let n: Result<usize, ()> = Ok(0usize);
  let o: Result<(), &'static str> = Err("I'm an error!");
 
  //  zipped_ok: Option<(i32, usize)>
  let zipped_ok = zip_result!(m, n);
  assert_eq!(zipped_some, Some((1i32, 0usize)));
 
  //  zipped_err: Option<((i32, usize), ())>
  let zipped_err = zip_result!(zipped_ok, o);
  assert_eq!(zipped_err, None);
}

Dependencies

~230–680KB
~16K SLoC