Skip to content

Commit

Permalink
Expose BiLock as unstable API
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e authored and cramertj committed Aug 28, 2019
1 parent a5f3870 commit e9cd539
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 14 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Expand Up @@ -75,7 +75,7 @@ matrix:
rust: nightly
script:
- cargo bench --all
- cargo bench --manifest-path futures-util/Cargo.toml --features=bench,unstable
- cargo bench --manifest-path futures-util/Cargo.toml --features=bilock,unstable

- name: cargo +stable build --no-default-features
rust: stable
Expand Down Expand Up @@ -146,11 +146,18 @@ matrix:
- cargo check --manifest-path futures-util/Cargo.toml --features io-compat
- cargo check --manifest-path futures-util/Cargo.toml --features sink,compat
- cargo check --manifest-path futures-util/Cargo.toml --features sink,channel
- cargo check --manifest-path futures-util/Cargo.toml --features bilock,unstable
- cargo check --manifest-path futures-util/Cargo.toml --features sink,bilock,unstable
- cargo check --manifest-path futures-util/Cargo.toml --features io,bilock,unstable
- cargo check --manifest-path futures-util/Cargo.toml --features sink,io

- cargo check --manifest-path futures-util/Cargo.toml --no-default-features
- cargo check --manifest-path futures-util/Cargo.toml --no-default-features --features sink
- cargo check --manifest-path futures-util/Cargo.toml --no-default-features --features alloc,sink
- cargo check --manifest-path futures-util/Cargo.toml --no-default-features --features async-await
- cargo check --manifest-path futures-util/Cargo.toml --no-default-features --features bilock,unstable
- cargo check --manifest-path futures-util/Cargo.toml --no-default-features --features sink,bilock,unstable
- cargo check --manifest-path futures-util/Cargo.toml --no-default-features --features io,bilock,unstable

- name: cargo doc
rust: nightly
Expand Down
2 changes: 1 addition & 1 deletion futures-util/Cargo.toml
Expand Up @@ -32,7 +32,7 @@ select-macro = ["async-await", "futures-select-macro-preview", "proc-macro-hack"
# `unstable` feature as an explicit opt-in to unstable API.
unstable = ["futures-core-preview/unstable"]
cfg-target-has-atomic = ["futures-core-preview/cfg-target-has-atomic"]
bench = []
bilock = []

[dependencies]
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.18", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion futures-util/benches_disabled/bilock.rs
@@ -1,6 +1,6 @@
#![feature(test)]

#[cfg(feature = "bench")]
#[cfg(feature = "bilock")]
mod bench {
use futures::task::{Context, Waker};
use futures::executor::LocalPool;
Expand Down
4 changes: 2 additions & 2 deletions futures-util/src/lib.rs
Expand Up @@ -16,8 +16,8 @@
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "unstable")))]
compile_error!("The `cfg-target-has-atomic` feature requires the `unstable` feature as an explicit opt-in to unstable features");

#[cfg(all(feature = "bench", not(feature = "unstable")))]
compile_error!("The `bench` feature requires the `unstable` feature as an explicit opt-in to unstable features");
#[cfg(all(feature = "bilock", not(feature = "unstable")))]
compile_error!("The `bilock` feature requires the `unstable` feature as an explicit opt-in to unstable features");

#[cfg(feature = "alloc")]
extern crate alloc;
Expand Down
12 changes: 10 additions & 2 deletions futures-util/src/lock/bilock.rs
@@ -1,11 +1,10 @@
//! Futures-powered synchronization primitives.
#![allow(unused)]

