1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Single-Sender, Single-Receiver (SPSC) channels.
//!
//! These channels are optimized for the case where there is only one sender (producer)
//! and only one receiver (consumer). They offer the highest throughput and lowest
//! latency for this specific 1-to-1 communication pattern.
//!
//! Both synchronous and asynchronous bounded SPSC channels are provided.
//! They share an underlying core implementation (`SpscShared`) ensuring
//! consistent behavior and allowing for conversion between synchronous and
//! asynchronous channel ends if needed (though direct conversion methods like
//! `to_async` are typically added to the producer/consumer structs themselves for
//! public API convenience, which is done in `bounded_sync.rs` and `bounded_async.rs`).
//!
//! # Features
//! - **Bounded**: Channels have a fixed capacity set at creation.
//! - **Blocking/Async**: Both blocking synchronous operations and non-blocking
//! asynchronous (Future-based) operations are supported.
//! - **Drop Safety**: Dropping either the producer or consumer will correctly
//! signal the other end (e.g., consumer receives `Disconnected`, producer's
//! send attempts result in `Closed`).
//! - **Cache-Padding**: Internal head/tail pointers and parking flags are
//! cache-line padded to reduce false sharing in scenarios where producer and
//! consumer might run on different cores.
//!
//! # Examples
//!
//! ### Synchronous SPSC Channel
//!
//! ```
//! use fibre::spsc;
//! use std::thread;
//!
//! // Create a bounded synchronous SPSC channel with a capacity of 5.
//! let (mut producer, mut consumer) = spsc::bounded_sync(5);
//!
//! let producer_handle = thread::spawn(move || {
//! for i in 0..10 {
//! match producer.send(format!("Sync Item {}", i)) {
//! Ok(()) => println!("[Sync Sender] Sent item {}", i),
//! Err(e) => {
//! eprintln!("[Sync Sender] Send error: {:?}", e);
//! break;
//! }
//! }
//! // The producer will block here if the queue is full.
//! }
//! });
//!
//! let consumer_handle = thread::spawn(move || {
//! for _ in 0..10 {
//! match consumer.recv() {
//! Ok(item) => println!("[Sync Receiver] Received: {}", item),
//! Err(e) => {
//! eprintln!("[Sync Receiver] Recv error: {:?}", e);
//! break;
//! }
//! }
//! }
//! });
//!
//! producer_handle.join().unwrap();
//! consumer_handle.join().unwrap();
//! ```
//!
//! ### Asynchronous SPSC Channel
//!
//! ```
//! use fibre::spsc;
//!
//! async fn async_spsc_example() {
//! // Create a bounded asynchronous SPSC channel with a capacity of 3.
//! let (producer, mut consumer) = spsc::bounded_async(3);
//!
//! let producer_task = tokio::spawn(async move {
//! for i in 0..7 {
//! if let Err(e) = producer.send(format!("Async Item {}", i)).await {
//! eprintln!("[Async Sender] Send error: {:?}", e);
//! break;
//! }
//! println!("[Async Sender] Sent item {}", i);
//! // The producer's send future will complete when there's space.
//! // tokio::task::yield_now().await; // Optionally yield
//! }
//! });
//!
//! let consumer_task = tokio::spawn(async move {
//! for _ in 0..7 {
//! match consumer.recv().await {
//! Ok(item) => println!("[Async Receiver] Received: {}", item),
//! Err(e) => {
//! eprintln!("[Async Receiver] Recv error: {:?}", e);
//! break;
//! }
//! }
//! }
//! });
//!
//! producer_task.await.unwrap();
//! consumer_task.await.unwrap();
//! }
//!
//! // To run the async example:
//! // tokio::runtime::Runtime::new().unwrap().block_on(async_spsc_example());
//! ```
// Private modules that form the SPSC implementation.
// `bounded_sync` contains the SpscShared core and synchronous P/C.
// `bounded_async` contains the asynchronous P/C wrappers and futures.
// Publicly re-export the primary channel constructors and types.
pub use ;
pub use ;
// Re-export common error types used by SPSC channels.
pub use crate;