Skip to content

Commit 753b19f

Browse files
authored
[docs] Docs and doctests succeed for all features (#369)
`cargo test --doc` and `cargo doc` now succeed on all feature combinations. Previously, we had a separate CI job step for running doc tests, and we had doc tests disabled by default in `Cargo.toml`. Now, we can get rid of the separate CI job, and we can get rid of the disablement in `Cargo.toml`.
1 parent 52ffa4d commit 753b19f

File tree

4 files changed

+19
-36
lines changed

4 files changed

+19
-36
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,6 @@ jobs:
179179
# wasm32-wasi once those work.
180180
if: matrix.toolchain == 'nightly' && matrix.target != 'riscv64gc-unknown-linux-gnu' && matrix.target != 'wasm32-wasi'
181181

182-
- name: Run doc tests
183-
# We explicitly pass `--doc` here because doc tests are disabled by
184-
# default in zerocopy's `Cargo.toml`. This is because some doc examples
185-
# make use of derives, and so would fail without the `derive` feature
186-
# enabled. We skip this step for `zerocopy` when the `derive` feature is
187-
# omitted for that reason.
188-
run: ./cargo.sh +${{ matrix.toolchain }} test --doc --package ${{ matrix.crate }} --target ${{ matrix.target }} ${{ matrix.features }} --verbose
189-
# Only run tests when targetting x86 (32- or 64-bit) - we're executing on
190-
# x86_64, so we can't run tests for any non-x86 target.
191-
#
192-
# TODO(https://github.com/dtolnay/trybuild/issues/184#issuecomment-1269097742):
193-
# Run compile tests when building for other targets.
194-
if: ${{ (contains(matrix.target, 'x86_64') || contains(matrix.target, 'i686')) && !(matrix.crate == 'zerocopy' && !contains(matrix.features, 'derive')) }}
195-
196182
- name: Clippy check
197183
run: ./cargo.sh +${{ matrix.toolchain }} clippy --package ${{ matrix.crate }} --target ${{ matrix.target }} ${{ matrix.features }} --tests --verbose
198184
# Clippy improves the accuracy of lints over time, and fixes bugs. Only
@@ -210,12 +196,11 @@ jobs:
210196
run: |
211197
export RUSTDOCFLAGS="${{ matrix.toolchain == 'nightly' && '-Z unstable-options --document-hidden-items' || '' }} $RUSTDOCFLAGS"
212198
./cargo.sh +${{ matrix.toolchain }} doc --document-private-items --package ${{ matrix.crate }} ${{ matrix.features }}
213-
# When the `alloc` feature is disabled, `cargo doc` fails because we link
214-
# to `alloc::vec::Vec` in a doc comment, and the `alloc` crate is not in
215-
# scope without the `alloc` feature. This isn't a big deal because we care
216-
# primarily about `cargo doc` working for `docs.rs`, which enables the
217-
# `alloc` feature.
218-
if: matrix.features != '' && matrix.features != '--no-default-features'
199+
# When the `byteorder` feature is disabled, `cargo doc` fails because we
200+
# link to the `byteorder` module in doc comments. This isn't a big deal
201+
# because we primarily care about `cargo doc` working for `docs.rs`, which
202+
# enables the `byteorder` feature.
203+
if: matrix.features != '--no-default-features'
219204

220205
# Check semver compatibility with the most recently-published version on
221206
# crates.io. We do this in the matrix rather than in its own job so that it

Cargo.toml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@ all-features = true
2828
pinned-stable = "1.69.0"
2929
pinned-nightly = "nightly-2023-05-25"
3030

31-
# Don't run doctests during `cargo test` without the `--doc` flag (the `--doc`
32-
# flag will still cause doctests to run despite this directive). Running
33-
# doctests without the `derive` feature fails because our doc examples make use
34-
# of zerocopy-derive, which is an optional dependency and is disabled by
35-
# default. Unfortunately, there's no way to automatically include
36-
# zerocopy-derive as a dependency during doctests, so we have to do it manually
37-
# by passing the appropriate flags in CI.
38-
[lib]
39-
doctest = false
40-
# Not technically necessary - this is the default value for `path` - but if a
41-
# `lib` section is present, then `cargo readme` expects it to have a `path`
42-
# field, and will fail if it's missing.
43-
path = "src/lib.rs"
44-
4531
[features]
4632
default = ["byteorder"]
4733

src/byteorder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
//! One use of these types is for representing network packet formats, such as
3030
//! UDP:
3131
//!
32-
//! ```edition2021
32+
//! ```rust,edition2021
33+
//! # #[cfg(feature = "derive")] { // This example uses derives, and won't compile without them
3334
//! use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeroes, Ref, Unaligned};
3435
//! use zerocopy::byteorder::network_endian::U16;
3536
//!
@@ -59,6 +60,7 @@
5960
//!
6061
//! // more getters...
6162
//! }
63+
//! # }
6264
//! ```
6365
6466
use core::{

src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,7 @@ macro_rules! transmute {
13811381
/// simply a reference to that type.
13821382
///
13831383
/// ```rust
1384+
/// # #[cfg(feature = "derive")] { // This example uses derives, and won't compile without them
13841385
/// use zerocopy::{AsBytes, ByteSlice, ByteSliceMut, FromBytes, FromZeroes, Ref, Unaligned};
13851386
///
13861387
/// #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
@@ -1413,6 +1414,7 @@ macro_rules! transmute {
14131414
/// self.header.src_port = src_port;
14141415
/// }
14151416
/// }
1417+
/// # }
14161418
/// ```
14171419
pub struct Ref<B, T: ?Sized>(B, PhantomData<T>);
14181420

@@ -2404,8 +2406,16 @@ mod sealed {
24042406
/// method would involve reallocation, and `split_at` must be a very cheap
24052407
/// operation in order for the utilities in this crate to perform as designed.
24062408
///
2407-
/// [`Vec<u8>`]: alloc::vec::Vec
24082409
/// [`split_at`]: crate::ByteSlice::split_at
2410+
// It may seem overkill to go to this length to ensure that this doc link never
2411+
// breaks. We do this because it simplifies CI - it means that generating docs
2412+
// always succeeds, so we don't need special logic to only generate docs under
2413+
// certain features.
2414+
#[cfg_attr(feature = "alloc", doc = "[`Vec<u8>`]: alloc::vec::Vec")]
2415+
#[cfg_attr(
2416+
not(feature = "alloc"),
2417+
doc = "[`Vec<u8>`]: https://doc.rust-lang.org/std/vec/struct.Vec.html"
2418+
)]
24092419
pub unsafe trait ByteSlice:
24102420
Deref<Target = [u8]> + Sized + self::sealed::ByteSliceSealed
24112421
{

0 commit comments

Comments
 (0)