diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4f7b1d93..e715868b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Install Rust - run: rustup update stable && rustup default stable + run: rustup update stable - name: Check formatting run: cargo fmt --all -- --check @@ -52,7 +52,7 @@ jobs: - name: Install Rust run: rustup update 1.39.0 && rustup default 1.39.0 - name: Check - run: . ci/test-stable.sh check + run: ci/test-stable.sh check # Stable stable: @@ -68,9 +68,9 @@ jobs: - uses: actions/checkout@v3 - name: Install Rust # --no-self-update is necessary because the windows environment cannot self-update rustup.exe. - run: rustup update stable --no-self-update && rustup default stable + run: rustup update stable --no-self-update - name: Test - run: . ci/test-stable.sh test + run: ci/test-stable.sh test # Nightly nightly: @@ -81,7 +81,7 @@ jobs: - name: Install Rust run: rustup update $nightly && rustup default $nightly - name: Test - run: . ci/test-stable.sh test + run: ci/test-stable.sh test # Run tests on some extra platforms cross: @@ -98,7 +98,7 @@ jobs: steps: - uses: actions/checkout@v3 - name: Install Rust - run: rustup update stable && rustup default stable + run: rustup update stable - name: cross build --target ${{ matrix.target }} run: | cargo install cross @@ -111,6 +111,26 @@ jobs: cargo build --target ${{ matrix.target }} if: matrix.target == 'wasm32-unknown-unknown' + # Build for no_std environment. + no-std: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install Rust + run: rustup update stable + - name: Install cargo-hack + run: cargo install cargo-hack + # thumbv7m-none-eabi supports atomic CAS. + # thumbv6m-none-eabi supports atomic, but not atomic CAS. + - run: rustup target add thumbv7m-none-eabi + - run: rustup target add thumbv6m-none-eabi + # * --optional-deps is needed for serde feature + # * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866 + - run: cargo hack build --target thumbv7m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps + # A sound way to provide atomic CAS on platforms without native atomic CAS is system-dependent. + # portable-atomic provides major ways via cfgs and accepts user-defined implementations via critical-section feature. + - run: cargo hack build --target thumbv6m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps --features extra-platforms,portable-atomic/critical-section + # Sanitizers tsan: name: tsan @@ -122,7 +142,7 @@ jobs: - name: Install rust-src run: rustup component add rust-src - name: ASAN / TSAN - run: . ci/tsan.sh + run: ci/tsan.sh miri: name: miri runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 4a96ec1ed..9af5d2a6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,9 +20,14 @@ edition = "2018" [features] default = ["std"] std = [] +# Use portable-atomic crate to support platforms without atomic CAS. +# See https://docs.rs/portable-atomic for more information. +extra-platforms = ["portable-atomic"] [dependencies] serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] } +# Enable require-cas feature to provide a better error message if the end user forgets to use the cfg or feature. +portable-atomic = { version = "1.3", optional = true, default-features = false, features = ["require-cas"] } [dev-dependencies] serde_test = "1.0" diff --git a/ci/test-stable.sh b/ci/test-stable.sh old mode 100644 new mode 100755 index 4421f3a97..3b62de653 --- a/ci/test-stable.sh +++ b/ci/test-stable.sh @@ -22,7 +22,10 @@ if [[ "${RUST_VERSION}" == "nightly"* ]]; then cargo check --benches # Check minimal versions - cargo clean - cargo update -Zminimal-versions + # Remove dev-dependencies from Cargo.toml to prevent the next `cargo update` + # from determining minimal versions based on dev-dependencies. + cargo hack --remove-dev-deps --workspace + # Update Cargo.lock to minimal version dependencies. + cargo update -Z minimal-versions cargo check --all-features fi diff --git a/ci/tsan.sh b/ci/tsan.sh old mode 100644 new mode 100755 diff --git a/src/buf/chain.rs b/src/buf/chain.rs index 78979a123..2011e9edc 100644 --- a/src/buf/chain.rs +++ b/src/buf/chain.rs @@ -1,5 +1,5 @@ use crate::buf::{IntoIter, UninitSlice}; -use crate::{Buf, BufMut, Bytes}; +use crate::{Buf, BufMut}; #[cfg(feature = "std")] use std::io::IoSlice; @@ -171,7 +171,7 @@ where n } - fn copy_to_bytes(&mut self, len: usize) -> Bytes { + fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { let a_rem = self.a.remaining(); if a_rem >= len { self.a.copy_to_bytes(len) diff --git a/src/buf/take.rs b/src/buf/take.rs index d3cb10ab6..c4436f288 100644 --- a/src/buf/take.rs +++ b/src/buf/take.rs @@ -1,4 +1,4 @@ -use crate::{Buf, Bytes}; +use crate::Buf; use core::cmp; @@ -145,7 +145,7 @@ impl Buf for Take { self.limit -= cnt; } - fn copy_to_bytes(&mut self, len: usize) -> Bytes { + fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { assert!(len <= self.remaining(), "`len` greater than remaining"); let r = self.inner.copy_to_bytes(len); diff --git a/src/loom.rs b/src/loom.rs index 9e6b2d5e2..7d83b90ea 100644 --- a/src/loom.rs +++ b/src/loom.rs @@ -1,7 +1,10 @@ #[cfg(not(all(test, loom)))] pub(crate) mod sync { pub(crate) mod atomic { + #[cfg(not(feature = "extra-platforms"))] pub(crate) use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering}; + #[cfg(feature = "extra-platforms")] + pub(crate) use portable_atomic::{AtomicPtr, AtomicUsize, Ordering}; pub(crate) trait AtomicMut { fn with_mut(&mut self, f: F) -> R