rotary-add-0.2.0 has been yanked.
Cyclical Arithmetic with Unsigned Integers and Arbitrary Ranges
This library crate provides an extension trait, CycleAdd, for performing cyclical (modular) addition and subtraction with unsigned integer types (u8, u16, and u32). Unlike the default + and - operators, these operations never overflow.
Features
- Cyclical addition and subtraction: Perform modular arithmetic without overflow.
- 1-based series support: Simplifies operations on series starting from 1 (e.g., months, weekdays).
- Lightweight and focused: Extends core unsigned integer types for cryptography, metrology, and other use cases.
Examples
Cyclical Addition
let first_number: u8 = 22;
let second_number: u8 = 6;
let result = first_number.cycle_add;
// Result: 4 (22 + 6 wraps around at 24)
Cyclical Subtraction
let first_number: u8 = 37;
let second_number: u8 = 78;
let result = first_number.cycle_sub;
// Result: 59 (37 - 78 wraps around at 100)
1-Based Series Operations
Many common series (e.g., months, weekdays) start from 1. Modular arithmetic typically assumes 0-based indexing, which can lead to unexpected results. The series_add, series_sub, and series_mod methods handle these conversions for you.
let sample_month_1: u8 = 11;
let limit = 12; // Months of the year
let result = sample_month_1.series_add;
// Result: 12 (November + 1 = December)
let sample_month_2: u8 = 3;
let result = sample_month_2.series_sub;
// Result: 12 (March - 3 = December)
let sample_month_value: u8 = 24; // 24th month
let result = sample_month_value.series_mod;
// Result: 12 (24 wraps around to December)
Comparison with Related Crates
Unlike the Ring360 crate, RotaryAdd focuses exclusively on extending core unsigned integer types. It is designed for use cases such as:
- Cryptography
- Metrology
- Building blocks for other crates (e.g., encoding/decoding operations)
Changelog
Version 0.2.0
- Removed
T.rotary_add(&T)andT.rotary_sub(&T)as they were functionally equivalent toT.wrapping_add(T)andT.wrapping_sub(T)in the Rust core library (but with the first parameter passed by reference). - All remaining extension methods now take the first parameter by value, consistent with
wrapping_addandwrapping_sub.
Version 0.1.2
- Added
series_add,series_sub, andseries_modmethods for 1-based series operations.
For more details, visit the documentation or the GitHub repository.