scalar_map

Crates.ioscalar_map
lib.rsscalar_map
version0.1.4
sourcesrc
created_at2023-11-28 08:16:36.967404
updated_at2023-12-02 06:23:35.884742
description`map` for scalar types
homepage
repositoryhttps://github.com/Banyc/scalar_map
max_upload_size
id1051699
size6,508
IchHabeKeineNamen (Banyc)

documentation

README

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));

What does it solve

  • 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))
    

Custom struct

Deriving

# 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));

Without deriving

#[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));
Commit count: 8

cargo fmt