Crates.io | scalar_map |
lib.rs | scalar_map |
version | 0.1.4 |
source | src |
created_at | 2023-11-28 08:16:36.967404 |
updated_at | 2023-12-02 06:23:35.884742 |
description | `map` for scalar types |
homepage | |
repository | https://github.com/Banyc/scalar_map |
max_upload_size | |
id | 1051699 |
size | 6,508 |
scalar_map
map
for scalar types.
let num: Option<i32> = Some(42);
assert_eq!(num.map(|x| 42 - x), Some(0));
let num: i32 = 42;
assert_eq!(num.map(|x| 42 - x), 0);
let num: Option<i32> = Some(42);
assert_eq!(num.and_then(Option::Some), Some(0));
let num: i32 = 42;
assert_eq!(num.and_then(Option::Some), Some(0));
You can still keep map
and and_then
intact even if Option
is refactored out.
You want
x.map(Mutex::new).map(Arc::new)
...instead of
Arc::new(Mutex::new(x))
# Run this to add the `derive` feature
cargo add scalar_map --features derive
#[derive(Debug, PartialEq, ScalarMap)]
struct MyNum(i32);
let num = MyNum(42);
assert_eq!(num.map(|x| 42 - x.0).map(MyNum), MyNum(0));
#[derive(Debug, PartialEq)]
struct MyNum(i32);
impl ScalarMapExt for MyNum {}
let num = MyNum(42);
assert_eq!(num.map(|x| 42 - x.0).map(MyNum), MyNum(0));