-
Notifications
You must be signed in to change notification settings - Fork 51
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 try from methods #86
base: master
Are you sure you want to change the base?
Conversation
@@ -16,7 +16,8 @@ edition = "2018" | |||
[package.metadata.docs.rs] | |||
features = ["std", "num-bigint-std", "serde"] | |||
|
|||
[dependencies] | |||
[dependencies.paste] | |||
version = "1.0" |
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.
Why is this dependency required?
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.
its for generating the call to the corresponding underlying impl ::[<from_ $name>]
fn try_from(n: $name) -> Result<Self, ()> {
<$into as FromPrimitive>::[<from_ $name>](n)
.map(Ratio::from_integer)
.ok_or(())
}
As far as I can see, this does not include any new tests for the added functionality. |
Yes, I was unsure how much this should be tested, maybe a few lines which check the Ok and Error case would be fine because it does not really add anything, just exposes existing functionality differently. I was using this as a fork and also needed the |
as described in #85
I went ahead and implemented From and TryFrom for all applicable types, not just floats.
This is a more detailed approach than the current FromPrimitive Trait which always returns an Option but some conversions can never fail so we can unwrap them.