Skip to content

Commit

Permalink
Check that target supports atomic pointer operations before defining …
Browse files Browse the repository at this point in the history
…max size for Arc (#168)

* Check that target supports atomic pointer operations before defining max size for Arc.

* Add test case

---------

Co-authored-by: James Munns <james@onevariable.com>
  • Loading branch information
sjudson and jamesmunns authored Aug 21, 2024
1 parent 1cb210f commit bb2be5b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ensure_target() {
}

ensure_target thumbv7em-none-eabi
ensure_target riscv32i-unknown-none-elf

has_toolchain() {
rustup toolchain list | grep -q "$1"
Expand All @@ -33,6 +34,10 @@ cargo_test --features=alloc,experimental-derive,use-std,use-crc
cargo_check --target=thumbv7em-none-eabi --no-default-features
cargo_check --target=thumbv7em-none-eabi --features=alloc,experimental-derive

# CC https://github.com/jamesmunns/postcard/issues/167 - don't accidentally use atomics
# on non-atomic systems
cargo_check --target=riscv32i-unknown-none-elf --features=alloc,experimental-derive

cargo fmt --all -- --check

# Check docs.rs build
Expand Down
12 changes: 9 additions & 3 deletions source/postcard/src/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::{boxed::Box, rc::Rc, sync::Arc};
use alloc::{boxed::Box, rc::Rc};

#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
use alloc::sync::Arc;

use crate::varint::varint_max;
use core::{
Expand Down Expand Up @@ -204,8 +207,8 @@ impl<T: MaxSize> MaxSize for Box<T> {
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", target_has_atomic = "ptr"))))]
impl<T: MaxSize> MaxSize for Arc<T> {
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
}
Expand Down Expand Up @@ -264,6 +267,8 @@ mod tests {

use super::*;
use alloc::rc::Rc;

#[cfg(target_has_atomic = "ptr")]
use alloc::sync::Arc;

#[test]
Expand All @@ -274,6 +279,7 @@ mod tests {
}

#[test]
#[cfg(target_has_atomic = "ptr")]
fn arc_max_size() {
assert_eq!(Arc::<u8>::POSTCARD_MAX_SIZE, 1);
assert_eq!(Arc::<u32>::POSTCARD_MAX_SIZE, 5);
Expand Down

0 comments on commit bb2be5b

Please sign in to comment.