Giter Club home page Giter Club logo

checked-math's Introduction

Checked Math

A convenience macro for changing the checking properties of math expressions without having to change those expressions (mostly).

Some Background

By default Rust's math expressions (e.g., 2 * 9) panic on overflow in debug builds and overflow silently in release builds. While this is a fine default, you may want different behaivor. For instance, you may want to panic on overflow even in release builds. In that case you'll want to reach for the various APIs available for Rust numbers (e.g., checked_mul). The issue with this however, is that you can no longer use normal math notation, instead needing to use cumbersome methods like a.checked_add(b).unwrap() instead of a + b.

Checked Math lets you keep using normal math notation by performing the rewrite in a proc macro.

Example

By default the following may silently overflow in release builds at runtime:

(x * y) + z

In order to to panic on error instead, people often rewrite to:

x.checked_mul(y).unwrap().checked_add(z).unwrap()

Or you could use Checked Math and write the following:

use checked_math::checked_math as cm;
cm!((x * y) + z).unwrap()

The macro call converts the normal math expression into an expression returning None if any of the checked math steps return None, and Some(_) on success.

Projects may want to wrap the rewriting macro to automatically include their error reporting, for example:

#[macro_export]
macro_rules! my_checked_math {
    ($x: expr) => {
        checked_math::checked_math!($x).unwrap_or_else(|| panic!("math error"))
    };
}

Limitations

Checked Math is currently limited in the following:

  • Because math operations can more easily propogate type inference information than method calls, you may have to add type information when using the macros that were not neceesary before.
  • The syntax the macro accepts is intentionally limited to binary expressions, parentheses, argument-less calls and some extras. The background is that it would be confusing if inner expressions like in checked_math!(foo(a + b)) or checked_math!(if a + b { b + c } else { d }) were silently not transformed.
  • Conversion of pow is extremely naive so if you call a pow method on some type, this will be converted to checked_pow even if that makes no sense for that type.

History

This is a modified version of overflow from https://github.com/rylev/overflow/ originally by Ryan Levick. See LICENSE.

checked-math's People

Contributors

rylev avatar ckamm avatar

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.