Skip to content

Commit 52175ce

Browse files
committed
Add feature cfgs for docs.rs
1 parent 2435646 commit 52175ce

File tree

10 files changed

+49
-16
lines changed

10 files changed

+49
-16
lines changed

.github/workflows/ci.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,22 @@ jobs:
6363
components: rustfmt
6464
- run: cargo fmt --all --check
6565

66+
doc:
67+
name: Docs.rs
68+
runs-on: ubuntu-latest
69+
steps:
70+
- uses: actions/checkout@v4
71+
- uses: dtolnay/rust-toolchain@nightly
72+
- run: cargo doc --features std,serde,rand,quickcheck,arbitrary
73+
env:
74+
RUSTDOCFLAGS: --cfg docsrs
75+
6676
# One job that "summarizes" the success state of this pipeline. This can then be added to branch
6777
# protection, rather than having to add each job separately.
6878
success:
6979
name: Success
7080
runs-on: ubuntu-latest
71-
needs: [test, i686, no_std, fmt]
81+
needs: [test, i686, no_std, fmt, doc]
7282
# Github branch protection is exceedingly silly and treats "jobs skipped because a dependency
7383
# failed" as success. So we have to do some contortions to ensure the job fails if any of its
7484
# dependencies fails.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ serde = ["dep:serde"]
2424

2525
[package.metadata.docs.rs]
2626
features = ["std", "serde", "rand", "quickcheck", "arbitrary"]
27+
rustdoc-args = ["--cfg", "docsrs"]
2728

2829
[[bench]]
2930
name = "bigint"

src/bigint.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ mod division;
2424
mod multiplication;
2525
mod subtraction;
2626

27+
mod arbitrary;
2728
mod bits;
2829
mod convert;
2930
mod power;
30-
mod shift;
31-
32-
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
33-
mod arbitrary;
34-
35-
#[cfg(feature = "serde")]
3631
mod serde;
32+
mod shift;
3733

3834
/// A `Sign` is a [`BigInt`]'s composing element.
3935
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, Hash)]

src/bigint/arbitrary.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
#![cfg(any(feature = "quickcheck", feature = "arbitrary"))]
2+
13
use super::{BigInt, Sign};
24
use crate::BigUint;
35

46
#[cfg(feature = "quickcheck")]
57
use alloc::boxed::Box;
68

79
#[cfg(feature = "quickcheck")]
10+
#[cfg_attr(docsrs, doc(cfg(feature = "quickcheck")))]
811
impl quickcheck::Arbitrary for BigInt {
912
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
1013
let positive = bool::arbitrary(g);
@@ -20,6 +23,7 @@ impl quickcheck::Arbitrary for BigInt {
2023
}
2124

2225
#[cfg(feature = "arbitrary")]
26+
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
2327
impl arbitrary::Arbitrary<'_> for BigInt {
2428
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
2529
let positive = bool::arbitrary(u)?;

src/bigint/serde.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![cfg(feature = "serde")]
2+
#![cfg_attr(docsrs, doc(cfg(feature = "serde")))]
3+
14
use super::{BigInt, Sign};
25

36
use serde::de::{Error, Unexpected};

src/bigrand.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Randomization of big integers
2+
#![cfg(feature = "rand")]
3+
#![cfg_attr(docsrs, doc(cfg(feature = "rand")))]
24

35
use rand::distributions::uniform::{SampleBorrow, SampleUniform, UniformSampler};
46
use rand::prelude::*;

src/biguint.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@ mod division;
1818
mod multiplication;
1919
mod subtraction;
2020

21+
mod arbitrary;
2122
mod bits;
2223
mod convert;
2324
mod iter;
2425
mod monty;
2526
mod power;
26-
mod shift;
27-
28-
#[cfg(any(feature = "quickcheck", feature = "arbitrary"))]
29-
mod arbitrary;
30-
31-
#[cfg(feature = "serde")]
3227
mod serde;
28+
mod shift;
3329

3430
pub(crate) use self::convert::to_str_radix_reversed;
3531
pub use self::iter::{U32Digits, U64Digits};

src/biguint/arbitrary.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(any(feature = "quickcheck", feature = "arbitrary"))]
2+
13
use super::{biguint_from_vec, BigUint};
24

