https://github.com/lambda-fairy/wrapping_macros
:gift_heart: A macro for wrapping arithmetic
https://github.com/lambda-fairy/wrapping_macros
macros rust
Last synced: 8 months ago
JSON representation
:gift_heart: A macro for wrapping arithmetic
- Host: GitHub
- URL: https://github.com/lambda-fairy/wrapping_macros
- Owner: lambda-fairy
- Created: 2015-03-31T06:51:57.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2017-03-12T03:36:45.000Z (over 9 years ago)
- Last Synced: 2025-04-09T10:48:43.215Z (over 1 year ago)
- Topics: macros, rust
- Language: Rust
- Homepage:
- Size: 23.4 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# wrapping_macros [](http://travis-ci.org/lfairy/wrapping_macros) [](https://crates.io/crates/wrapping_macros)
A macro for wrapping arithmetic.
Any code within a `wrapping! { .. }` block will be transformed as follows:
* `a + b` becomes `a.wrapping_add(b)`. Similarly for `-`, `*`, `/`, `%`, `<<`, `>>`.
* `a += b` becomes `a = a.wrapping_add(b)`. Similarly for `-=`, `*=`, `/=`, `%=`, `<<=`, `>>=`.
* `-a` becomes `a.wrapping_neg()`.
See [this Internals thread][1] for the motivation behind this crate.
[1]: http://internals.rust-lang.org/t/ergonomics-of-wrapping-operations/1756?u=lfairy
**Note:** This crate uses internal compiler APIs, and so requires the Nightly version of Rust.
## Cargo
Add this to your `Cargo.toml`:
```toml
wrapping_macros = "*"
```
## Example
```rust
#![feature(plugin)]
#![plugin(wrapping_macros)]
fn main() {
let mut sum = 0u8;
for x in 0u8..50 {
wrapping! {
sum += x;
}
}
}
```
## Caveats
* This crate uses unstable APIs, and will only work on Rust Nightly.
* You cannot nest another macro invocation within a `wrapping!` block. For example, this will not work:
```rust
let x = 41i32;
wrapping! {
println!("The answer is {}", x + 1); //~ ERROR
}
```
Instead, move the macro call out of the block:
```rust
let x = 41i32;
println!("The answer is {}", wrapping! { x + 1 });
```