Skip to content

Commit

Permalink
Decouple alloc from std (#52)
Browse files Browse the repository at this point in the history
* Decouple alloc from std
  • Loading branch information
chemicstry committed Nov 20, 2020
1 parent 8783a08 commit 2b000ec
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -14,5 +14,5 @@ before_script:
script:
- cargo fmt --all -- --check
- cargo test --no-default-features
- cargo test --all-targets --no-default-features --features alloc
- cargo test --no-default-features --features alloc
- cargo test --all-targets --all-features
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -14,8 +14,8 @@ Futures based on intrusive data structures - for std and no-std environments.
name = "futures_intrusive"

[features]
alloc = ["futures-core/alloc", "parking_lot"]
std = ["alloc"]
alloc = ["futures-core/alloc"]
std = ["alloc", "parking_lot"]
default = ["std"]

[dependencies]
Expand Down
8 changes: 4 additions & 4 deletions src/channel/mod.rs
Expand Up @@ -19,7 +19,7 @@ mod oneshot;

pub use self::oneshot::{GenericOneshotChannel, LocalOneshotChannel};

#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
pub use self::oneshot::OneshotChannel;

mod oneshot_broadcast;
Expand All @@ -28,7 +28,7 @@ pub use self::oneshot_broadcast::{
GenericOneshotBroadcastChannel, LocalOneshotBroadcastChannel,
};

#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
pub use self::oneshot_broadcast::OneshotBroadcastChannel;

mod state_broadcast;
Expand All @@ -37,7 +37,7 @@ pub use state_broadcast::{
StateReceiveFuture,
};

#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
pub use self::state_broadcast::StateBroadcastChannel;

mod mpmc;
Expand All @@ -46,7 +46,7 @@ pub use self::mpmc::{
ChannelStream, GenericChannel, LocalChannel, LocalUnbufferedChannel,
};

#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
pub use self::mpmc::{Channel, UnbufferedChannel};

#[cfg(feature = "alloc")]
Expand Down
31 changes: 19 additions & 12 deletions src/channel/mpmc.rs
Expand Up @@ -9,7 +9,6 @@ use crate::{
use core::{marker::PhantomData, pin::Pin};
use futures_core::{
future::Future,
ready,
stream::{FusedStream, Stream},
task::{Context, Poll, Waker},
};
Expand Down Expand Up @@ -649,8 +648,8 @@ pub type LocalChannel<T, A> = GenericChannel<NoopLock, T, ArrayBuf<T, A>>;
/// An unbuffered [`GenericChannel`] implementation which is not thread-safe.
pub type LocalUnbufferedChannel<T> = LocalChannel<T, [T; 0]>;

#[cfg(feature = "alloc")]
mod if_alloc {
#[cfg(feature = "std")]
mod if_std {
use super::*;

// Export a thread-safe version using parking_lot::RawMutex
Expand All @@ -670,6 +669,14 @@ mod if_alloc {

/// An unbuffered [`GenericChannel`] implementation backed by [`parking_lot`].
pub type UnbufferedChannel<T> = Channel<T, [T; 0]>;
}

#[cfg(feature = "std")]
pub use self::if_std::*;

#[cfg(feature = "alloc")]
mod if_alloc {
use super::*;

/// Channel implementations where Sender and Receiver sides are cloneable
/// and owned.
Expand Down Expand Up @@ -871,11 +878,6 @@ mod if_alloc {
/// itself will be closed.
///
/// The channel can buffer up to `capacity` items internally.
///
/// ```
/// # use futures_intrusive::channel::shared::channel;
/// let (sender, receiver) = channel::<i32>(4);
/// ```
pub fn generic_channel<MutexType, T, A>(
capacity: usize,
) -> (
Expand Down Expand Up @@ -1069,8 +1071,8 @@ mod if_alloc {
}

// Export parking_lot based shared channels in std mode
#[cfg(feature = "alloc")]
mod if_alloc {
#[cfg(feature = "std")]
mod if_std {
use super::*;

use crate::buffer::GrowingHeapBuf;
Expand Down Expand Up @@ -1098,6 +1100,11 @@ mod if_alloc {
/// the given limit. Refer to [`generic_channel`] and [`GrowingHeapBuf`] for more information.
///
/// [`GrowingHeapBuf`]: ../../buffer/struct.GrowingHeapBuf.html
///
/// ```
/// # use futures_intrusive::channel::shared::channel;
/// let (sender, receiver) = channel::<i32>(4);
/// ```
pub fn channel<T>(capacity: usize) -> (Sender<T>, Receiver<T>)
where
T: Send,
Expand Down Expand Up @@ -1126,8 +1133,8 @@ mod if_alloc {
}
}

#[cfg(feature = "alloc")]
pub use self::if_alloc::*;
#[cfg(feature = "std")]
pub use self::if_std::*;
}
}

Expand Down
37 changes: 22 additions & 15 deletions src/channel/oneshot.rs
Expand Up @@ -232,15 +232,22 @@ impl<MutexType: RawMutex, T> ChannelReceiveAccess<T>
/// A [`GenericOneshotChannel`] which is not thread-safe.
pub type LocalOneshotChannel<T> = GenericOneshotChannel<NoopLock, T>;

#[cfg(feature = "alloc")]
mod if_alloc {
#[cfg(feature = "std")]
mod if_std {
use super::*;

// Export a thread-safe version using parking_lot::RawMutex

/// A [`GenericOneshotChannel`] implementation backed by [`parking_lot`].
pub type OneshotChannel<T> =
GenericOneshotChannel<parking_lot::RawMutex, T>;
}

#[cfg(feature = "std")]
pub use self::if_std::*;

#[cfg(feature = "alloc")]
mod if_alloc {
use super::*;

pub mod shared {
use super::*;
Expand Down Expand Up @@ -353,13 +360,6 @@ mod if_alloc {
///
/// As soon es either the senders or receivers is closed, the channel
/// itself will be closed.
///
/// Example for creating a channel to transmit an integer value:
///
/// ```
/// # use futures_intrusive::channel::shared::oneshot_channel;
/// let (sender, receiver) = oneshot_channel::<i32>();
/// ```
pub fn generic_oneshot_channel<MutexType, T>() -> (
GenericOneshotSender<MutexType, T>,
GenericOneshotReceiver<MutexType, T>,
Expand Down Expand Up @@ -411,9 +411,9 @@ mod if_alloc {
}
}

// Export parking_lot based shared channels in alloc mode
#[cfg(feature = "alloc")]
mod if_alloc {
// Export parking_lot based shared channels in std mode
#[cfg(feature = "std")]
mod if_std {
use super::*;

/// A [`GenericOneshotSender`] implementation backed by [`parking_lot`].
Expand All @@ -426,6 +426,13 @@ mod if_alloc {
/// Creates a new oneshot channel.
///
/// Refer to [`generic_oneshot_channel`] for details.
///
/// Example for creating a channel to transmit an integer value:
///
/// ```
/// # use futures_intrusive::channel::shared::oneshot_channel;
/// let (sender, receiver) = oneshot_channel::<i32>();
/// ```
pub fn oneshot_channel<T>() -> (OneshotSender<T>, OneshotReceiver<T>)
where
T: Send,
Expand All @@ -434,8 +441,8 @@ mod if_alloc {
}
}

#[cfg(feature = "alloc")]
pub use self::if_alloc::*;
#[cfg(feature = "std")]
pub use self::if_std::*;
}
}

Expand Down
36 changes: 22 additions & 14 deletions src/channel/oneshot_broadcast.rs
Expand Up @@ -242,15 +242,23 @@ where
pub type LocalOneshotBroadcastChannel<T> =
GenericOneshotBroadcastChannel<NoopLock, T>;

#[cfg(feature = "alloc")]
mod if_alloc {
#[cfg(feature = "std")]
mod if_std {
use super::*;

// Export a thread-safe version using parking_lot::RawMutex

/// A [`GenericOneshotBroadcastChannel`] implementation backed by [`parking_lot`].
pub type OneshotBroadcastChannel<T> =
GenericOneshotBroadcastChannel<parking_lot::RawMutex, T>;
}

#[cfg(feature = "std")]
pub use self::if_std::*;

#[cfg(feature = "alloc")]
mod if_alloc {
use super::*;

pub mod shared {
use super::*;
Expand Down Expand Up @@ -386,13 +394,6 @@ mod if_alloc {
///
/// As soon es either the senders or all receivers is closed, the channel
/// itself will be closed.
///
/// Example for creating a channel to transmit an integer value:
///
/// ```
/// # use futures_intrusive::channel::shared::oneshot_broadcast_channel;
/// let (sender, receiver) = oneshot_broadcast_channel::<i32>();
/// ```
pub fn generic_oneshot_broadcast_channel<MutexType, T>() -> (
GenericOneshotBroadcastSender<MutexType, T>,
GenericOneshotBroadcastReceiver<MutexType, T>,
Expand Down Expand Up @@ -446,9 +447,9 @@ mod if_alloc {
}
}

// Export parking_lot based shared channels in alloc mode
#[cfg(feature = "alloc")]
mod if_alloc {
// Export parking_lot based shared channels in std mode
#[cfg(feature = "std")]
mod if_std {
use super::*;

/// A [`GenericOneshotBroadcastSender`] implementation backed by [`parking_lot`].
Expand All @@ -461,6 +462,13 @@ mod if_alloc {
/// Creates a new oneshot broadcast channel.
///
/// Refer to [`generic_oneshot_broadcast_channel`] for details.
///
/// Example for creating a channel to transmit an integer value:
///
/// ```
/// # use futures_intrusive::channel::shared::oneshot_broadcast_channel;
/// let (sender, receiver) = oneshot_broadcast_channel::<i32>();
/// ```
pub fn oneshot_broadcast_channel<T>(
) -> (OneshotBroadcastSender<T>, OneshotBroadcastReceiver<T>)
where
Expand All @@ -470,8 +478,8 @@ mod if_alloc {
}
}

#[cfg(feature = "alloc")]
pub use self::if_alloc::*;
#[cfg(feature = "std")]
pub use self::if_std::*;
}
}

Expand Down
36 changes: 22 additions & 14 deletions src/channel/state_broadcast.rs
Expand Up @@ -428,15 +428,23 @@ impl<MutexType: RawMutex, T: Clone> ChannelReceiveAccess<T>
pub type LocalStateBroadcastChannel<T> =
GenericStateBroadcastChannel<NoopLock, T>;

#[cfg(feature = "alloc")]
mod if_alloc {
#[cfg(feature = "std")]
mod if_std {
use super::*;

// Export a thread-safe version using parking_lot::RawMutex

/// A [`GenericStateBroadcastChannel`] implementation backed by [`parking_lot`].
pub type StateBroadcastChannel<T> =
GenericStateBroadcastChannel<parking_lot::RawMutex, T>;
}

#[cfg(feature = "std")]
pub use self::if_std::*;

#[cfg(feature = "alloc")]
mod if_alloc {
use super::*;

pub mod shared {
use super::*;
Expand Down Expand Up @@ -689,13 +697,6 @@ mod if_alloc {
///
/// As soon es either the senders or receivers is closed, the channel
/// itself will be closed.
///
/// Example for creating a channel to transmit an integer value:
///
/// ```
/// # use futures_intrusive::channel::shared::state_broadcast_channel;
/// let (sender, receiver) = state_broadcast_channel::<i32>();
/// ```
pub fn generic_state_broadcast_channel<MutexType, T>() -> (
GenericStateSender<MutexType, T>,
GenericStateReceiver<MutexType, T>,
Expand Down Expand Up @@ -772,9 +773,9 @@ mod if_alloc {
}
}

// Export parking_lot based shared channels in alloc mode
#[cfg(feature = "alloc")]
mod if_alloc {
// Export parking_lot based shared channels in std mode
#[cfg(feature = "std")]
mod if_std {
use super::*;

/// A [`GenericStateSender`] implementation backed by [`parking_lot`].
Expand All @@ -787,6 +788,13 @@ mod if_alloc {
/// Creates a new state broadcast channel.
///
/// Refer to [`generic_state_broadcast_channel`] for details.
///
/// Example for creating a channel to transmit an integer value:
///
/// ```
/// # use futures_intrusive::channel::shared::state_broadcast_channel;
/// let (sender, receiver) = state_broadcast_channel::<i32>();
/// ```
pub fn state_broadcast_channel<T>(
) -> (StateSender<T>, StateReceiver<T>)
where
Expand All @@ -796,8 +804,8 @@ mod if_alloc {
}
}

#[cfg(feature = "alloc")]
pub use self::if_alloc::*;
#[cfg(feature = "std")]
pub use self::if_std::*;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/sync/manual_reset_event.rs
Expand Up @@ -300,8 +300,8 @@ pub type LocalManualResetEvent = GenericManualResetEvent<NoopLock>;
/// A [`GenericWaitForEventFuture`] for [`LocalManualResetEvent`].
pub type LocalWaitForEventFuture<'a> = GenericWaitForEventFuture<'a, NoopLock>;

#[cfg(feature = "alloc")]
mod if_alloc {
#[cfg(feature = "std")]
mod if_std {
use super::*;

// Export a thread-safe version using parking_lot::RawMutex
Expand All @@ -313,5 +313,5 @@ mod if_alloc {
GenericWaitForEventFuture<'a, parking_lot::RawMutex>;
}

#[cfg(feature = "alloc")]
pub use self::if_alloc::*;
#[cfg(feature = "std")]
pub use self::if_std::*;

0 comments on commit 2b000ec

Please sign in to comment.