Skip to content

feat: Implement Step trait for BigInt and BigUint #297

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ arbitrary = ["dep:arbitrary"]
quickcheck = ["dep:quickcheck"]
rand = ["dep:rand"]
serde = ["dep:serde"]
nightly-step = []

[package.metadata.docs.rs]
features = ["std", "serde", "rand", "quickcheck", "arbitrary"]
Expand Down Expand Up @@ -73,3 +74,6 @@ default-features = false
optional = true
version = "1"
default-features = false

[dev-dependencies]
itertools = { version = "0.12.1", default-features = false }
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ num-bigint = { version = "0.4", features = ["rand"] }
Note that you must use the version of `rand` that `num-bigint` is compatible
with: `0.8`.

### Range iterators

On nightly you can enable `nightly-step` feature that allows you to iterate over ranges such as `Bigint::zero()..`; keep in mind that it relies on `step_trait` nightly features and is prone to break with a new Rust or `num-bigint` release.

## Releases

Release notes are available in [RELEASES.md](RELEASES.md).
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,19 @@
//! Note that you must use the version of `rand` that `num-bigint` is compatible
//! with: `0.8`.
//!
//! ### Range iterators
//!
//! On nightly you can enable `nightly-step` feature that allows you to iterate
//! over ranges such as `Bigint::zero()..`;
//! keep in mind that it relies on `step_trait` nightly features and is prone
//! to break with a new Rust or `num-bigint` release.
//!
//!
//! ## Compatibility
//!
//! The `num-bigint` crate is tested for rustc 1.60 and greater.

#![cfg_attr(feature = "nightly-step", feature(step_trait))]
#![doc(html_root_url = "https://docs.rs/num-bigint/0.4")]
#![warn(rust_2018_idioms)]
#![no_std]
Expand Down Expand Up @@ -126,6 +134,9 @@ mod biguint;
#[cfg(feature = "rand")]
mod bigrand;

#[cfg(feature = "nightly-step")]
mod step;

#[cfg(target_pointer_width = "32")]
type UsizePromotion = u32;
#[cfg(target_pointer_width = "64")]
Expand Down
62 changes: 62 additions & 0 deletions src/step.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use core::iter::Step;

use num_traits::CheckedSub;

use crate::{BigInt, BigUint};

impl Step for BigUint {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
end.checked_sub(start)?.try_into().ok()
}

fn forward_checked(start: Self, count: usize) -> Option<Self> {
Some(Self::forward(start, count))
}

fn backward_checked(start: Self, count: usize) -> Option<Self> {
start.checked_sub(&count.into())
}

fn forward(start: Self, count: usize) -> Self {
start + count
}
}

impl Step for BigInt {
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
(end - start).try_into().ok()
}

fn forward_checked(start: Self, count: usize) -> Option<Self> {
Some(Self::forward(start, count))
}

fn backward_checked(start: Self, count: usize) -> Option<Self> {
Some(Self::backward(start, count))
}

fn forward(start: Self, count: usize) -> Self {
start + count
}

fn backward(start: Self, count: usize) -> Self {
start - count
}
}

#[cfg(test)]
mod tests {
use itertools::assert_equal;
use num_traits::Zero;

use crate::{BigInt, BigUint};

#[test]
fn ranges() {
assert_equal(
BigUint::zero()..BigUint::from(6u8),
(0u8..6).map(BigUint::from),
);
assert_equal(BigInt::from(-6)..BigInt::from(6), (-6..6).map(BigInt::from));
}
}