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

NonZero*? #66

Open
Rudxain opened this issue Jun 22, 2024 · 0 comments
Open

NonZero*? #66

Rudxain opened this issue Jun 22, 2024 · 0 comments

Comments

@Rudxain
Copy link

Rudxain commented Jun 22, 2024

This isn't a follow-up to #63, but it's related. I understand that despite NonZero being stable, its trait-bound isn't, so nothing should be done WRT that.

However, if does get stabilized with that trait-bound, it would be convenient if there was a non-prim counterpart provided by this crate, such as:

use std::hint::unreachable_unchecked;
use num_integer::Integer; // 0.1

pub struct NonZeroInt<T: Integer>(T);
impl<T: Integer> NonZeroInt<T> {
    #[must_use]
    fn new(n: T) -> Option<Self> {
        if n.is_zero() {
            None
        } else {
            Some(Self(n))
        }
    }
    #[must_use]
    unsafe fn new_unchecked(n: T) -> Self {
        match Self::new(n) {
            Some(n) => n,
            _ => unsafe { unreachable_unchecked() },
        }
    }
    #[must_use]
    fn get(self) -> T {
        self.0
    }
}

// (optional)
use core::ops::Deref;
impl<T: Integer> Deref for NonZeroInt<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
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