Crates.io | zips |
lib.rs | zips |
version | 0.1.7 |
source | src |
created_at | 2024-07-18 22:08:08.262748 |
updated_at | 2024-09-04 20:15:21.096865 |
description | Macros for wrapping up a bunch of Option |
homepage | |
repository | https://github.com/cartercanedy/zips |
max_upload_size | |
id | 1308077 |
size | 14,288 |
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...])>
.
$> cargo add zips
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);
}
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);
}