Skip to content

Commit

Permalink
Add an std feature
Browse files Browse the repository at this point in the history
This feature can be disabled to allow the crate to be used without
the standard library on embedded environments.

Closes #64

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Sep 16, 2023
1 parent 6d99af0 commit a8cd074
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,28 @@ jobs:
if: startsWith(matrix.rust, 'nightly')
run: cargo check -Z features=dev_dep
- run: cargo test
- run: cargo test --no-default-features
- name: Install cargo-hack
uses: taiki-e/install-action@cargo-hack
- run: rustup target add thumbv7m-none-eabi
- name: Run cargo check (without dev-dependencies to catch missing feature flags)
run: cargo hack build --all --no-dev-deps
- run: cargo hack build --all --target thumbv7m-none-eabi --no-default-features --no-dev-deps
- run: cargo hack build --target thumbv7m-none-eabi --no-default-features --no-dev-deps --features portable-atomic

msrv:
runs-on: ubuntu-latest
strategy:
matrix:
# When updating this, the reminder to update the minimum supported
# Rust version in Cargo.toml.
rust: ['1.45']
rust: ['1.59']
steps:
- uses: actions/checkout@v3
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: cargo build
- run: cargo build --no-default-features

clippy:
runs-on: ubuntu-latest
Expand Down
16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "async-channel"
version = "1.9.0"
authors = ["Stjepan Glavina <stjepang@gmail.com>"]
edition = "2018"
rust-version = "1.45"
rust-version = "1.59"
description = "Async multi-producer multi-consumer channel"
license = "Apache-2.0 OR MIT"
repository = "https://github.com/smol-rs/async-channel"
Expand All @@ -15,15 +15,19 @@ categories = ["asynchronous", "concurrency"]
exclude = ["/.*"]

[dependencies]
concurrent-queue = "2"
event-listener = "2.4.0"
event-listener-strategy = { git = "https://github.com/smol-rs/event-listener" }
futures-core = "0.3.5"
concurrent-queue = { version = "2", default-features = false }
event-listener = { version = "2.4.0", default-features = false }
event-listener-strategy = { git = "https://github.com/smol-rs/event-listener", default-features = false }
futures-core = { version = "0.3.5", default-features = false }
pin-project-lite = "0.2.11"

[dev-dependencies]
easy-parallel = "3"
futures-lite = "1"

[features]
default = ["std"]
std = ["concurrent-queue/std", "event-listener/std", "event-listener-strategy/std"]

[patch.crates-io]
event-listener = { git = "https://github.com/smol-rs/event-listener" }
event-listener = { git = "https://github.com/smol-rs/event-listener", default-features = false }
61 changes: 44 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//! # });
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
#![doc(
Expand All @@ -35,15 +36,16 @@
html_logo_url = "https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"
)]

use std::error;
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::process;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::usize;
extern crate alloc;

use core::fmt;
use core::future::Future;
use core::pin::Pin;
use core::sync::atomic::{AtomicUsize, Ordering};
use core::task::{Context, Poll};
use core::usize;

use alloc::sync::Arc;

