var_num

Crates.iovar_num
lib.rsvar_num
version0.4.2
sourcesrc
created_at2024-07-02 07:24:31.407186
updated_at2024-07-17 07:30:31.484883
descriptionVariable length number implementation that can be used as a drop in replacement for any number primitive.
homepage
repositoryhttps://gitlab.com/magicfoodhand/var_num
max_upload_size
id1289308
size50,420
(inapinch-io)

documentation

README

var_num

The idea from this crate was inspired by Rob Pike' - "What We Got Right, What We Got Wrong" talk where he mentions that the int type should've been variable sized, "what if they didn't overflow". This crate was build to be used by rigz_vm/fn_vm, to simplify dealing with numbers.

Variable length number implementation that can be used as a drop in replacement for any number primitive, values are stored in their smallest representation initially (when calling From). For mathematical operations the output value matches the overall type of the left hand side. Assume the following are converted to values first:

i32::MAX - i16::MAX = i16
f32::MAX + u8::MAX = f32 // overflow, TODO #1

Components

The following enums should not be created directly, instead use the From trait to convert to the desired type.

  • VarNum: The main type, used to store numbers and represent any number.
  • VarFloat: A floating point type that can be used to store any floating point number.
  • VarInt: A signed integer type that can be used to store any integer, supports overflow and underflow automatically.
  • VarUInt: An unsigned integer type that can be used to store any unsigned integer.

Usage

use var_num::VarNum;

fn main() {
    let a: u8 = 255;
    let b: f32 = 3.14;
    let a: VarNum = a.into();
    let b: VarNum = b.into();
    let num = a + b;
    // num is a VarNum::U16(258)
    let num = b + a;
    // num is a VarNum::F32(258.14)
}

Special Cases

Float and UInt are left unchanged, but they include all operations available on ints. For ints this crate used checked_* to handle overflow or underflow with a few short circuits to skip the operation.

The BitAnd, BitOr, BitXor, Shl, and Shr operators are unchanged and will not change the size of the value. With the first three operators the size of the value can't change, and there were no other special cases; for the last two I personally don't want my numbers changing size when I shift them (even ints, this may change in a future version).

TODO

  • Automatically scale up from f32 to f64 when needed, implement checked_* for Float.
  • allow scaling up UInt through scaled_* functions, not default behavior
  • Allow explicit scaling for Shl or Shr, scaled_shl & scaled_shr
  • Add support for nightly custom types through feature
  • Benchmarking (From, operations, to/from bytes, etc.)
Commit count: 8

cargo fmt