Skip to content

Commit

Permalink
fix: compilation errors with --no-default-features (#59)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
hawkw authored Apr 11, 2022
1 parent a4cc6f5 commit a2ab178
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 40 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}

Expand Down
76 changes: 39 additions & 37 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -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)*),
));
}
}
}
Expand All @@ -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
}
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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 change: 1 addition & 0 deletions src/mpsc/async_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/util/mutex/spin_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/util/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
}

0 comments on commit a2ab178

Please sign in to comment.