35
use crate::big_digit::BigDigit;
@@ -6,6 +8,7 @@ use alloc::boxed::Box;
68
use alloc::vec::Vec;
79

810
#[cfg(feature = "quickcheck")]
11+
#[cfg_attr(docsrs, doc(cfg(feature = "quickcheck")))]
912
impl quickcheck::Arbitrary for BigUint {
1013
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
1114
// Use arbitrary from Vec
@@ -19,6 +22,7 @@ impl quickcheck::Arbitrary for BigUint {
1922
}
2023

2124
#[cfg(feature = "arbitrary")]
25+
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
2226
impl arbitrary::Arbitrary<'_> for BigUint {
2327
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
2428
Ok(biguint_from_vec(Vec::<BigDigit>::arbitrary(u)?))

src/biguint/serde.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#![cfg(feature = "serde")]
2+
#![cfg_attr(docsrs, doc(cfg(feature = "serde")))]
3+
14
use super::{biguint_from_vec, BigUint};
25

36
use alloc::vec::Vec;

src/lib.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,24 @@
7878
//! Note that you must use the version of `rand` that `num-bigint` is compatible
7979
//! with: `0.8`.
8080
//!
81+
//! ### Arbitrary Big Integers
82+
//!
83+
//! `num-bigint` supports `arbitrary` and `quickcheck` features to implement
84+
//! [`arbitrary::Arbitrary`] and [`quickcheck::Arbitrary`], respectively, for both `BigInt` and
85+
//! `BigUint`. These are useful for fuzzing and other forms of randomized testing.
86+
//!
87+
//! ### Serialization
88+
//!
89+
//! The `serde` feature adds implementations of [`Serialize`][serde::Serialize] and
90+
//! [`Deserialize`][serde::Deserialize] for both `BigInt` and `BigUint`. Their serialized data is
91+
//! generated portably, regardless of platform differences like the internal digit size.
92+
//!
8193
//!
8294
//! ## Compatibility
8395
//!
8496
//! The `num-bigint` crate is tested for rustc 1.60 and greater.
8597
98+
#![cfg_attr(docsrs, feature(doc_cfg))]
8699
#![doc(html_root_url = "https://docs.rs/num-bigint/0.4")]
87100
#![warn(rust_2018_idioms)]
88101
#![no_std]
@@ -99,10 +112,8 @@ use core::fmt;
99112
mod macros;
100113

101114
mod bigint;
102-
mod biguint;
103-
104-
#[cfg(feature = "rand")]
105115
mod bigrand;
116+
mod biguint;
106117

107118
#[cfg(target_pointer_width = "32")]
108119
type UsizePromotion = u32;
@@ -154,6 +165,7 @@ impl fmt::Display for ParseBigIntError {
154165
}
155166

156167
#[cfg(feature = "std")]
168+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
157169
impl std::error::Error for ParseBigIntError {
158170
fn description(&self) -> &str {
159171
self.__description()
@@ -183,6 +195,7 @@ impl<T> TryFromBigIntError<T> {
183195
}
184196

185197
#[cfg(feature = "std")]
198+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
186199
impl<T> std::error::Error for TryFromBigIntError<T>
187200
where
188201
T: fmt::Debug,
@@ -208,6 +221,7 @@ pub use crate::bigint::Sign;
208221
pub use crate::bigint::ToBigInt;
209222

210223
#[cfg(feature = "rand")]
224+
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
211225
pub use crate::bigrand::{RandBigInt, RandomBits, UniformBigInt, UniformBigUint};
212226

213227
mod big_digit {

0 commit comments

Comments
 (0)