Crates.io | polyvalue |
lib.rs | polyvalue |
version | 0.3.4 |
source | src |
created_at | 2024-01-16 20:05:38.955307 |
updated_at | 2024-04-03 19:28:01.247356 |
description | A weak-typed analog for rust |
homepage | |
repository | https://github.com/rscarson/polyvalue |
max_upload_size | |
id | 1101938 |
size | 367,207 |
A dynamic type system for rust
Built for lavendeux-parser
Values Can be bool, int, float, fixed, currency, string, array, object, or range.
All types implement Hash, Ord, Eq, Serialize, Deserialize, Clone, Debug and Default
Values of different types will be coerced to the same type when performing operations on them.
This is done using the following order-of-precedence:
T
to an array [T]
, and then converting the array to an object of the form {0: T}
.At this point, remaining types are numeric and will be coerced to the type containing the most information. The order-of-precedence is:
Operations can be performed directly on Value, or on inner types. Operations on Value will coerce the inner types to the same type, and then perform the operation.
There are 5 types of operations: arithmetic, bitwise, boolean, comparison, and indexing.
1>>-1 == 1<<1
), and values >64 wrap using r%64use polyvalue::{Value, Int, Float, Fixed, Currency, Str, Array, Object, Range};
fn main() {
let v = Value::from(1);
assert_eq!(v, Value::Int(Int::new(1)));
let v = Value::from(1.0).arithmetic_op(Value::from(2.0), ArithmeticOperation::Add).unwrap();
assert_eq!(v, Value::Float(Float::new(3.0)));
}
This crate was built for use in a parser, where the type of a value is not known until runtime. Please open an issue if you have any suggestions for improvement.