Skip to content

Commit 986e3c5

Browse files
authored
[unsafe-fields] Initial commit (#1929)
Makes progress on #1931 gherrit-pr-id: If0e198c377137dd941ebd5dc68787766a593e1eb
1 parent 300ddc2 commit 986e3c5

File tree

9 files changed

+544
-4
lines changed

9 files changed

+544
-4
lines changed

.github/workflows/ci.yml

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ permissions: read-all
1616

1717
env:
1818
CARGO_TERM_COLOR: always
19+
RUST_BACKTRACE: 1
1920
RUSTFLAGS: -Dwarnings
2021
RUSTDOCFLAGS: -Dwarnings
2122
# `ZC_NIGHTLY_XXX` are flags that we add to `XXX` only on the nightly
@@ -507,6 +508,73 @@ jobs:
507508
# `roll-pinned-toolchain-versions.yml`.
508509
kani-version: 0.55.0
509510

511+
unsafe_fields:
512+
runs-on: ubuntu-latest
513+
needs: generate_cache
514+
strategy:
515+
# By default, this is set to `true`, which means that a single CI job
516+
# failure will cause all outstanding jobs to be canceled. This slows down
517+
# development because it means that errors need to be encountered and
518+
# fixed one at a time.
519+
fail-fast: false
520+
matrix:
521+
toolchain: [
522+
"msrv",
523+
"stable",
524+
"nightly",
525+
]
526+
steps:
527+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
528+
- name: Configure environment variables
529+
run: |
530+
set -eo pipefail
531+
ZC_TOOLCHAIN="$(./cargo.sh --version ${{ matrix.toolchain }})"
532+
echo "ZC_TOOLCHAIN=$ZC_TOOLCHAIN" >> $GITHUB_ENV
533+
- name: Install stable Rust for use in 'cargo.sh'
534+
uses: dtolnay/rust-toolchain@00b49be78f40fba4e87296b2ead62868750bdd83 # stable
535+
with:
536+
toolchain: stable
537+
- name: Install Rust with nightly toolchain (${{ env.ZC_TOOLCHAIN }}) and target aarch64_be-unknown-linux-gnu
538+
uses: dtolnay/rust-toolchain@00b49be78f40fba4e87296b2ead62868750bdd83 # stable
539+
with:
540+
toolchain: ${{ env.ZC_TOOLCHAIN }}
541+
components: clippy, rust-src
542+
- name: Check
543+
run: ./cargo.sh +${{ matrix.toolchain }} check --package unsafe-fields --verbose
544+
- name: Check tests
545+
run: ./cargo.sh +${{ matrix.toolchain }} check --tests --package unsafe-fields --verbose
546+
- name: Build
547+
run: ./cargo.sh +${{ matrix.toolchain }} build --package unsafe-fields --verbose
548+
- name: Run tests
549+
run: ./cargo.sh +${{ matrix.toolchain }} test --package unsafe-fields --verbose
550+
- name: Clippy
551+
run: ./cargo.sh +${{ matrix.toolchain }} clippy --package unsafe-fields --verbose
552+
# See comment in next step for why we only run on nightly.
553+
if: matrix.toolchain == 'nightly'
554+
- name: Clippy tests
555+
run: ./cargo.sh +${{ matrix.toolchain }} clippy --package unsafe-fields --tests --verbose
556+
# Clippy improves the accuracy of lints over time, and fixes bugs. Only
557+
# running Clippy on nightly allows us to avoid having to write code
558+
# which is compatible with older versions of Clippy, which sometimes
559+
# requires hacks to work around limitations that are fixed in more
560+
# recent versions.
561+
if: matrix.toolchain == 'nightly'
562+
- name: Cargo doc
563+
# We pass --document-private-items and --document-hidden items to ensure
564+
# that documentation always builds even for these items. This makes
565+
# future changes to make those items public/non-hidden more painless.
566+
# Note that --document-hidden-items is unstable; if a future release
567+
# breaks or removes it, we can just update CI to no longer pass that
568+
# flag.
569+
run: |
570+
# Include arguments passed during docs.rs deployments to make sure those
571+
# work properly.
572+
set -eo pipefail
573+
METADATA_DOCS_RS_RUSTDOC_ARGS="$(cargo metadata --format-version 1 | \
574+
jq -r ".packages[] | select(.name == \"unsafe-fields\").metadata.docs.rs.\"rustdoc-args\"[]" | tr '\n' ' ')"
575+
export RUSTDOCFLAGS="${{ matrix.toolchain == 'nightly' && '-Z unstable-options --document-hidden-items $METADATA_DOCS_RS_RUSTDOC_ARGS'|| '' }} $RUSTDOCFLAGS"
576+
./cargo.sh +${{ matrix.toolchain }} doc --document-private-items --package unsafe-fields
577+
510578
# NEON intrinsics are currently broken on big-endian platforms. [1] This test ensures
511579
# that we don't accidentally attempt to compile these intrinsics on such platforms. We
512580
# can't use this as part of the build matrix because rustup doesn't support the
@@ -670,7 +738,7 @@ jobs:
670738
# https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/troubleshooting-required-status-checks#handling-skipped-but-required-checks
671739
if: failure()
672740
runs-on: ubuntu-latest
673-
needs: [build_test, kani,check_be_aarch64 , check_fmt, check_readme, check_versions, generate_cache, check-all-toolchains-tested, check-job-dependencies, run-git-hooks]
741+
needs: [build_test, kani,check_be_aarch64, check_fmt, check_readme, check_versions, generate_cache, check-all-toolchains-tested, check-job-dependencies, run-git-hooks, unsafe_fields]
674742
steps:
675743
- name: Mark the job as failed
676744
run: exit 1

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
# https://github.com/dtolnay/trybuild/issues/207#issuecomment-131227.594
1313
[workspace]
1414

