Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cfg(target_has_atomic) on no-std targets to support platforms without atomic CAS (no dep) #761

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ 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@v4
- name: Install Rust
run: rustup update stable
- name: Install cargo-hack
uses: taiki-e/install-action@cargo-hack
# thumbv6m-none-eabi supports atomic, but not atomic CAS.
# thumbv7m-none-eabi supports atomic CAS.
- run: rustup target add thumbv6m-none-eabi thumbv7m-none-eabi
# * --optional-deps is needed for serde feature
# * --no-dev-deps is needed to avoid https://github.com/rust-lang/cargo/issues/4866
# Note that Bytes and BytesMut are provided only on platforms that supports atomic CAS.
- run: cargo hack build --target thumbv6m-none-eabi --target thumbv7m-none-eabi --feature-powerset --skip std,default --optional-deps --no-dev-deps

# Sanitizers
tsan:
name: tsan
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ Next, add this to your crate:
use bytes::{Bytes, BytesMut, Buf, BufMut};
```

## no_std support

To use `bytes` with no_std environment, disable the (enabled by default) `std` feature.

```toml
[dependencies]
bytes = { version = "1", default-features = false }
```

On no_std environment without atomic CAS, such as thumbv6m, `Bytes` and `BytesMut` are currently not provided.
See [#467](https://github.com/tokio-rs/bytes/pull/467) for the proposal about support these types on such a environment.

## Serde support

Serde support is optional and disabled by default. To enable use the feature `serde`.
Expand Down
2 changes: 2 additions & 0 deletions src/buf/buf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,7 @@ pub trait Buf {
/// # Panics
///
/// This function panics if `len > self.remaining()`.
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
use super::BufMut;

Expand Down Expand Up @@ -2872,6 +2873,7 @@ macro_rules! deref_forward_buf {
}

#[inline]
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
(**self).copy_to_bytes(len)
}
Expand Down
5 changes: 3 additions & 2 deletions src/buf/chain.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -169,7 +169,8 @@ where
n
}

fn copy_to_bytes(&mut self, len: usize) -> Bytes {
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
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)
Expand Down
5 changes: 3 additions & 2 deletions src/buf/take.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Buf, Bytes};
use crate::Buf;

use core::cmp;

Expand Down Expand Up @@ -148,7 +148,8 @@ impl<T: Buf> Buf for Take<T> {
self.limit -= cnt;
}

fn copy_to_bytes(&mut self, len: usize) -> Bytes {
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
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);
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,25 @@ extern crate std;
pub mod buf;
pub use crate::buf::{Buf, BufMut};

#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
mod bytes;
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
mod bytes_mut;
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
mod fmt;
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
mod loom;
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
pub use crate::bytes::Bytes;
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
pub use crate::bytes_mut::BytesMut;

// Optional Serde support
#[cfg(feature = "serde")]
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
mod serde;

#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
#[inline(never)]
#[cold]
fn abort() -> ! {
Expand Down Expand Up @@ -194,6 +202,7 @@ fn panic_does_not_fit(size: usize, nbytes: usize) -> ! {
///
/// But due to min rust is 1.39 and it is only stabilized
/// in 1.47, we cannot use it.
#[cfg_attr(target_os = "none", cfg(target_has_atomic = "ptr"))]
#[inline]
fn offset_from(dst: *const u8, original: *const u8) -> usize {
dst as usize - original as usize
Expand Down
Loading