### 4 releases (breaking)

0.4.0 | Oct 24, 2021 |
---|---|

0.3.0 | Oct 18, 2021 |

0.2.0 | Oct 15, 2021 |

0.1.0 | Oct 7, 2021 |

#**102** in Math

**18,989** downloads per month

Used in **59** crates
(via gstreamer)

**MIT/Apache**

110KB

2.5K
SLoC

# option-operations

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

s.`Option`

## Example

Dealing with two

s, can lead to verbose expressions:`Option`

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

Thanks to the trait

we can write:`OptionSaturatingAdd`

`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`>`

`PartialOrd`

`Option``<`T`>`Another purpose is to workaround the

implementation
for `PartiaOrd`

, which uses the declaration order of the variants
for `Option``<`T`>`

. `Option`

appearing before `None`

, it results in
the following behavior:`Some``(``_``)`

`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

reflects a value which
is not defined and thus can not be compared with `None`

.`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

- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.