pub enum Value {
UnsignedInt(u64),
UnsignedBigInt(u128),
SignedInt(i64),
SignedBigInt(i128),
Float(f64),
}Expand description
A numeric value.
Every calculation is parsed calculated as a common value type. This type can be concretely represented by one of a number of memory formats.
§Value Orders
The order of a value is jargon for its in-memory representation. The orders currently available are:
u64u128i64i128f64
Note that in general, lower orders have a narrower scope and higher orders have a broader scope. This enables us to promote values to higher compatible orders as necessary.
§Promotion
It will sometimes be necessary to promote a value. The next order after a promotion depends on the value in question. It follows these rules:
-
u64values are unconditionally promoted tou128as that conversion is infallible -
u128values are promoted to the next order in sequence which can represent the type, according to whether or not it fits inside the type bounds.I.e. the value
u64::MAXwould be promoted toi128, skippingi64, as it could not be losslessly converted. The valuei128::MAX + 1would be promoted tof64, even though this will lose precision, becausef64can better approximate that overflow thani128could. -
i64values are unconditionally promoted toi128as that conversion is infallible. -
i128values are unconditionally promoted tof64as that type can better approximate very large values. -
f64values remainf64.
§Parsing Rules
When parsing a value, each order is checked in sequence. The first value type which parses without error is used.
§Math Rules
When computing an expression, for each pair of values, this algorithm is applied:
- the lower-order of the pair is promoted
- if the two orders are still not equal, the previous step is repeated
- once the two orders are equal, math is performed as normal.
§Equality and Comparison
Equality and comparison operations are defined on the logical values. This is to say that when testing equality or comparing values, they are promoted until they match, and then the appropriate calculation is performed.
For strict equality comparisons, use the strict_eq method. For strict ordering,
use the strict_cmp method.
Variants§
Implementations§
Source§impl Value
impl Value
Source§impl Value
impl Value
Sourcepub fn strict_eq(self, other: Self) -> bool
pub fn strict_eq(self, other: Self) -> bool
Perform a strict equality comparison: this is equal if the values have equal value and order without promotion.
Sourcepub fn strict_cmp(self, other: Self) -> Ordering
pub fn strict_cmp(self, other: Self) -> Ordering
Compute a strict ordering: this orders first by the [Order][super::Order], then by value only if the orders match
Source§impl Value
impl Value
Source§impl Value
impl Value
Sourcepub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseValueError>
pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseValueError>
Parses an integer from a string slice with digits in a given base.
The string is expected to be an optional + or - sign followed by only digits.
Leading and trailing non-digit characters (including whitespace) represent an error.
Underscores (which are accepted in Rust literals) also represent an error.
This function panics if radix is not in 2..=36.
Sourcepub fn parse_binary(s: &str) -> Result<Self, ParseValueError>
pub fn parse_binary(s: &str) -> Result<Self, ParseValueError>
Parse a binary input without decimals.
Should succeed with or without a leading 0b.
Sourcepub fn parse_octal(s: &str) -> Result<Self, ParseValueError>
pub fn parse_octal(s: &str) -> Result<Self, ParseValueError>
Parse an octal input without decimals.
Should succeed with or without a leading 0o.
Sourcepub fn parse_decimal(s: &str) -> Result<Self, ParseValueError>
pub fn parse_decimal(s: &str) -> Result<Self, ParseValueError>
Parse a decimal input which may or may not contain a decimal point.
Should succeed with or without a leading 0d.
Sourcepub fn parse_hex(s: &str) -> Result<Self, ParseValueError>
pub fn parse_hex(s: &str) -> Result<Self, ParseValueError>
Parse an octal input without decimals.
Should succeed with or without a leading 0o.
Trait Implementations§
Source§impl<Rhs> AddAssign<Rhs> for Value
impl<Rhs> AddAssign<Rhs> for Value
Source§fn add_assign(&mut self, rhs: Rhs)
fn add_assign(&mut self, rhs: Rhs)
+= operation. Read moreSource§impl<Rhs> DivAssign<Rhs> for Value
impl<Rhs> DivAssign<Rhs> for Value
Source§fn div_assign(&mut self, rhs: Rhs)
fn div_assign(&mut self, rhs: Rhs)
/= operation. Read moreSource§impl IntoDiscriminant for Value
impl IntoDiscriminant for Value
Source§type Discriminant = Order
type Discriminant = Order
fn discriminant(&self) -> Self::Discriminant
Source§impl<Rhs> MulAssign<Rhs> for Value
impl<Rhs> MulAssign<Rhs> for Value
Source§fn mul_assign(&mut self, rhs: Rhs)
fn mul_assign(&mut self, rhs: Rhs)
*= operation. Read moreSource§impl Numeric for Value
impl Numeric for Value
Source§type BinIter = Box<dyn Iterator<Item = char>>
type BinIter = Box<dyn Iterator<Item = char>>
Source§type OctIter = Box<dyn Iterator<Item = char>>
type OctIter = Box<dyn Iterator<Item = char>>
Source§type DecLeftIter = Box<dyn Iterator<Item = char>>
type DecLeftIter = Box<dyn Iterator<Item = char>>
Source§type DecRightIter = Box<dyn Iterator<Item = char>>
type DecRightIter = Box<dyn Iterator<Item = char>>
Source§type HexIter = Box<dyn Iterator<Item = char>>
type HexIter = Box<dyn Iterator<Item = char>>
Source§fn binary(&self) -> Option<Self::BinIter>
fn binary(&self) -> Option<Self::BinIter>
Source§fn octal(&self) -> Option<Self::OctIter>
fn octal(&self) -> Option<Self::OctIter>
Source§fn decimal(&self) -> (Self::DecLeftIter, Option<Self::DecRightIter>)
fn decimal(&self) -> (Self::DecLeftIter, Option<Self::DecRightIter>)
Source§fn hex(&self) -> Option<Self::HexIter>
fn hex(&self) -> Option<Self::HexIter>
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
true when this value is less than 0.Source§impl Ord for Value
impl Ord for Value
Source§impl PartialOrd for Value
impl PartialOrd for Value
Source§impl<Rhs> RemAssign<Rhs> for Value
impl<Rhs> RemAssign<Rhs> for Value
Source§fn rem_assign(&mut self, rhs: Rhs)
fn rem_assign(&mut self, rhs: Rhs)
%= operation. Read moreSource§impl<Rhs> SubAssign<Rhs> for Value
impl<Rhs> SubAssign<Rhs> for Value
Source§fn sub_assign(&mut self, rhs: Rhs)
fn sub_assign(&mut self, rhs: Rhs)
-= operation. Read moreimpl Copy for Value
impl Eq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more