rust-num / num-traits

Numeric traits for generic mathematics in Rust
Apache License 2.0
731 stars 135 forks source link

Make Zero and One const traits #276

Closed sigurd4 closed 9 months ago

sigurd4 commented 1 year ago

Hi, i'm a a big fan of this library, and use it often. I'm currently working on some libraries using compile-time traits and functions, and figured it would be very convenient if the zero and one traits in your library are const traits.

Surely these traits should be const (at some point), however this requires enabling experimental language features, which i'm not sure is desireable at this point. I can understand if you guys want to wait with that until const traits are a stable rust language feature.

cuviper commented 1 year ago

Surely these traits should be const (at some point),

Optionally, yes, but some types can't be const because they involve allocation, like BigUint::one().

however this requires enabling experimental language features, which i'm not sure is desireable at this point. I can understand if you guys want to wait with that until const traits are a stable rust language feature.

Yeah, I don't want to deal with unstable features in general. Definitely not unconditional, but even putting it behind a "nightly" cargo feature would be work to keep up, and especially here given that const traits are in flux lately.

tarcieri commented 1 year ago

Could there be separate traits with associated constants for zero and one? (and potentially, a blanket impl of Zero or One for those hypothetical new traits)

That could be a purely additive change that would also have the benefit of working on stable Rust.

haennes commented 1 year ago

would be maintaining a "nightly" branch be an option #283

Optionally, yes, but some types can't be const because they involve allocation, like BigUint::one().

Note: currently the #[const_trait] just marks that a trait can be implemented const.

cuviper commented 1 year ago

would be maintaining a "nightly" branch be an option

I'm not interested in maintaining that... but as a branch, anyone could do this in their own fork.

haennes commented 1 year ago

would be maintaining a "nightly" branch be an option

I'm not interested in maintaining that... but as a branch, anyone could do this in their own fork.

Ok. Hopefully const generics become stable soon. If/When my need for this arises again I will create a fork. Once I do this I will mention this issue

tarcieri commented 1 year ago

I think it'd be possible to get similar benefits in a purely additive manner through a separate trait which defines them as consts, and could be linked to the existing traits via blanket impls:

pub trait ZeroConst: Sized {
    const ZERO: Self;
}

impl<T> Zero for T
where
    T: Add<Self, Output = Self> + Eq + ZeroConst
{
    fn zero() -> Self {
        Self::ZERO
    }

    fn is_zero(&self) -> bool {
        self.eq(&Self::ZERO)
    }
}
cuviper commented 1 year ago

I would be open to separate traits with const items.

brundonsmith commented 11 months ago

Just chiming in to say I'd love this feature too, and doing it as separate traits would be perfectly acceptable

tarcieri commented 11 months ago

I opened a PR which adds ZeroConstant and OneConstant ConstZero and ConstOne traits here: https://github.com/rust-num/num-traits/pull/303

tarcieri commented 9 months ago

With #303 merged I think this can be closed?