Skip to content

Support non-numeric types as traits associated constants #7553

Open
@TomAFrench

Description

@TomAFrench

From the documentation:

Since associated constants can also be used in a type position, its values are limited to only other expression kinds allowed in numeric generics.

I think this is quite limiting as it would be useful to use associated types in the bignum library to implement a trait defining bignum behaviour using a given config.

Currently there's a hack where we need feed in a generic type for which the only purpose is to fake an associated constant through calling another trait method on a different trait which will return the desired constants.

/// Repeated logic per field modulus

pub type BigNumWithParams = BigNum<ParamsGetter>

pub struct ParamsGetter {}

impl ParamsGetterTrait for ParamsGetter {
    fn get_params() -> ParamsStruct {
        PARAMS
    }
}

pub global PARAMS: ParamsStruct = ParamStruct {
    // param fields
};

/// shared logic

pub struct BigNum<Params> {
    pub limbs: [u128; 3],
}

impl<T> BigNumTrait for BigNum<T>
where
    T: ParamsGetterTrait,
{

    fn do_stuff_with_params() -> Self {
        let params = T::get_params();
        Self { limbs: do_stuff(params) }
    }
}

This is pretty ugly whereas we could be able to just implement BigNumTrait on a concrete struct version of BigNumWithParams where the params are encoded in the associated constant.

@jfecher is the quoted justification a showstopper as it seems like something we should be able to deal with tbh?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    📋 Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions