Skip to content

Make working with NonZero Nicer #348

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

Open
Keith-Cancel opened this issue Apr 7, 2025 · 0 comments
Open

Make working with NonZero Nicer #348

Keith-Cancel opened this issue Apr 7, 2025 · 0 comments

Comments

@Keith-Cancel
Copy link

It would be nice if there was some way to express things like this.

// There is `ZeroablePrimitive` but that is an unstable and internal trait.
struct Example<U: PrimInt> {
    nz: NonZero<U>,
    val: U
}

I don't see anything really in the num_traits for working with NonZero or knowing that the type can be made into one.

// I ended up doing something like this:
struct Example<U: PrimInt + NonZeroType> {
    nz: U::NonZero,
    val: U
}
// However this leaves a lot traits and other features of NonZero not available
// short of adding more to this trait
pub trait NonZeroType: PrimInt {
    type NonZero;
    fn new(self) -> Option<Self::NonZero>;
    unsafe fn new_unchecked(self) -> Self::NonZero;
    fn get(nz: Self::NonZero) -> Self;
}
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

No branches or pull requests

1 participant