Crates.io | clamps |
lib.rs | clamps |
version | 0.1.2 |
source | src |
created_at | 2022-05-04 13:01:34.318336 |
updated_at | 2022-05-06 08:41:29.867523 |
description | Wrapping, saturating, and other forms of number clamping |
homepage | |
repository | https://github.com/nebulaeandstars/clamps |
max_upload_size | |
id | 580392 |
size | 49,337 |
Wrapping, saturating, and other forms of number clamping!
Doing this sucks:
if foo >= min && foo <= max {
// do stuff
}
Instead, it'd be great if we could encode max
and min
into the type of
foo
somehow, so that we can get all the benefits of bounds checking without
having to hard-code the busy-work ourselves. Ada has a nice solution with its
range and
mod types, and it
would be great if Rust had them, too.
Clamps provides a variety of types with three basic forms:
Bounded
Wrapping
AddAssign
, SubAssign
, etc.Saturating
AddAssign
, SubAssign
, etc.All three variants have generic and concrete forms. The generic types are slightly more expensive and cumbersome, but can be used with more types. The concrete forms are cheap and easier to use, but are currently limited to integers.
The generic variants also allow you to assign their bounds at runtime, which can help when dynamically checking indexes, etc.