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 try from methods #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add try from methods #86

wants to merge 1 commit into from

Conversation

benmkw
Copy link

@benmkw benmkw commented Oct 22, 2020

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.

@@ -16,7 +16,8 @@ edition = "2018"
[package.metadata.docs.rs]
features = ["std", "num-bigint-std", "serde"]

[dependencies]
[dependencies.paste]
version = "1.0"
Copy link
Contributor

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?

Copy link
Author

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(())
                    }

@vks
Copy link
Contributor

vks commented Feb 4, 2021

As far as I can see, this does not include any new tests for the added functionality.

@benmkw
Copy link
Author

benmkw commented Feb 6, 2021

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 try_into counterparts so I could add those as well if you think this would be a good direction.

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.

None yet

2 participants