#[cfg(feature = "bilock")]
use futures_core::future::Future;
use futures_core::task::{Context, Poll, Waker};
use core::cell::UnsafeCell;
use core::fmt;
use core::mem;
use core::ops::{Deref, DerefMut};
use core::pin::Pin;
use core::sync::atomic::AtomicUsize;
Expand Down Expand Up @@ -35,6 +34,9 @@ use std::error::Error;
/// example a TCP stream could be both a reader and a writer or a framing layer
/// could be both a stream and a sink for messages. A `BiLock` enables splitting
/// these two and then using each independently in a futures-powered fashion.
///
/// This type is only available when the `bilock` feature of this
/// library is activated.
#[derive(Debug)]
pub struct BiLock<T> {
arc: Arc<Inner<T>>,
Expand Down Expand Up @@ -140,6 +142,7 @@ impl<T> BiLock<T> {
/// `BiLockGuard<T>`.
///
/// Note that the returned future will never resolve to an error.
#[cfg(feature = "bilock")]
pub fn lock(&self) -> BiLockAcquire<'_, T> {
BiLockAcquire {
bilock: self,
Expand All @@ -149,6 +152,7 @@ impl<T> BiLock<T> {
/// Attempts to put the two "halves" of a `BiLock<T>` back together and
/// recover the original value. Succeeds only if the two `BiLock<T>`s
/// originated from the same call to `BiLock::new`.
#[cfg(any(feature = "bilock", feature = "sink"))]
pub fn reunite(self, other: Self) -> Result<T, ReuniteError<T>>
where
T: Unpin,
Expand Down Expand Up @@ -182,6 +186,7 @@ impl<T> BiLock<T> {
}
}

#[cfg(any(feature = "bilock", feature = "sink"))]
impl<T: Unpin> Inner<T> {
unsafe fn into_value(mut self) -> T {
self.value.take().unwrap().into_inner()
Expand Down Expand Up @@ -255,15 +260,18 @@ impl<T> Drop for BiLockGuard<'_, T> {

/// Future returned by `BiLock::lock` which will resolve when the lock is
/// acquired.
#[cfg(feature = "bilock")]
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[derive(Debug)]
pub struct BiLockAcquire<'a, T> {
bilock: &'a BiLock<T>,
}

// Pinning is never projected to fields
#[cfg(feature = "bilock")]
impl<T> Unpin for BiLockAcquire<'_, T> {}

#[cfg(feature = "bilock")]
impl<'a, T> Future for BiLockAcquire<'a, T> {
type Output = BiLockGuard<'a, T>;

Expand Down
12 changes: 7 additions & 5 deletions futures-util/src/lock/mod.rs
@@ -1,16 +1,18 @@
//! Futures-powered synchronization primitives.
//!
//! This module is only available when the `std` or `alloc` feature of this
//! library is activated, and it is activated by default.

#[cfg(feature = "std")]
mod mutex;
#[cfg(feature = "std")]
pub use self::mutex::{Mutex, MutexLockFuture, MutexGuard};

#[cfg(any(feature = "sink", feature = "io"))]
#[allow(unreachable_pub)]
#[cfg(any(feature = "bilock", feature = "sink", feature = "io"))]
#[cfg_attr(not(feature = "bilock"), allow(unreachable_pub))]
mod bilock;
#[cfg(any(feature = "sink", feature = "io"))]
#[cfg(any(test, feature = "bench"))]
#[cfg(feature = "bilock")]
pub use self::bilock::{BiLock, BiLockAcquire, BiLockGuard, ReuniteError};
#[cfg(any(feature = "sink", feature = "io"))]
#[cfg(not(any(test, feature = "bench")))]
#[cfg(not(feature = "bilock"))]
pub(crate) use self::bilock::BiLock;
1 change: 1 addition & 0 deletions futures/Cargo.toml
Expand Up @@ -48,6 +48,7 @@ io-compat = ["compat", "futures-util-preview/io-compat"]
# `unstable` feature as an explicit opt-in to unstable API.
unstable = ["futures-core-preview/unstable", "futures-channel-preview/unstable", "futures-util-preview/unstable"]
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"]
bilock = ["futures-util-preview/bilock"]

[package.metadata.docs.rs]
all-features = true
15 changes: 13 additions & 2 deletions futures/src/lib.rs
Expand Up @@ -37,6 +37,9 @@
#[cfg(all(feature = "cfg-target-has-atomic", not(feature = "unstable")))]
compile_error!("The `cfg-target-has-atomic` feature requires the `unstable` feature as an explicit opt-in to unstable features");

#[cfg(all(feature = "bilock", not(feature = "unstable")))]
compile_error!("The `bilock` feature requires the `unstable` feature as an explicit opt-in to unstable features");

#[doc(hidden)] pub use futures_core::core_reexport;

#[doc(hidden)] pub use futures_core::future::Future;
Expand Down Expand Up @@ -307,13 +310,21 @@ pub mod io {
};
}

#[cfg(feature = "std")]
#[cfg_attr(
feature = "cfg-target-has-atomic",
cfg(all(target_has_atomic = "cas", target_has_atomic = "ptr"))
)]
#[cfg(feature = "alloc")]
pub mod lock {
//! Futures-powered synchronization primitives.
//!
//! This module is only available when the `std` feature of this
//! This module is only available when the `std` or `alloc` feature of this
//! library is activated, and it is activated by default.

#[cfg(feature = "bilock")]
pub use futures_util::lock::{BiLock, BiLockAcquire, BiLockGuard, ReuniteError};

#[cfg(feature = "std")]
pub use futures_util::lock::{Mutex, MutexLockFuture, MutexGuard};
}

Expand Down
File renamed without changes.

0 comments on commit e9cd539

Please sign in to comment.