diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 25cc048..fb3d153 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -20,6 +20,25 @@ env: name: Tests jobs: + build_no_std: + name: Check no_std + strategy: + matrix: + feature: [alloc, static] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rustfmt + - uses: actions-rs/cargo@v1 + with: + command: check + args: --no-default-features --features ${{ matrix.feature }} tests: name: Tests diff --git a/src/lib.rs b/src/lib.rs index 8a0be41..4163c12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,7 +167,7 @@ impl Core { fn close(&self) -> bool { test_println!("Core::close"); - if std::thread::panicking() { + if crate::util::panic::panicking() { return false; } test_dbg!(self.tail.fetch_or(self.closed, SeqCst) & self.closed == 0) @@ -516,7 +516,7 @@ unsafe impl Sync for Ref<'_, T> {} impl Slot { #[cfg(feature = "alloc")] - pub(crate) fn make_boxed_array(capacity: usize) -> Box<[Self]> { + pub(crate) fn make_boxed_array(capacity: usize) -> alloc::boxed::Box<[Self]> { (0..capacity).map(|i| Slot::new(i)).collect() } diff --git a/src/macros.rs b/src/macros.rs index b9c6a6a..49ed9ca 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,19 +1,18 @@ macro_rules! test_println { ($($arg:tt)*) => { - if cfg!(test) || cfg!(all(thingbuf_trace, feature = "std")) { - if crate::util::panic::panicking() { - // getting the thread ID while panicking doesn't seem to play super nicely with loom's - // mock lazy_static... - println!("[PANIC {:>30}:{:<3}] {}", file!(), line!(), format_args!($($arg)*)) - } else { - crate::loom::traceln(format_args!( - "[{:?} {:>30}:{:<3}] {}", - crate::loom::thread::current().id(), - file!(), - line!(), - format_args!($($arg)*), - )); - } + #[cfg(all(feature = "std", any(thingbuf_trace, test)))] + if crate::util::panic::panicking() { + // getting the thread ID while panicking doesn't seem to play super nicely with loom's + // mock lazy_static... + println!("[PANIC {:>30}:{:<3}] {}", file!(), line!(), format_args!($($arg)*)) + } else { + crate::loom::traceln(format_args!( + "[{:?} {:>30}:{:<3}] {}", + crate::loom::thread::current().id(), + file!(), + line!(), + format_args!($($arg)*), + )); } } } @@ -22,7 +21,7 @@ macro_rules! test_dbg { ($e:expr) => { match $e { e => { - #[cfg(any(test, all(thingbuf_trace, feature = "std")))] + #[cfg(all(feature = "std", any(thingbuf_trace, test)))] test_println!("{} = {:?}", stringify!($e), &e); e } @@ -40,7 +39,7 @@ macro_rules! assert_dbg { }; (@$e:expr, $($msg:tt)+) => { { - #[cfg(any(test, all(thingbuf_trace, feature = "std")))] + #[cfg(all(feature = "std", any(thingbuf_trace, test)))] test_println!("ASSERT: {}{}", stringify!($e), format_args!($($msg)*)); assert!($e, $($msg)*); test_println!("-> ok"); @@ -58,7 +57,7 @@ macro_rules! assert_eq_dbg { }; (@ $a:expr, $b:expr, $($msg:tt)+) => { { - #[cfg(any(test, all(thingbuf_trace, feature = "std")))] + #[cfg(all(feature = "std", any(thingbuf_trace, test)))] test_println!("ASSERT: {} == {}{}", stringify!($a), stringify!($b), format_args!($($msg)*)); assert_eq!($a, $b, $($msg)*); test_println!("-> ok"); @@ -96,29 +95,32 @@ macro_rules! fmt_bits { #[allow(unused_macros)] macro_rules! unreachable_unchecked { - ($($arg:tt)+) => { - crate::unreachable_unchecked!(@inner , format_args!(": {}", format_args!($($arg)*))) + (@inner $msg:expr) => { + { + #[cfg(debug_assertions)] { + panic!( + "internal error: entered unreachable code{}\n\n\ + /!\\ EXTREMELY SERIOUS WARNING /!\\\n + This code should NEVER be entered; in release mode, this would \ + have been an `unreachable_unchecked` hint. The fact that this \ + occurred means something VERY bad is going on. \n\ + Please contact the `thingbuf` maintainers immediately. Sorry!", + $msg, + ); + } + #[cfg(not(debug_assertions))] + unsafe { + core::hint::unreachable_unchecked(); + } + + } }; - () => { - crate::unreachable_unchecked!(@inner ".") + ($($arg:tt)+) => { + unreachable_unchecked!(@inner format_args!(": {}", format_args!($($arg)*))) }; - (@inner $msg:expr) => { - #[cfg(debug_assertions)] { - panic!( - "internal error: entered unreachable code{}\n\n\ - /!\\ EXTREMELY SERIOUS WARNING /!\\\n - This code should NEVER be entered; in release mode, this would \ - have been an `unreachable_unchecked` hint. The fact that this \ - occurred means something VERY bad is going on. \n\ - Please contact the `thingbuf` maintainers immediately. Sorry!", - $msg, - ); - } - #[cfg(not(debug_assertions))] - unsafe { - core::hint::unreachable_unchecked(); - } + () => { + unreachable_unchecked!(@inner ".") }; } diff --git a/src/mpsc/async_impl.rs b/src/mpsc/async_impl.rs index 3b35ce8..13277d9 100644 --- a/src/mpsc/async_impl.rs +++ b/src/mpsc/async_impl.rs @@ -17,6 +17,7 @@ feature! { #![feature = "alloc"] use crate::loom::sync::Arc; + use alloc::boxed::Box; /// Returns a new asynchronous multi-producer, single consumer (MPSC) /// channel with the provided capacity. diff --git a/src/util/mutex/spin_impl.rs b/src/util/mutex/spin_impl.rs index d25e11c..69f5a04 100644 --- a/src/util/mutex/spin_impl.rs +++ b/src/util/mutex/spin_impl.rs @@ -28,6 +28,7 @@ pub(crate) const fn const_mutex(data: T) -> Mutex { } impl Mutex { + #[cfg(loom)] pub(crate) fn new(data: T) -> Self { Self { locked: AtomicBool::new(false), diff --git a/src/util/panic.rs b/src/util/panic.rs index dafa2f3..0a51a08 100644 --- a/src/util/panic.rs +++ b/src/util/panic.rs @@ -21,7 +21,7 @@ mod inner { Ok(f()) } - pub(crate) fn resume_unwind(payload: ()) -> ! { + pub(crate) fn resume_unwind(_: ()) -> ! { unreachable_unchecked!("code compiled with no_std cannot unwind!") } }