use concurrent_queue::{ConcurrentQueue, PopError, PushError};
use event_listener::{Event, EventListener};
Expand Down Expand Up @@ -274,6 +276,7 @@ impl<T> Sender<T> {
/// drop(r);
/// assert_eq!(s.send_blocking(2), Err(SendError(2)));
/// ```
#[cfg(feature = "std")]
pub fn send_blocking(&self, msg: T) -> Result<(), SendError<T>> {
self.send(msg).wait()
}
Expand Down Expand Up @@ -465,7 +468,7 @@ impl<T> Clone for Sender<T> {

// Make sure the count never overflows, even if lots of sender clones are leaked.
if count > usize::MAX / 2 {
process::abort();
abort();
}

Sender {
Expand Down Expand Up @@ -596,6 +599,7 @@ impl<T> Receiver<T> {
/// assert_eq!(r.recv_blocking(), Ok(1));
/// assert_eq!(r.recv_blocking(), Err(RecvError));
/// ```
#[cfg(feature = "std")]
pub fn recv_blocking(&self) -> Result<T, RecvError> {
self.recv().wait()
}
Expand Down Expand Up @@ -778,7 +782,7 @@ impl<T> Clone for Receiver<T> {

// Make sure the count never overflows, even if lots of receiver clones are leaked.
if count > usize::MAX / 2 {
process::abort();
abort();
}

Receiver {
Expand Down Expand Up @@ -864,7 +868,7 @@ impl<T> WeakSender<T> {
Err(_) => None,
Ok(new_value) if new_value > usize::MAX / 2 => {
// Make sure the count never overflows, even if lots of sender clones are leaked.
process::abort();
abort();
}
Ok(_) => Some(Sender {
channel: self.channel.clone(),
Expand Down Expand Up @@ -910,7 +914,7 @@ impl<T> WeakReceiver<T> {
Err(_) => None,
Ok(new_value) if new_value > usize::MAX / 2 => {
// Make sure the count never overflows, even if lots of receiver clones are leaked.
process::abort();
abort();
}
Ok(_) => Some(Receiver {
channel: self.channel.clone(),
Expand Down Expand Up @@ -948,7 +952,8 @@ impl<T> SendError<T> {
}
}

impl<T> error::Error for SendError<T> {}
#[cfg(feature = "std")]
impl<T> std::error::Error for SendError<T> {}

impl<T> fmt::Debug for SendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -998,7 +1003,8 @@ impl<T> TrySendError<T> {
}
}

impl<T> error::Error for TrySendError<T> {}
#[cfg(feature = "std")]
impl<T> std::error::Error for TrySendError<T> {}

impl<T> fmt::Debug for TrySendError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -1024,7 +1030,8 @@ impl<T> fmt::Display for TrySendError<T> {
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct RecvError;

impl error::Error for RecvError {}
#[cfg(feature = "std")]
impl std::error::Error for RecvError {}

impl fmt::Display for RecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -1060,7 +1067,8 @@ impl TryRecvError {
}
}

impl error::Error for TryRecvError {}
#[cfg(feature = "std")]
impl std::error::Error for TryRecvError {}

impl fmt::Display for TryRecvError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -1076,6 +1084,7 @@ easy_wrapper! {
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Send<'a, T>(SendInner<'a, T> => Result<(), SendError<T>>);
#[cfg(feature = "std")]
pub(crate) wait();
}

Expand Down Expand Up @@ -1125,6 +1134,7 @@ easy_wrapper! {
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Recv<'a, T>(RecvInner<'a, T> => Result<T, RecvError>);
#[cfg(feature = "std")]
pub(crate) wait();
}

Expand Down Expand Up @@ -1166,3 +1176,20 @@ impl<'a, T> EventListenerFuture for RecvInner<'a, T> {
}
}
}

#[cfg(feature = "std")]
use std::process::abort;

#[cfg(not(feature = "std"))]
fn abort() -> ! {
struct PanicOnDrop;

impl Drop for PanicOnDrop {
fn drop(&mut self) {
panic!("Panic while panicking to abort");
}
}

let _bomb = PanicOnDrop;
panic!("Panic while panicking to abort")
}
2 changes: 2 additions & 0 deletions tests/bounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ fn smoke() {
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
}

#[cfg(feature = "std")]
#[test]
fn smoke_blocking() {
let (s, r) = bounded(1);
Expand Down Expand Up @@ -459,6 +460,7 @@ fn mpmc_stream() {
}
}

#[cfg(feature = "std")]
#[test]
fn weak() {
let (s, r) = bounded::<usize>(3);
Expand Down
2 changes: 2 additions & 0 deletions tests/unbounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn smoke() {
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
}

#[cfg(feature = "std")]
#[test]
fn smoke_blocking() {
let (s, r) = unbounded();
Expand Down Expand Up @@ -318,6 +319,7 @@ fn mpmc_stream() {
}
}

#[cfg(feature = "std")]
#[test]
fn weak() {
let (s, r) = unbounded::<usize>();
Expand Down

0 comments on commit a8cd074

Please sign in to comment.