Skip to content

[Discussion] Refactor the Decimals by using constant generic. #2001

@HaoYang670

Description

@HaoYang670

Is your feature request related to a problem or challenge? Please describe what you are trying to do.
This is actually a question or a discussion.
Our current may to implement Decimal128 and Decimal256 is:

trait BasicDecimal: ... {
    const BIT_LENGTH: i32;
}

struct Decimal128 {}

struct Decimal256 {}

impl BasicDecimal for Decimal128 {}
impl BasicDecimal for Decimla256{}
impl Decimal128 {}

Actually I find an alternative way to implement this:

struct Decimal<const BYTE_LENGTH: usize> {
    precision: usize,
    scale: usize,
    value: [u8; BYTE_LENGTH],
}
impl<BYTE_LENGTH> Decimal<BYTE_LENGTH> {}

type Decimal128 = Decimal<16>;
type Decimal256 = Decimal<32>;

impl Decimal128 {}

I am not sure which one is better, but these are some pros and cons I thought:
pros: don't need the basic trait, reducing using macro when coding (better debugging)
cons: cannot restrict the bit_length to be 128 or 256 because const_generic_exprs is an unstable feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions