Browse Source

fix: compilation errors with `--no-default-features` (#59)

This fixes compilation for no-std targets. My bad --- I was checking
this manually and never added a CI job for it.

This branch adds a CI check that the crate compiles with
`--no-default-features`, and fixes a number of issues that broke the
no-std build:

* f8d9527 fix(no_std): fix warning in `resume_unwind`
* cbf943a fix(no_std): fix compilation error in `unreachable_unchecked`
* 8ae178a fix(no_std): remove use of `thread::panicking`
* dab0d49 fix(alloc): fix missing `Box` imports
* 208defc fix(no_std): wrong feature gate for println macros

Fixes #58

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Eliza Weisman 3 years ago
parent
commit
a2ab17880e
6 changed files with 63 additions and 40 deletions
  1. 19 0
      .github/workflows/tests.yml
  2. 2 2
      src/lib.rs
  3. 39 37
      src/macros.rs
  4. 1 0
      src/mpsc/async_impl.rs
  5. 1 0
      src/util/mutex/spin_impl.rs
  6. 1 1
      src/util/panic.rs

+ 19 - 0
.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

+ 2 - 2
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<T: Send> Sync for Ref<'_, T> {}
 
 impl<T> Slot<T> {
     #[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()
     }
 

+ 39 - 37
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 ".")
     };
 }

+ 1 - 0
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.

+ 1 - 0
src/util/mutex/spin_impl.rs

@@ -28,6 +28,7 @@ pub(crate) const fn const_mutex<T>(data: T) -> Mutex<T> {
 }
 
 impl<T> Mutex<T> {
+    #[cfg(loom)]
     pub(crate) fn new(data: T) -> Self {
         Self {
             locked: AtomicBool::new(false),

+ 1 - 1
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!")
     }
 }