Skip to content

Commit 45817aa

Browse files
committed
feat: add "alloc" feature
1 parent b7f2d15 commit 45817aa

File tree

11 files changed

+61
-43
lines changed

11 files changed

+61
-43
lines changed

Cargo.toml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@ description = "A buffering MPSC queue."
77
homepage = "https://github.com/wyfo/swap-buffer-queue"
88
readme = "README.md"
99
keywords = [
10-
"atomic",
11-
"lock-free",
12-
"no-std",
13-
"mpsc",
14-
"async",
10+
"atomic",
11+
"lock-free",
12+
"no-std",
13+
"mpsc",
14+
"async",
1515
]
1616
categories = [
17-
"concurrency",
18-
"data-structures",
19-
"no-std",
17+
"concurrency",
18+
"data-structures",
19+
"no-std",
2020
]
2121
license = "MIT"
2222
repository = "https://github.com/wyfo/swap-buffer-queue"
2323

2424
[features]
25-
default = ["stream"]
26-
std = []
27-
stream = ["std", "dep:futures"]
25+
default = ["std"]
26+
alloc = []
27+
std = ["alloc"]
28+
stream = ["dep:futures-core", "dep:futures-util"]
2829
write = []
2930

3031
[dependencies]
3132
crossbeam-utils = { version = "0.8", default-features = false }
32-
futures = { version = "0.3", optional = true }
33+
futures-core = { version = "0.3", default-features = false, optional = true }
34+
futures-util = { version = "0.3", default-features = false, optional = true }
3335

3436
[dev-dependencies]
3537
tokio-test = "0.4"
@@ -45,6 +47,6 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(loom)'] }
4547
[package.metadata.docs.rs]
4648
all-features = true
4749
rustdoc-args = [
48-
"--cfg",
49-
"docsrs",
50+
"--cfg",
51+
"docsrs",
5052
]

src/buffer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! [`Buffer`] definition and simple implementations.
22
3-
use std::{
3+
use core::{
44
fmt,
55
iter::FusedIterator,
66
marker::PhantomData,
@@ -12,12 +12,11 @@ use std::{
1212
use crate::queue::Queue;
1313

1414
mod array;
15-
#[cfg(feature = "std")]
16-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
15+
#[cfg(feature = "alloc")]
1716
mod vec;
1817

1918
pub use array::ArrayBuffer;
20-
#[cfg(feature = "std")]
19+
#[cfg(feature = "alloc")]
2120
pub use vec::VecBuffer;
2221

2322
/// [`Queue`] buffer. It is used together with [`InsertIntoBuffer`].
@@ -114,8 +113,9 @@ where
114113
// don't loop on iterator, because `ExactSizeIterator` is not a sufficient guarantee
115114
// for unsafe code
116115
for i in index..(index + self.0.len()) {
116+
const ERROR: &str = "iterator exhausted before reaching its exact size";
117117
// SAFETY: function contract encompass `CellBuffer::insert` one
118-
unsafe { buffer.insert(i, self.0.next().unwrap()) };
118+
unsafe { buffer.insert(i, self.0.next().expect(ERROR)) };
119119
}
120120
}
121121
}

src/buffer/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::Cell, mem::MaybeUninit, ops::Range};
1+
use core::{cell::Cell, mem::MaybeUninit, ops::Range};
22

33
use crate::{
44
buffer::{Buffer, CellBuffer, Drain},

src/buffer/vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::{cell::Cell, mem::MaybeUninit, ops::Range};
1+
use alloc::boxed::Box;
2+
use core::{cell::Cell, mem::MaybeUninit, ops::Range};
23

34
use crate::buffer::{Buffer, CellBuffer, Drain, Resize};
45

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Queue error types.
22
3-
use std::fmt;
3+
use core::fmt;
44

55
/// Error returned by [`Queue::try_enqueue`](crate::Queue::try_enqueue).
66
///

src/lib.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![forbid(missing_docs)]
44
#![forbid(unsafe_op_in_unsafe_fn)]
55
#![forbid(clippy::undocumented_unsafe_blocks)]
6-
#![cfg_attr(docsrs, feature(doc_cfg))]
6+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
77
#![cfg_attr(not(feature = "std"), no_std)]
88
//! # swap-buffer-queue
99
//! A buffering MPSC queue.
@@ -41,28 +41,23 @@
4141
//! assert_eq!(slice.into_iter().collect::<Vec<_>>(), vec![0, 1, 2, 3, 4]);
4242
//! ```
4343
44-
#[cfg(not(feature = "std"))]
45-
extern crate core as std;
44+
#[cfg(feature = "alloc")]
45+
extern crate alloc;
4646

4747
pub mod buffer;
4848
pub mod error;
4949
mod loom;
5050
pub mod notify;
5151
mod queue;
5252
#[cfg(feature = "std")]
53-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
5453
mod synchronized;
5554
mod utils;
5655
#[cfg(feature = "write")]
57-
#[cfg_attr(docsrs, doc(cfg(feature = "write")))]
5856
pub mod write;
5957
#[cfg(feature = "write")]
60-
#[cfg_attr(docsrs, doc(cfg(feature = "write")))]
6158
#[cfg(feature = "std")]
62-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
6359
pub mod write_vectored;
6460

6561
pub use queue::Queue;
6662
#[cfg(feature = "std")]
67-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
6863
pub use synchronized::{SynchronizedNotifier, SynchronizedQueue};

src/queue.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt, num::NonZeroUsize, ops::Range};
1+
use core::{fmt, num::NonZeroUsize, ops::Range};
22

