-
Notifications
You must be signed in to change notification settings - Fork 46
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
Conversation
This is a useful addition in my opinion. I'd love to see it merged! |
There was a problem hiding this 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?
@paholg would it not need to be |
One could add less than / greater than 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. |
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 |
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.
3b0e3b1
to
466df16
Compare
Sorry I've been absent. If you'd still like this merged, any thought on implementing |
let three = i64::from(U3::new()); The let three: i64 = U3::to_int(); |
But 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. |
The
ToInt
type operator return arbitrary integer types, including {i,u}{8,16,32,64} types. Below is the example usage ofToInt
. 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.EDIT 1:
This PR resolves #133.
EDIT 2:
Fix typo