15+
[workspace.package]
16+
# Inherited by zerocopy and unsafe-fields.
17+
rust-version = "1.65.0"
18+
1519
[package]
1620
edition = "2021"
1721
name = "zerocopy"
@@ -22,7 +26,7 @@ categories = ["embedded", "encoding", "no-std::no-alloc", "parsing", "rust-patte
2226
keywords = ["cast", "convert", "transmute", "transmutation", "type-punning"]
2327
license = "BSD-2-Clause OR Apache-2.0 OR MIT"
2428
repository = "https://github.com/google/zerocopy"
25-
rust-version = "1.65.0"
29+
rust-version = { workspace = true }
2630

2731
exclude = [".*"]
2832

@@ -81,6 +85,7 @@ testutil = { path = "testutil" }
8185
# sometimes change the output format slightly, so a version mismatch can cause
8286
# CI test failures.
8387
trybuild = { version = "=1.0.90", features = ["diff"] }
88+
unsafe-fields = { path = "./unsafe-fields" }
8489
# In tests, unlike in production, zerocopy-derive is not optional
8590
zerocopy-derive = { version = "=0.9.0-alpha.0", path = "zerocopy-derive" }
8691
# TODO(#381) Remove this dependency once we have our own layout gadgets.

testutil/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl PinnedVersions {
4949
.ok_or(format!("expected string value for path in Cargo.toml: {:?}", keys))
5050
};
5151

52-
let msrv = extract(&["package", "rust-version"])?;
52+
let msrv = extract(&["workspace", "package", "rust-version"])?;
5353
let stable = extract(&["package", "metadata", "ci", "pinned-stable"])?;
5454
let nightly = extract(&["package", "metadata", "ci", "pinned-nightly"])?;
5555
Ok(PinnedVersions { msrv, stable, nightly })

tools/cargo-zerocopy/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,17 @@ impl Versions {
112112
fn get_toolchain_versions() -> Versions {
113113
let manifest_text = fs::read_to_string("Cargo.toml").unwrap();
114114
let manifest = toml::from_str::<Value>(&manifest_text).unwrap();
115+
let manifest_table = manifest.as_table().unwrap();
115116

117+
let workspace_package =
118+
manifest_table["workspace"].as_table().unwrap()["package"].as_table().unwrap();
116119
let package = manifest.as_table().unwrap()["package"].as_table().unwrap();
117120
let metadata = package["metadata"].as_table().unwrap();
118121
let build_rs = metadata["build-rs"].as_table().unwrap();
119122
let ci = metadata["ci"].as_table().unwrap();
120123

121124
Versions {
122-
msrv: package["rust-version"].as_str().unwrap().to_string(),
125+
msrv: workspace_package["rust-version"].as_str().unwrap().to_string(),
123126
stable: ci["pinned-stable"].as_str().unwrap().to_string(),
124127
nightly: ci["pinned-nightly"].as_str().unwrap().to_string(),
125128
build_rs: build_rs.clone(),

unsafe-fields/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2024 The Fuchsia Authors
2+
#
3+
# Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
# <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
# license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
# This file may not be copied, modified, or distributed except according to
7+
# those terms.
8+
9+
[package]
10+
name = "unsafe-fields"
11+
version = "0.1.0"
12+
edition = "2021"
13+
license = "BSD-2-Clause OR Apache-2.0 OR MIT"
14+
repository = "https://github.com/google/zerocopy"
15+
rust-version = { workspace = true }
16+
17+
exclude = [".*"]
18+
19+
[package.metadata.docs.rs]
20+
all-features = true
21+
rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"]

unsafe-fields/LICENSE-APACHE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE-APACHE

unsafe-fields/LICENSE-BSD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE-BSD

unsafe-fields/LICENSE-MIT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../LICENSE-MIT

0 commit comments

Comments
 (0)