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

Add is_signed #322

Open
geeknoid opened this issue May 8, 2024 · 1 comment
Open

Add is_signed #322

geeknoid opened this issue May 8, 2024 · 1 comment

Comments

@geeknoid
Copy link

geeknoid commented May 8, 2024

When working in a generic context, it would be useful to access whether a PrimInt-based generic type is signed or not signed at build time.

fn foo(v: X)
where X: PrimInt {
    if (K::is_signed()) {
       let num = v.to_i64();
   else {
       let num = v.to_u64();
   }
}
@cuviper
Copy link
Member

cuviper commented May 8, 2024

It's possible to add, and you can emulate this now with T::min_value() < T::zero(), which is probably how we would write a default implementation in the trait.

Since the to_* methods return Option, you could also write it based on the runtime values like this:

if let Some(i) = v.to_i64() {
    // signed operations using `i`
} else {
    let u = v.to_u64().expect("unsigned number");
}

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

2 participants