Skip to content

Make working with NonZero Nicer #348

Description

@Keith-Cancel

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;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions