Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Zero and One const traits #276

Closed
wants to merge 1 commit into from
Closed

Conversation

sigurd4
Copy link

@sigurd4 sigurd4 commented Jul 4, 2023

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
Copy link
Member

cuviper commented Jul 5, 2023

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
Copy link
Contributor

tarcieri commented Jul 7, 2023

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
Copy link

haennes commented Aug 1, 2023

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
Copy link
Member

cuviper commented Sep 1, 2023

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
Copy link

haennes commented Oct 7, 2023

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
Copy link
Contributor

tarcieri commented Oct 7, 2023

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
Copy link
Member

cuviper commented Nov 13, 2023

I would be open to separate traits with const items.

@brundonsmith
Copy link

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

@tarcieri
Copy link
Contributor

tarcieri commented Dec 14, 2023

I opened a PR which adds ZeroConstant and OneConstant ConstZero and ConstOne traits here: #303

@tarcieri
Copy link
Contributor

tarcieri commented Feb 8, 2024

With #303 merged I think this can be closed?

@cuviper cuviper closed this Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants