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

Implementation of std::iter::{Product, Sum} #22

Merged
merged 3 commits into from
Feb 27, 2018

Conversation

dodomorandi
Copy link
Contributor

This PR is relative to the issue 4. This is a
very simple implementation, based on two macros. Maybe a more elegant
solution is possible for these, feel free to suggest.

Two tests for both BigInt and BigUInt have been added, one relative
to the Sum, the other to the Product.

@cuviper
Copy link
Member

cuviper commented Feb 22, 2018

The CI failure on 1.8 is expected -- we'll have to hold this until I'm ready to bump to num-bigint 0.2.

I wonder, can this be implemented generically? Something like:

impl<T> Sum<T> for BigUint
where BigUint: Add<T, Output=Self> { ... }

Then it should work not just for iterators of BigUint by value and by reference, but also for any of the primitive integers we can add too -- without having to map each to temporary BigUint first!

If this works, I'd just write it directly, without the macro impls.

@cuviper
Copy link
Member

cuviper commented Feb 22, 2018

I think adding such a generic impl might itself be a breaking change, as someone could have their own impl Sum<Foo> for BigUint already -- but that's ok if we're rolling this into 0.2.

@dodomorandi
Copy link
Contributor Author

I really liked your suggestion, so I decided to make the two implementations generic. Two additional tests have been added to check against generic Sum and Product (in this case from a vector of i32 or u32, depending from the case).

I want to ask you a thing about conditional compilation: is there a way to check against std features using cfg (iter_arith_traits in this case)? I am quite new with Rust, and I never needed to perform this kind of check... until now. I am not able to find anything useful, but maybe it is just me. Do you have any idea?

@cuviper
Copy link
Member

cuviper commented Feb 22, 2018

is there a way to check against std features

Sadly, no. So many things would be easier if there were.

This is relative to the
[issue 4](rust-num#4). This is a
very simple implementation, based on two macros. Maybe a more elegant
solution is possible for these, feel free to suggest.

Two tests for both `BigInt` and `BigUInt` have been added, one relative
to the `Sum`, the other to the `Product`.
The succestion came from @cupiver, and it was simple and elegant. Now it
should be possible to use `std::iter::{Sum, Product}` with everything
that can be Add`-ed and `Mul`-tiplied _against_ `BigInt` and `BigUint`
@cuviper
Copy link
Member

cuviper commented Feb 27, 2018

I rebased now that 0.2 has started, and fixed on issue with rust 1.15.

Thanks for implementing this!

bors r+

bors bot added a commit that referenced this pull request Feb 27, 2018
22: Implementation of std::iter::{Product, Sum} r=cuviper a=dodomorandi

This PR is relative to the [issue 4](#4). This is a
very simple implementation, based on two macros. Maybe a more elegant
solution is possible for these, feel free to suggest.

Two tests for both `BigInt` and `BigUInt` have been added, one relative
to the `Sum`, the other to the `Product`.
@bors
Copy link
Contributor

bors bot commented Feb 27, 2018

Build succeeded

@bors bors bot merged commit 6091089 into rust-num:master Feb 27, 2018
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