Skip to main content

Crate inc_dec

Crate inc_dec 

Source
Expand description

§IncDec

Crates.io License Downloads Docs Twitch Status

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 MethodMethod Used
try_ppchecked_add
try_mmchecked_sub
IntIncDecExt MethodMethod
oppoverflowing_add
ommoverflowing_sub
wppwrapping_add
wmmwrapping_sub

§Features

FeatureDescription
numEnable 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_ppnum
non_zero_signed_mmnum
non_zero_signed_ommnum
non_zero_signed_oppnum
non_zero_signed_try_mmnum
non_zero_signed_wmmnum
non_zero_try_ppnum
non_zero_unsigned_mmnum
non_zero_unsigned_ommnum
non_zero_unsigned_oppnum
non_zero_unsigned_try_mmnum
non_zero_unsigned_wmmnum
non_zero_wppnum
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§

IncDecExt
For implementing incrementation and decrementation on numeric types.
IntIncDecExt
For implementing incrementation and decrementation on integer types.