#overflow #checked #macro

macro overf

Customize overflow behavior in mathematical operations

2 releases

0.1.1 Nov 16, 2024
0.1.0 Nov 12, 2024

#321 in Procedural macros

Download history 92/week @ 2024-11-08 135/week @ 2024-11-15 11/week @ 2024-11-22 1/week @ 2024-11-29 23/week @ 2024-12-06

175 downloads per month

MIT/Apache

21KB
389 lines

overf

github crates.io docs.rs

Define the overflow behavior of mathematical operations within blocks of code. This library provides the macros checked!, overflowing!, saturating!, propagating! and default!, enabling you to easily manage overflow in nested contexts.

Features

  • Customizable Overflow Behavior: Choose between checked, overflowing, and saturating operations for any block of code.
  • Nested Blocks: Define different overflow behaviors in nested blocks for more granular control.
  • Reset Behavior: Use the default! macro to reset the overflow behavior back to the default.

Installation

Add the following to your Cargo.toml:

[dependencies]
overf = "0.1"

Usage

use overf::{checked, overflowing, saturating, default};

fn main() {
    checked! {
        let result = 1usize + 1usize;
        let sum = default! { 2usize + 2usize }; // Use default behavior
        let difference = 5usize - 3usize;

        // Nested blocks
        saturating! {
            let total = 100u8 - 200u8;
        }
    }

    overflowing! {
        let result = 1usize + usize::MAX;
    }
}

Dependencies

~220–660KB
~16K SLoC