pub trait VariantDecimalType: Into<Variant<'static, 'static>> {
type Native;
const MAX_PRECISION: u8;
const MAX_UNSCALED_VALUE: Self::Native;
// Required methods
fn try_new(integer: Self::Native, scale: u8) -> Result<Self, ArrowError>;
fn try_new_with_signed_scale(
integer: Self::Native,
scale: i8,
) -> Result<Self, ArrowError>;
fn integer(&self) -> Self::Native;
fn scale(&self) -> u8;
// Provided method
fn is_valid_precision_and_scale(precision: &u8, scale: &i8) -> bool { ... }
}Expand description
Trait for variant decimal types, enabling generic code across Decimal4/8/16
This trait provides a common interface for the three variant decimal types,
allowing generic functions and data structures to work with any decimal width.
It is modeled after Arrow’s DecimalType trait but adapted for variant semantics.
§Example
fn extract_scale<D: VariantDecimalType>(decimal: D) -> u8 {
decimal.scale()
}
let dec4 = VariantDecimal4::try_new(12345, 2).unwrap();
let dec8 = VariantDecimal8::try_new(67890, 3).unwrap();
assert_eq!(extract_scale(dec4), 2);
assert_eq!(extract_scale(dec8), 3);Required Associated Constants§
Sourceconst MAX_PRECISION: u8
const MAX_PRECISION: u8
Maximum number of significant digits this decimal type can represent (9, 18, or 38)
Sourceconst MAX_UNSCALED_VALUE: Self::Native
const MAX_UNSCALED_VALUE: Self::Native
The largest positive unscaled value that fits in Self::MAX_PRECISION digits.
Required Associated Types§
Required Methods§
Sourcefn try_new(integer: Self::Native, scale: u8) -> Result<Self, ArrowError>
fn try_new(integer: Self::Native, scale: u8) -> Result<Self, ArrowError>
Creates a new decimal value from the given unscaled integer and scale, failing if the
integer’s width, or the requested scale, exceeds MAX_PRECISION.
NOTE: For compatibility with arrow decimal types, negative scale is allowed as long as the rescaled value fits in the available precision.
§Example
// Valid: 123.45 (5 digits, scale 2)
let d = VariantDecimal4::try_new(12345, 2).unwrap();
assert_eq!(d.integer(), 12345);
assert_eq!(d.scale(), 2);
VariantDecimal4::try_new(123, 10).expect_err("scale exceeds MAX_PRECISION");
VariantDecimal4::try_new(1234567890, 10).expect_err("value's width exceeds MAX_PRECISION");Sourcefn try_new_with_signed_scale(
integer: Self::Native,
scale: i8,
) -> Result<Self, ArrowError>
fn try_new_with_signed_scale( integer: Self::Native, scale: i8, ) -> Result<Self, ArrowError>
Attempts to convert an unscaled arrow decimal value to the indicated variant decimal type.
Unlike Self::try_new, this function accepts a signed scale, and attempts to rescale
negative-scale values to their equivalent (larger) scale-0 values. For example, a decimal
value of 123 with scale -2 becomes 12300 with scale 0.
Fails if rescaling fails, or for any of the reasons Self::try_new could fail.
Provided Methods§
Sourcefn is_valid_precision_and_scale(precision: &u8, scale: &i8) -> bool
fn is_valid_precision_and_scale(precision: &u8, scale: &i8) -> bool
True if the given precision and scale are valid for this variant decimal type.
NOTE: By a strict reading of the “decimal table” in the [variant spec], one might conclude that each decimal type has both lower and upper bounds on precision (i.e. Decimal16 with precision 5 is invalid because Decimal4 “covers” it). But the variant shredding integration tests specifically expect such cases to succeed, so we only enforce the upper bound here.
§Example
assert!(VariantDecimal4::is_valid_precision_and_scale(&5, &2));
assert!(!VariantDecimal4::is_valid_precision_and_scale(&10, &2)); // too wide
assert!(!VariantDecimal4::is_valid_precision_and_scale(&5, &-1)); // negative scale
assert!(!VariantDecimal4::is_valid_precision_and_scale(&5, &7)); // scale too bigDyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.