Skip to content

Commit 7136a1e

Browse files
taiki-ecramertj
authored andcommitted
Support oneshot in no_std environment
1 parent a705910 commit 7136a1e

File tree

6 files changed

+54
-23
lines changed

6 files changed

+54
-23
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ matrix:
9494
- cargo build --manifest-path futures/Cargo.toml --no-default-features --features alloc
9595
- cargo build --manifest-path futures-core/Cargo.toml --no-default-features --features alloc
9696
- cargo build --manifest-path futures-sink/Cargo.toml --no-default-features --features alloc
97+
- cargo build --manifest-path futures-channel/Cargo.toml --no-default-features --features alloc
9798
- cargo build --manifest-path futures-util/Cargo.toml --no-default-features --features alloc
9899

99100
- name: cargo build --target=thumbv6m-none-eabi

futures-channel/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ Channels for asynchronous communication using futures-rs.
1515
name = "futures_channel"
1616

1717
[features]
18-
std = ["futures-core-preview/std"]
19-
sink = ["futures-sink-preview"]
2018
default = ["std"]
19+
std = ["alloc", "futures-core-preview/std"]
20+
alloc = ["futures-core-preview/alloc"]
21+
sink = ["futures-sink-preview"]
22+
nightly = ["futures-core-preview/nightly"]
23+
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic"]
2124

2225
[dependencies]
2326
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.17", default-features = false }

futures-channel/src/lib.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
//! This crate provides channels that can be used to communicate between
44
//! asynchronous tasks.
55
//!
6-
//! All items of this library are only available when the `std` feature of this
6+
//! All items of this library are only available when the `std` or `alloc` feature of this
77
//! library is activated, and it is activated by default.
88
9+
#![cfg_attr(feature = "cfg-target-has-atomic", feature(cfg_target_has_atomic))]
10+
911
#![cfg_attr(not(feature = "std"), no_std)]
1012

1113
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
@@ -17,9 +19,27 @@
1719

1820
#![doc(html_root_url = "https://rust-lang-nursery.github.io/futures-api-docs/0.3.0-alpha.17/futures_channel")]
1921

20-
#[cfg(feature = "std")]
21-
mod lock;
22-
#[cfg(feature = "std")]
23-
pub mod mpsc;
24-
#[cfg(feature = "std")]
25-
pub mod oneshot;
22+
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "nightly")))]
23+
compile_error!("The `cfg-target-has-atomic` feature requires the `nightly` feature as an explicit opt-in to unstable features");
24+
25+
macro_rules! cfg_target_has_atomic {
26+
($($item:item)*) => {$(
27+
#[cfg_attr(
28+
feature = "cfg-target-has-atomic",
29+
cfg(all(target_has_atomic = "cas", target_has_atomic = "ptr"))
30+
)]
31+
$item
32+
)*};
33+
}
34+
35+
cfg_target_has_atomic! {
36+
#[cfg(feature = "alloc")]
37+
extern crate alloc;
38+
39+
#[cfg(feature = "alloc")]
40+
mod lock;
41+
#[cfg(feature = "std")]
42+
pub mod mpsc;
43+
#[cfg(feature = "alloc")]
44+
pub mod oneshot;
45+
}

futures-channel/src/oneshot.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
//! A channel for sending a single message between asynchronous tasks.
22
3+
use alloc::sync::Arc;
4+
use core::fmt;
5+
use core::pin::Pin;
6+
use core::sync::atomic::AtomicBool;
7+
use core::sync::atomic::Ordering::SeqCst;
38
use futures_core::future::Future;
49
use futures_core::task::{Context, Poll, Waker};
5-
use std::pin::Pin;
6-
use std::sync::Arc;
7-
use std::sync::atomic::AtomicBool;
8-
use std::sync::atomic::Ordering::SeqCst;
9-
use std::error::Error;
10-
use std::fmt;
1110

1211
use crate::lock::Lock;
1312

@@ -387,7 +386,8 @@ impl fmt::Display for Canceled {
387386
}
388387
}
389388

390-
impl Error for Canceled {}
389+
#[cfg(feature = "std")]
390+
impl std::error::Error for Canceled {}
391391

392392
impl<T> Receiver<T> {
393393
/// Gracefully close this receiver, preventing any subsequent attempts to

futures/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ tokio = "0.1.11"
3636
assert_matches = "1.3.0"
3737

3838
[features]
39-
nightly = ["futures-core-preview/nightly", "futures-util-preview/nightly"]
39+
default = ["std"]
4040
std = ["alloc", "futures-core-preview/std", "futures-executor-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-util-preview/std", "futures-util-preview/io", "futures-util-preview/channel"]
41+
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "futures-channel-preview/alloc", "futures-util-preview/alloc"]
42+
nightly = ["futures-core-preview/nightly", "futures-channel-preview/nightly", "futures-util-preview/nightly"]
4143
async-await = ["futures-util-preview/async-await", "futures-util-preview/select-macro"]
42-
default = ["std"]
4344
compat = ["std", "futures-util-preview/compat"]
4445
io-compat = ["compat", "futures-util-preview/io-compat"]
45-
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic", "futures-util-preview/cfg-target-has-atomic"]
46-
alloc = ["futures-core-preview/alloc", "futures-sink-preview/alloc", "futures-util-preview/alloc"]
46+
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic", "futures-channel-preview/cfg-target-has-atomic", "futures-util-preview/cfg-target-has-atomic"]
4747

4848
[package.metadata.docs.rs]
4949
all-features = true

futures/src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ pub use futures_util::{
7373
join, try_join, pending, poll,
7474
};
7575

76-
#[cfg(feature = "std")]
76+
#[cfg_attr(
77+
feature = "cfg-target-has-atomic",
78+
cfg(all(target_has_atomic = "cas", target_has_atomic = "ptr"))
79+
)]
80+
#[cfg(feature = "alloc")]
7781
pub mod channel {
7882
//! Cross-task communication.
7983
//!
@@ -86,10 +90,13 @@ pub mod channel {
8690
//! channel for sending values between tasks, analogous to the
8791
//! similarly-named structure in the standard library.
8892
//!
89-
//! This module is only available when the `std` feature of this
93+
//! This module is only available when the `std` or `alloc` feature of this
9094
//! library is activated, and it is activated by default.
9195
92-
pub use futures_channel::{oneshot, mpsc};
96+
pub use futures_channel::oneshot;
97+
98+
#[cfg(feature = "std")]
99+
pub use futures_channel::mpsc;
93100
}
94101

95102
#[cfg(feature = "compat")]

0 commit comments

Comments
 (0)