GithubHelp home page GithubHelp logo

fengalin / option-operations Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 150 KB

Traits and auto-implementations to improve arithmetic operations usability when dealing with `Option`s.

License: Apache License 2.0

Rust 100.00%
rust option arithmetic operations traits ord cmp

option-operations's Introduction

option-operations

crates.io Documentation Build Status MSRV

option-operations provides traits and auto-implementations to improve arithmetic operations usability when dealing with Options.

Example

Dealing with two Options, can lead to verbose expressions:

let lhs = Some(1u64);
let rhs = Some(u64::MAX);

assert_eq!(
    lhs.zip(rhs).map(|(lhs, rhs)| lhs.saturating_add(rhs)),
    Some(u64::MAX),
);

Thanks to the trait OptionSaturatingAdd we can write:

assert_eq!(
    lhs.opt_saturating_add(rhs),
    Some(u64::MAX),
);

The trait can also be used with the inner type:

assert_eq!(
    lhs.opt_saturating_add(u64::MAX),
    Some(u64::MAX),
);

assert_eq!(
    1.opt_saturating_add(rhs),
    Some(u64::MAX),
);

Alternative to PartialOrd for Option<T>

Another purpose is to workaround the PartiaOrd implementation for Option<T>, which uses the declaration order of the variants for Option. None appearing before Some(_), it results in the following behavior:

let some_0 = Some(0);
let none: Option<u64> = None;

assert_eq!(none.partial_cmp(&some_0), Some(Ordering::Less));
assert_eq!(some_0.partial_cmp(&none), Some(Ordering::Greater));

In some cases, we might consider that None reflects a value which is not defined and thus can not be compared with Some(_).

assert_eq!(none.opt_cmp(&some_0), None);
assert_eq!(some_0.opt_cmp(&none), None);

Of course, this is consistent with other usual comparisons:

assert_eq!(none.opt_lt(&some_0), None);
assert_eq!(none.opt_min(&some_0), None);

LICENSE

This crate is licensed under either of

at your option.

option-operations's People

Contributors

a-kenji avatar fengalin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

a-kenji

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.