Expand description
§IncDec
X | Twitch | Youtube | Mastodon | GitHub | GitHub Sponsors
Incrementation and decrementation in Rust.
IncDec provides macros and extension traits which make doing incrementation and decrementation of numeric values a bit easier.
§Examples
The pp and mm extension methods:
use inc_dec::IncDecExt;
let mut val: u32 = 0;
assert_eq!(1, val.pp());
assert_eq!(0, val.mm());
The try_pp and try_mm extension methods:
use inc_dec::IncDecExt;
let mut val: u32 = 0;
assert_eq!(Some(1), val.try_pp());
assert_eq!(Some(0), val.try_mm());
assert_eq!(None, val.try_mm());
The opp and omm extension methods:
use inc_dec::IntIncDecExt;
let mut val: u32 = 0;
assert_eq!((1, false), val.opp());
assert_eq!((0, false), val.omm());
assert_eq!((u32::MAX, true), val.omm());
The wpp and wmm extension methods:
use inc_dec::IntIncDecExt;
let mut val: u32 = 0;
assert_eq!(1, val.wpp());
assert_eq!(0, val.wmm());
assert_eq!(u32::MAX, val.wmm());
The below tables indicate which trait implementation methods use which core integer methods (where applicable):
| IncDecExt Method | Method Used |
|---|---|
| try_pp | checked_add |
| try_mm | checked_sub |
| IntIncDecExt Method | Method |
|---|---|
| opp | overflowing_add |
| omm | overflowing_sub |
| wpp | wrapping_add |
| wmm | wrapping_sub |
§Features
| Feature | Description |
|---|---|
| num | Enable core::num NonZero extension methods. |
§Todo
- Add more documentation.
- Add more code examples.
- Add more tests.
- Clean-up the code.
- Support saturating incrementation and decrementation.
§Maybe
- Add support for numeric types in other crates.
§Coding Style
This project uses a coding style that emphasises the use of white space over keeping the line and column counts as low as possible.
So this:
fn foo()
{
bar();
}
Not this:
fn foo()
{
bar();
}
§License
Licensed under either of:
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0 (see also: https://www.tldrlegal.com/license/apache-license-2-0-apache-2-0))
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT (see also: https://www.tldrlegal.com/license/mit-license))
at your discretion
§Contributing
Please clone the repository and create an issue explaining what feature or features you’d like to add or bug or bugs you’d like to fix and perhaps how you intend to implement these additions or fixes. Try to include details though it doesn’t need to be exhaustive and we’ll take it from there (dependant on availability).
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Macros§
- mm
- Decrements the provided integer by one and returns it.
- mm_
- Decrements the provided integer by one.
- mmf
- Decrements the provided floating point number by one and returns it.
- mmf_
- Decrements the provided floating point number by one.
- non_
zero_ pp num - non_
zero_ signed_ mm num - non_
zero_ signed_ omm num - non_
zero_ signed_ opp num - non_
zero_ signed_ try_ mm num - non_
zero_ signed_ wmm num - non_
zero_ try_ pp num - non_
zero_ unsigned_ mm num - non_
zero_ unsigned_ omm num - non_
zero_ unsigned_ opp num - non_
zero_ unsigned_ try_ mm num - non_
zero_ unsigned_ wmm num - non_
zero_ wpp num - omm
- Calls overflowing_sub on an integer with 1 as a parameter.
- opp
- Calls overflowing_add on an integer with 1 as a parameter.
- pp
- Increments the provided integer by one and returns it.
- pp_
- Increments the provided integer by one.
- ppf
- Increments the provided floating point number by one and returns it.
- ppf_
- Increments the provided floating point number by one.
- try_mm
- Calls checked_sub on an integer with 1 as a parameter.
- try_pp
- Calls checked_add on an integer with 1 as a parameter.
- wmm
- Calls wrapping_sub on an integer with 1 as a parameter.
- wpp
- Calls wrapping_add on an integer with 1 as a parameter.
Traits§
- IncDec
Ext - For implementing incrementation and decrementation on numeric types.
- IntInc
DecExt - For implementing incrementation and decrementation on integer types.