From 4ab919f1ab40248ff02581b42aead4a2ce0ea4e4 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Wed, 20 Jan 2021 15:24:13 +0900 Subject: [PATCH 1/8] Support non-atomic targets --- ci/test-stable.sh | 6 ++++++ src/buf/buf_impl.rs | 2 ++ src/lib.rs | 7 +++++++ src/loom.rs | 2 ++ 4 files changed, 17 insertions(+) diff --git a/ci/test-stable.sh b/ci/test-stable.sh index 01a32f5a6..f5b4fa6cc 100644 --- a/ci/test-stable.sh +++ b/ci/test-stable.sh @@ -17,6 +17,12 @@ cargo "${cmd}" --all-features cargo doc --no-deps --all-features if [[ "${RUST_VERSION}" == "nightly"* ]]; then + # Check for no_std environment. + rustup target add thumbv7m-none-eabi + rustup target add thumbv6m-none-eabi + cargo build --no-default-features --target thumbv7m-none-eabi + RUSTFLAGS="--cfg bytes_unstable -Dwarnings" cargo build --no-default-features --target thumbv6m-none-eabi + # Check benchmarks cargo check --benches diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 16ad8a7ee..b7fd59bdd 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -810,6 +810,7 @@ pub trait Buf { /// let bytes = (&b"hello world"[..]).copy_to_bytes(5); /// assert_eq!(&bytes[..], &b"hello"[..]); /// ``` + #[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { use super::BufMut; @@ -1001,6 +1002,7 @@ macro_rules! deref_forward_buf { (**self).get_int_le(nbytes) } + #[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { (**self).copy_to_bytes(len) } diff --git a/src/lib.rs b/src/lib.rs index dd8cc9661..fb78f3639 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ ))] #![doc(html_root_url = "https://docs.rs/bytes/1.0.1")] #![no_std] +#![cfg_attr(bytes_unstable, feature(cfg_target_has_atomic))] //! Provides abstractions for working with bytes. //! @@ -84,17 +85,23 @@ extern crate std; pub mod buf; pub use crate::buf::{Buf, BufMut}; +#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] mod bytes; +#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] mod bytes_mut; +#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] mod fmt; mod loom; +#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] pub use crate::bytes::Bytes; +#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] pub use crate::bytes_mut::BytesMut; // Optional Serde support #[cfg(feature = "serde")] mod serde; +#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] #[inline(never)] #[cold] fn abort() -> ! { diff --git a/src/loom.rs b/src/loom.rs index 1cae8812e..dad6e23b8 100644 --- a/src/loom.rs +++ b/src/loom.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), allow(unused_imports))] + #[cfg(not(all(test, loom)))] pub(crate) mod sync { pub(crate) mod atomic { From 3dbe14b265249f60aaadb2b6cbddf95dce5480a0 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 23 May 2021 17:44:49 +0900 Subject: [PATCH 2/8] Remove uses of unstable feature(cfg_target_has_atomic) --- .github/workflows/ci.yml | 38 ++++++++++++++++++++++++++++++-------- build.rs | 31 +++++++++++++++++++++++++++++++ ci/no_atomic_cas.sh | 27 +++++++++++++++++++++++++++ ci/test-stable.sh | 13 +++++-------- ci/tsan.sh | 0 no_atomic_cas.rs | 11 +++++++++++ src/buf/buf_impl.rs | 4 ++-- src/lib.rs | 14 +++++++------- src/loom.rs | 1 + 9 files changed, 114 insertions(+), 25 deletions(-) create mode 100644 build.rs create mode 100755 ci/no_atomic_cas.sh mode change 100644 => 100755 ci/test-stable.sh mode change 100644 => 100755 ci/tsan.sh create mode 100644 no_atomic_cas.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fc03588df..34dbdacc8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,7 +25,7 @@ jobs: steps: - uses: actions/checkout@v2 - 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@v2 - 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@v2 - 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,28 @@ jobs: cargo build --target ${{ matrix.target }} if: matrix.target == 'wasm32-unknown-unknown' + # Build for no_std environment. + no-std: + strategy: + fail-fast: false + matrix: + # thumbv7m-none-eabi supports atomic CAS. + # thumbv6m-none-eabi supports atomic, but not atomic CAS. + # riscv32i-unknown-none-elf does not support atomic at all. + target: + - thumbv7m-none-eabi + - thumbv6m-none-eabi + - riscv32i-unknown-none-elf + 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 + - run: rustup target add ${{ matrix.target }} + - run: cargo hack build --target ${{ matrix.target }} --feature-powerset --skip std,default --optional-deps --no-dev-deps + # Sanitizers tsan: name: tsan @@ -122,7 +144,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 @@ -157,7 +179,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Install Rust - run: rustup update stable && rustup default stable + run: rustup update stable - name: Build documentation run: cargo doc --no-deps --all-features - name: Publish documentation diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..b71e3cab3 --- /dev/null +++ b/build.rs @@ -0,0 +1,31 @@ +#![warn(rust_2018_idioms)] + +use std::env; + +include!("no_atomic_cas.rs"); + +// The rustc-cfg strings below are *not* public API. Please let us know by +// opening a GitHub issue if your build environment requires some way to enable +// these cfgs other than by executing our build script. +fn main() { + let target = match env::var("TARGET") { + Ok(target) => target, + Err(e) => { + println!( + "cargo:warning=bytes: unable to get TARGET environment variable: {}", + e + ); + return; + } + }; + + // Note that this is `no_*`, not `has_*`. This allows treating + // `cfg(target_has_atomic = "ptr")` as true when the build script doesn't + // run. This is needed for compatibility with non-cargo build systems that + // don't run the build script. + if NO_ATOMIC_CAS.contains(&&*target) { + println!("cargo:rustc-cfg=bytes_no_atomic_cas"); + } + + println!("cargo:rerun-if-changed=no_atomic_cas.rs"); +} diff --git a/ci/no_atomic_cas.sh b/ci/no_atomic_cas.sh new file mode 100755 index 000000000..bc2e3500f --- /dev/null +++ b/ci/no_atomic_cas.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Update the list of targets that do not support atomic CAS operations. +# +# Usage: +# ./ci/no_atomic_cas.sh + +set -euo pipefail +IFS=$'\n\t' + +cd "$(cd "$(dirname "$0")" && pwd)"/.. + +file="no_atomic_cas.rs" + +{ + echo "// This file is @generated by $(basename "$0")." + echo "// It is not intended for manual editing." + echo "" +} >"$file" + +echo "const NO_ATOMIC_CAS: &[&str] = &[" >>"$file" +for target in $(rustc --print target-list); do + res=$(rustc --print target-spec-json -Z unstable-options --target "$target" \ + | jq -r "select(.\"atomic-cas\" == false)") + [[ -z "$res" ]] || echo " \"$target\"," >>"$file" +done +echo "];" >>"$file" diff --git a/ci/test-stable.sh b/ci/test-stable.sh old mode 100644 new mode 100755 index f5b4fa6cc..212b43c7a --- a/ci/test-stable.sh +++ b/ci/test-stable.sh @@ -17,17 +17,14 @@ cargo "${cmd}" --all-features cargo doc --no-deps --all-features if [[ "${RUST_VERSION}" == "nightly"* ]]; then - # Check for no_std environment. - rustup target add thumbv7m-none-eabi - rustup target add thumbv6m-none-eabi - cargo build --no-default-features --target thumbv7m-none-eabi - RUSTFLAGS="--cfg bytes_unstable -Dwarnings" cargo build --no-default-features --target thumbv6m-none-eabi - # Check benchmarks 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/no_atomic_cas.rs b/no_atomic_cas.rs new file mode 100644 index 000000000..9e8fb4094 --- /dev/null +++ b/no_atomic_cas.rs @@ -0,0 +1,11 @@ +// This file is @generated by no_atomic_cas.sh. +// It is not intended for manual editing. + +const NO_ATOMIC_CAS: &[&str] = &[ + "avr-unknown-gnu-atmega328", + "msp430-none-elf", + "riscv32i-unknown-none-elf", + "riscv32imc-unknown-none-elf", + "thumbv4t-none-eabi", + "thumbv6m-none-eabi", +]; diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index b7fd59bdd..50ee6f424 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -810,7 +810,7 @@ pub trait Buf { /// let bytes = (&b"hello world"[..]).copy_to_bytes(5); /// assert_eq!(&bytes[..], &b"hello"[..]); /// ``` - #[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] + #[cfg(not(bytes_no_atomic_cas))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { use super::BufMut; @@ -1002,7 +1002,7 @@ macro_rules! deref_forward_buf { (**self).get_int_le(nbytes) } - #[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] + #[cfg(not(bytes_no_atomic_cas))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { (**self).copy_to_bytes(len) } diff --git a/src/lib.rs b/src/lib.rs index fb78f3639..3e20c0adc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,6 @@ ))] #![doc(html_root_url = "https://docs.rs/bytes/1.0.1")] #![no_std] -#![cfg_attr(bytes_unstable, feature(cfg_target_has_atomic))] //! Provides abstractions for working with bytes. //! @@ -85,23 +84,24 @@ extern crate std; pub mod buf; pub use crate::buf::{Buf, BufMut}; -#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] +#[cfg(not(bytes_no_atomic_cas))] mod bytes; -#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] +#[cfg(not(bytes_no_atomic_cas))] mod bytes_mut; -#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] +#[cfg(not(bytes_no_atomic_cas))] mod fmt; mod loom; -#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] +#[cfg(not(bytes_no_atomic_cas))] pub use crate::bytes::Bytes; -#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] +#[cfg(not(bytes_no_atomic_cas))] pub use crate::bytes_mut::BytesMut; // Optional Serde support +#[cfg(not(bytes_no_atomic_cas))] #[cfg(feature = "serde")] mod serde; -#[cfg_attr(bytes_unstable, cfg(target_has_atomic = "ptr"))] +#[cfg(not(bytes_no_atomic_cas))] #[inline(never)] #[cold] fn abort() -> ! { diff --git a/src/loom.rs b/src/loom.rs index dad6e23b8..09fc71e09 100644 --- a/src/loom.rs +++ b/src/loom.rs @@ -2,6 +2,7 @@ #[cfg(not(all(test, loom)))] pub(crate) mod sync { + #[cfg(not(bytes_no_atomic_cas))] pub(crate) mod atomic { pub(crate) use core::sync::atomic::{fence, AtomicPtr, AtomicUsize, Ordering}; From 6bad5b00fe705bfa084c30cc0f751755a41b4d31 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 23 May 2021 17:50:50 +0900 Subject: [PATCH 3/8] fix --- src/buf/chain.rs | 5 +++-- src/buf/take.rs | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/buf/chain.rs b/src/buf/chain.rs index 9ce5f23aa..a87d7c9a0 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,8 @@ where n } - fn copy_to_bytes(&mut self, len: usize) -> Bytes { + #[cfg(not(bytes_no_atomic_cas))] + 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..6ca190c9d 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,8 @@ impl Buf for Take { self.limit -= cnt; } - fn copy_to_bytes(&mut self, len: usize) -> Bytes { + #[cfg(not(bytes_no_atomic_cas))] + 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); From 22292b59244723a351c67811cf4b1a981624d203 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 23 May 2021 17:56:32 +0900 Subject: [PATCH 4/8] setup codegen ci --- .github/workflows/ci.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 296b24c99..64ce5db7d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -133,6 +133,19 @@ jobs: - run: rustup target add ${{ matrix.target }} - run: cargo hack build --target ${{ matrix.target }} --feature-powerset --skip std,default --optional-deps --no-dev-deps + # When this job failed, run ci/no_atomic_cas.sh and commit result changes. + # TODO(taiki-e): Ideally, this should be automated using a bot that creates + # PR when failed, but there is no bandwidth to implement it + # right now... + codegen: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install Rust + run: rustup update nightly && rustup default nightly + - run: ci/no_atomic_cas.sh + - run: git diff --exit-code + # Sanitizers tsan: name: tsan From 2105374d25af2347ce4fb0a0bea6cf53e183e30e Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 23 May 2021 18:02:32 +0900 Subject: [PATCH 5/8] add comment --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 64ce5db7d..46fb297c3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -131,6 +131,8 @@ jobs: - name: Install cargo-hack run: cargo install cargo-hack - run: rustup target add ${{ matrix.target }} + # * --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 ${{ matrix.target }} --feature-powerset --skip std,default --optional-deps --no-dev-deps # When this job failed, run ci/no_atomic_cas.sh and commit result changes. From 77f30824ff886c27fcb5a599f06079e2f20f17a8 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 19 Jun 2021 00:00:14 +0900 Subject: [PATCH 6/8] update list --- no_atomic_cas.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/no_atomic_cas.rs b/no_atomic_cas.rs index 9e8fb4094..9b05d4b9f 100644 --- a/no_atomic_cas.rs +++ b/no_atomic_cas.rs @@ -3,6 +3,8 @@ const NO_ATOMIC_CAS: &[&str] = &[ "avr-unknown-gnu-atmega328", + "bpfeb-unknown-none", + "bpfel-unknown-none", "msp430-none-elf", "riscv32i-unknown-none-elf", "riscv32imc-unknown-none-elf", From 2be8c5e64aed95ebefb24b0ceeab1197c4795226 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 31 Mar 2023 04:01:32 +0900 Subject: [PATCH 7/8] Use portable-atomic and remove our own logic for now Adopt the third idea of https://github.com/tokio-rs/bytes/pull/573#issuecomment-1271567631. --- .github/workflows/ci.yml | 37 ++++++++++--------------------------- Cargo.toml | 4 ++++ build.rs | 31 ------------------------------- ci/no_atomic_cas.sh | 27 --------------------------- no_atomic_cas.rs | 13 ------------- src/buf/buf_impl.rs | 2 -- src/buf/chain.rs | 1 - src/buf/take.rs | 1 - src/lib.rs | 7 ------- src/loom.rs | 6 +++--- 10 files changed, 17 insertions(+), 112 deletions(-) delete mode 100644 build.rs delete mode 100755 ci/no_atomic_cas.sh delete mode 100644 no_atomic_cas.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 12ee4d62d..e715868b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -113,16 +113,6 @@ jobs: # Build for no_std environment. no-std: - strategy: - fail-fast: false - matrix: - # thumbv7m-none-eabi supports atomic CAS. - # thumbv6m-none-eabi supports atomic, but not atomic CAS. - # riscv32i-unknown-none-elf does not support atomic at all. - target: - - thumbv7m-none-eabi - - thumbv6m-none-eabi - - riscv32i-unknown-none-elf runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -130,23 +120,16 @@ jobs: run: rustup update stable - name: Install cargo-hack run: cargo install cargo-hack - - run: rustup target add ${{ matrix.target }} - # * --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 ${{ matrix.target }} --feature-powerset --skip std,default --optional-deps --no-dev-deps - - # When this job failed, run ci/no_atomic_cas.sh and commit result changes. - # TODO(taiki-e): Ideally, this should be automated using a bot that creates - # PR when failed, but there is no bandwidth to implement it - # right now... - codegen: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Install Rust - run: rustup update nightly && rustup default nightly - - run: ci/no_atomic_cas.sh - - run: git diff --exit-code + # 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: diff --git a/Cargo.toml b/Cargo.toml index 4a96ec1ed..2765fd064 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,9 +20,13 @@ 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"] } +portable-atomic = { version = "1", optional = true, default-features = false } [dev-dependencies] serde_test = "1.0" diff --git a/build.rs b/build.rs deleted file mode 100644 index b71e3cab3..000000000 --- a/build.rs +++ /dev/null @@ -1,31 +0,0 @@ -#![warn(rust_2018_idioms)] - -use std::env; - -include!("no_atomic_cas.rs"); - -// The rustc-cfg strings below are *not* public API. Please let us know by -// opening a GitHub issue if your build environment requires some way to enable -// these cfgs other than by executing our build script. -fn main() { - let target = match env::var("TARGET") { - Ok(target) => target, - Err(e) => { - println!( - "cargo:warning=bytes: unable to get TARGET environment variable: {}", - e - ); - return; - } - }; - - // Note that this is `no_*`, not `has_*`. This allows treating - // `cfg(target_has_atomic = "ptr")` as true when the build script doesn't - // run. This is needed for compatibility with non-cargo build systems that - // don't run the build script. - if NO_ATOMIC_CAS.contains(&&*target) { - println!("cargo:rustc-cfg=bytes_no_atomic_cas"); - } - - println!("cargo:rerun-if-changed=no_atomic_cas.rs"); -} diff --git a/ci/no_atomic_cas.sh b/ci/no_atomic_cas.sh deleted file mode 100755 index bc2e3500f..000000000 --- a/ci/no_atomic_cas.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -# Update the list of targets that do not support atomic CAS operations. -# -# Usage: -# ./ci/no_atomic_cas.sh - -set -euo pipefail -IFS=$'\n\t' - -cd "$(cd "$(dirname "$0")" && pwd)"/.. - -file="no_atomic_cas.rs" - -{ - echo "// This file is @generated by $(basename "$0")." - echo "// It is not intended for manual editing." - echo "" -} >"$file" - -echo "const NO_ATOMIC_CAS: &[&str] = &[" >>"$file" -for target in $(rustc --print target-list); do - res=$(rustc --print target-spec-json -Z unstable-options --target "$target" \ - | jq -r "select(.\"atomic-cas\" == false)") - [[ -z "$res" ]] || echo " \"$target\"," >>"$file" -done -echo "];" >>"$file" diff --git a/no_atomic_cas.rs b/no_atomic_cas.rs deleted file mode 100644 index 9b05d4b9f..000000000 --- a/no_atomic_cas.rs +++ /dev/null @@ -1,13 +0,0 @@ -// This file is @generated by no_atomic_cas.sh. -// It is not intended for manual editing. - -const NO_ATOMIC_CAS: &[&str] = &[ - "avr-unknown-gnu-atmega328", - "bpfeb-unknown-none", - "bpfel-unknown-none", - "msp430-none-elf", - "riscv32i-unknown-none-elf", - "riscv32imc-unknown-none-elf", - "thumbv4t-none-eabi", - "thumbv6m-none-eabi", -]; diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index 939d517cb..366cfc989 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -1100,7 +1100,6 @@ pub trait Buf { /// let bytes = (&b"hello world"[..]).copy_to_bytes(5); /// assert_eq!(&bytes[..], &b"hello"[..]); /// ``` - #[cfg(not(bytes_no_atomic_cas))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { use super::BufMut; @@ -1325,7 +1324,6 @@ macro_rules! deref_forward_buf { (**self).get_int_ne(nbytes) } - #[cfg(not(bytes_no_atomic_cas))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { (**self).copy_to_bytes(len) } diff --git a/src/buf/chain.rs b/src/buf/chain.rs index 8b058d263..2011e9edc 100644 --- a/src/buf/chain.rs +++ b/src/buf/chain.rs @@ -171,7 +171,6 @@ where n } - #[cfg(not(bytes_no_atomic_cas))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { let a_rem = self.a.remaining(); if a_rem >= len { diff --git a/src/buf/take.rs b/src/buf/take.rs index 6ca190c9d..c4436f288 100644 --- a/src/buf/take.rs +++ b/src/buf/take.rs @@ -145,7 +145,6 @@ impl Buf for Take { self.limit -= cnt; } - #[cfg(not(bytes_no_atomic_cas))] fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes { assert!(len <= self.remaining(), "`len` greater than remaining"); diff --git a/src/lib.rs b/src/lib.rs index d3e753055..af436b316 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,24 +84,17 @@ extern crate std; pub mod buf; pub use crate::buf::{Buf, BufMut}; -#[cfg(not(bytes_no_atomic_cas))] mod bytes; -#[cfg(not(bytes_no_atomic_cas))] mod bytes_mut; -#[cfg(not(bytes_no_atomic_cas))] mod fmt; mod loom; -#[cfg(not(bytes_no_atomic_cas))] pub use crate::bytes::Bytes; -#[cfg(not(bytes_no_atomic_cas))] pub use crate::bytes_mut::BytesMut; // Optional Serde support -#[cfg(not(bytes_no_atomic_cas))] #[cfg(feature = "serde")] mod serde; -#[cfg(not(bytes_no_atomic_cas))] #[inline(never)] #[cold] fn abort() -> ! { diff --git a/src/loom.rs b/src/loom.rs index e30555c3d..7d83b90ea 100644 --- a/src/loom.rs +++ b/src/loom.rs @@ -1,10 +1,10 @@ -#![cfg_attr(not(feature = "std"), allow(unused_imports))] - #[cfg(not(all(test, loom)))] pub(crate) mod sync { - #[cfg(not(bytes_no_atomic_cas))] 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 From c50b62159e542cc52e28515eacc4c4feb05b5eab Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sun, 4 Jun 2023 23:06:05 +0900 Subject: [PATCH 8/8] Enable portable-atomic's require-cas feature This provides a better error message if the end user forgets to use the cfg or feature. --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2765fd064..9af5d2a6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,8 @@ extra-platforms = ["portable-atomic"] [dependencies] serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] } -portable-atomic = { version = "1", optional = true, default-features = false } +# 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"