33
use crossbeam_utils::CachePadded;
44

@@ -281,6 +281,7 @@ where
281281
/// queue.try_enqueue([0]).unwrap();
282282
/// assert_eq!(queue.len(), 1);
283283
/// ```
284+
#[inline]
284285
pub fn len(&self) -> usize {
285286
let enqueuing =
286287
EnqueuingCapacity::from_atomic(self.enqueuing_capacity.load(Ordering::Relaxed));
@@ -406,7 +407,7 @@ where
406407
// Spin in case of concurrent modifications, except when the buffer is full ofc.
407408
if enqueuing.remaining_capacity() != 0 {
408409
for _ in 0..1 << backoff {
409-
std::hint::spin_loop();
410+
hint::spin_loop();
410411
}
411412
if backoff < BACKOFF_LIMIT {
412413
backoff += 1;
@@ -467,7 +468,7 @@ where
467468
.with_mut(|buf| unsafe { (*buf).slice(range.clone()) });
468469
return Some(BufferSlice::new(self, buffer_index, range, slice));
469470
}
470-
std::hint::spin_loop();
471+
hint::spin_loop();
471472
}
472473
// If the enqueuing are still ongoing, just save the dequeuing state in order to retry.
473474
self.dequeuing_length.store(
@@ -564,7 +565,7 @@ where
564565
};
565566
if let Some(ref mut backoff) = backoff {
566567
for _ in 0..1 << *backoff {
567-
std::hint::spin_loop();
568+
hint::spin_loop();
568569
}
569570
if *backoff < BACKOFF_LIMIT {
570571
*backoff += 1;

src/synchronized.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
//! Synchronization primitives for [`Queue`].
2+
//!
3+
//! It supports both synchronous and asynchronous API. [`SynchronizedQueue`] is just an alias
4+
//! for a [`Queue`] using [`SynchronizedNotifier`].
5+
//!
6+
//! # Examples
7+
//! ```rust
8+
//! # use std::sync::Arc;
9+
//! # use swap_buffer_queue::SynchronizedQueue;
10+
//! # use swap_buffer_queue::buffer::VecBuffer;
11+
//! let queue: Arc<SynchronizedQueue<VecBuffer<usize>>> =
12+
//! Arc::new(SynchronizedQueue::with_capacity(1));
13+
//! let queue_clone = queue.clone();
14+
//! std::thread::spawn(move || {
15+
//! queue_clone.enqueue([0]).unwrap();
16+
//! queue_clone.enqueue([1]).unwrap();
17+
//! });
18+
//! assert_eq!(queue.dequeue().unwrap()[0], 0);
19+
//! assert_eq!(queue.dequeue().unwrap()[0], 1);
20+
//! ```
121
use std::{
222
fmt,
323
future::poll_fn,
@@ -380,12 +400,11 @@ where
380400
}
381401

382402
#[cfg(feature = "stream")]
383-
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
384403
/// Returns an stream over the element of the queue (see [`BufferIter`](crate::buffer::BufferIter)).
385404
///
386405
/// # Examples
387406
/// ```
388-
/// # use futures::StreamExt;
407+
/// # use futures_util::StreamExt;
389408
/// # use swap_buffer_queue::SynchronizedQueue;
390409
/// # use swap_buffer_queue::buffer::VecBuffer;
391410
/// # tokio_test::block_on(async {
@@ -402,8 +421,8 @@ where
402421
/// assert_eq!(stream.next().await, None);
403422
/// # })
404423
/// ```
405-
pub fn stream(&self) -> impl futures::Stream<Item = B::Value> + '_ {
406-
use futures::{stream, StreamExt};
424+
pub fn stream(&self) -> impl futures_core::Stream<Item = B::Value> + '_ {
425+
use futures_util::{stream, StreamExt};
407426
stream::repeat_with(|| stream::once(self.dequeue_async()))
408427
.flatten()
409428
.take_while(|res| {

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{
1+
use core::{
22
mem,
33
mem::MaybeUninit,
44
ops::{Deref, DerefMut},

src/write.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929
use std::ops::{Deref, DerefMut};
3030

3131
mod array;
32-
#[cfg(feature = "std")]
33-
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
32+
#[cfg(feature = "alloc")]
3433
mod vec;
3534

3635
pub use array::WriteArrayBuffer;
37-
#[cfg(feature = "std")]
36+
#[cfg(feature = "alloc")]
3837
pub use vec::WriteVecBuffer;
3938

4039
/// A bytes slice with a `HEADER_SIZE`-bytes header and a `TRAILER_SIZE`-bytes trailer.

src/write/vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::{
1+
use alloc::boxed::Box;
2+
use core::{
23
cell::{Cell, UnsafeCell},
34
ops::Range,
45
};

0 commit comments

Comments
 (0)