Skip to content

Commit e0140d5

Browse files
taiki-eXLPhere
authored andcommitted
Support non-atomic targets
Remove uses of unstable feature(cfg_target_has_atomic) fix setup codegen ci add comment update list Use portable-atomic and remove our own logic for now Adopt the third idea of tokio-rs#573 (comment). Enable portable-atomic's require-cas feature This provides a better error message if the end user forgets to use the cfg or feature.
1 parent fa1daac commit e0140d5

File tree

7 files changed

+38
-6
lines changed

7 files changed

+38
-6
lines changed

.github/workflows/ci.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,26 @@ jobs:
118118
cargo build --target ${{ matrix.target }}
119119
if: matrix.target == 'wasm32-unknown-unknown'
120120

121+
# Build for no_std environment.
122+
no-std:
123+
runs-on: ubuntu-latest
124+
steps:
125+
- uses: actions/checkout@v2
126+
- name: Install Rust
127+
run: rustup update stable
128+
- name: Install cargo-hack
129+
run: cargo install cargo-hack
130+
# thumbv7m-none-eabi supports atomic CAS.
131+
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
132+
- run: rustup target add thumbv7m-none-eabi
133+
- run: rustup target add thumbv6m-none-eabi
134+
# * --optional-deps is needed for serde feature
135+
# * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866
136+
- run: cargo hack build --target thumbv7m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps
137+
# A sound way to provide atomic CAS on platforms without native atomic CAS is system-dependent.
138+
# portable-atomic provides major ways via cfgs and accepts user-defined implementations via critical-section feature.
139+
- 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
140+
121141
# Sanitizers
122142
tsan:
123143
name: tsan

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ categories = ["network-programming", "data-structures"]
2121
[features]
2222
default = ["std"]
2323
std = []
24+
# Use portable-atomic crate to support platforms without atomic CAS.
25+
# See https://docs.rs/portable-atomic for more information.
26+
extra-platforms = ["portable-atomic"]
2427

2528
[dependencies]
2629
serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] }
30+
# Enable require-cas feature to provide a better error message if the end user forgets to use the cfg or feature.
31+
portable-atomic = { version = "1.3", optional = true, default-features = false, features = ["require-cas"] }
2732

2833
[dev-dependencies]
2934
serde_test = "1.0"

ci/test-stable.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ if [[ "${RUST_VERSION}" == "nightly"* ]]; then
1616
cargo check --benches
1717

1818
# Check minimal versions
19-
cargo clean
20-
cargo update -Zminimal-versions
19+
# Remove dev-dependencies from Cargo.toml to prevent the next `cargo update`
20+
# from determining minimal versions based on dev-dependencies.
21+
cargo hack --remove-dev-deps --workspace
22+
# Update Cargo.lock to minimal version dependencies.
23+
cargo update -Z minimal-versions
2124
cargo check --all-features
2225
fi

src/buf/chain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::buf::{IntoIter, UninitSlice};
2-
use crate::{Buf, BufMut, Bytes};
2+
use crate::{Buf, BufMut};
33

44
#[cfg(feature = "std")]
55
use std::io::IoSlice;
@@ -169,7 +169,7 @@ where
169169
n
170170
}
171171

172-
fn copy_to_bytes(&mut self, len: usize) -> Bytes {
172+
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
173173
let a_rem = self.a.remaining();
174174
if a_rem >= len {
175175
self.a.copy_to_bytes(len)

src/buf/take.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Buf, Bytes};
1+
use crate::Buf;
22

33
use core::cmp;
44

@@ -145,7 +145,7 @@ impl<T: Buf> Buf for Take<T> {
145145
self.limit -= cnt;
146146
}
147147

148-
fn copy_to_bytes(&mut self, len: usize) -> Bytes {
148+
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
149149
assert!(len <= self.remaining(), "`len` greater than remaining");
150150

151151
let r = self.inner.copy_to_bytes(len);

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
))]
77
#![no_std]
88
#![cfg_attr(docsrs, feature(doc_cfg))]
9+
#![cfg_attr(bytes_unstable, feature(cfg_target_has_atomic))]
910

1011
//! Provides abstractions for working with bytes.
1112
//!

src/loom.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#[cfg(not(all(test, loom)))]
22
pub(crate) mod sync {
33
pub(crate) mod atomic {
4+
#[cfg(not(feature = "extra-platforms"))]
45
pub(crate) use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
6+
#[cfg(feature = "extra-platforms")]
7+
pub(crate) use portable_atomic::{AtomicPtr, AtomicUsize, Ordering};
58

69
pub(crate) trait AtomicMut<T> {
710
fn with_mut<F, R>(&mut self, f: F) -> R

0 commit comments

Comments
 (0)