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

Introduce ToInt type operator to return integer values without explicit type annotations (resolve #133) #135

Merged
merged 2 commits into from
Mar 12, 2021

Conversation

jerry73204
Copy link
Contributor

@jerry73204 jerry73204 commented Dec 31, 2019

The ToInt type operator return arbitrary integer types, including {i,u}{8,16,32,64} types. Below is the example usage of ToInt. It is useful when passing concrete values to methods with distinct argument types from typed numbers without runtime casting, or when you build a type operator upon arbitrary integer types.

use typenum::{type_operators::ToInt, consts::*};

fn accept_i8(n: i8) { /* ... */ }
fn accept_usize(n: usize) { /* ... */ }

type Value = U7;

fn main() {
    accept_i8(Value::to_int());
    accept_usize(Value::to_int());
}

EDIT 1:
This PR resolves #133.

EDIT 2:
Fix typo

@jerry73204 jerry73204 changed the title Introduce ToInt type operator to return integer values without explicit type annotations Introduce ToInt type operator to return integer values without explicit type annotations (resolve #133) Dec 31, 2019
@hgomersall
Copy link

This is a useful addition in my opinion. I'd love to see it merged!

Copy link
Owner

@paholg paholg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry I have been unresponsive. I did not have the mental energy to look at this repo for a while there.


I'm all for this, but what do you think of implementing std::convert::From instead of introducing a new trait?

@hgomersall
Copy link

@paholg would it not need to be TryFrom? On reflection, I can't see how the current code handles overflow...

@paholg
Copy link
Owner

paholg commented Apr 2, 2020

@paholg would it not need to be TryFrom? On reflection, I can't see how the current code handles overflow...

One could add less than / greater than where bounds, causing compilation failure on overflow. This would be my preferred solution:

impl<U> core::convert::From<PInt<U>> for i32
where
    U: Unsigned + NonZero + IsLess<U4294967296>,
    Le<U, U4294967296>: Same<True>,
{
    fn from(_: PInt<U>) -> i32 {
        <PInt<U> as Integer>::I32
    }
}

Edit: Fixed example.

@hgomersall
Copy link

That's nice - is the trait bound only invoked when the trait method is used?

@paholg
Copy link
Owner

paholg commented Apr 2, 2020

That's nice - is the trait bound only invoked when the trait method is used?

Yeah. Think about it; the compiler can only evaluate the bounds for U when it has some concrete U, which is when you're calling the function / using the trait 🙂.

@paholg
Copy link
Owner

paholg commented Apr 2, 2020

Note my initial example didn't actually work, as that bound was satisfied by all integers. I think there's a cleaner way to do it, but the updated example works.

The ToInt type operator return arbitrary integer types,
including {i,u}{8,16,32,64} types.
Base automatically changed from master to main March 12, 2021 03:55
@paholg
Copy link
Owner

paholg commented Mar 12, 2021

Sorry I've been absent.

If you'd still like this merged, any thought on implementing std::convert::From instead?

@jerry73204
Copy link
Contributor Author

std::convert::From needs a instanced value to work. Though the type is already zero sized, the writing is a bit verbose.

let three = i64::from(U3::new());

The .to_int() gives the advantage to get the value in desired integer type without instantiating the value.

let three: i64 = U3::to_int();

@paholg
Copy link
Owner

paholg commented Mar 12, 2021

But From gives you Into for free:

let three: u64 = U3::new().into()

I guess that's still more verbose, and it may be weird for users to be forced to instantiate.

Okay, you've convinced me.

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.

[feature-request] Type inferred concrete type
3 participants