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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#![allow(clippy::type_complexity)]

//! Make asynchronous [`Stream`] and [`Sink`] easy.
//!
//! This crate contains [`ScopedStream`] and [`ScopedSink`] type.
//! They use normal Rust lifetime mechanism to ensure safety
//! (eg. sending interior data outside of it's scope).
//! Unlike [`async_stream`](https://docs.rs/async-stream/latest/async_stream/),
//! it doesn't use macro.
//!
//! ## 📌 Plan for 2.0
//!
//! Since AFIT (and RPITIT) is stabilized, i plan to upgrade this library's interface to use them.
//! This _should_ eliminate the [`Box::pin`] requirement, at the cost of complicated type bounds
//! (and harder to use too, maybe).
//! So far i've been unsuccessful to fully reason the type bounds.
//!
//! So here are the (rough) plan for (possible) 2.0:
//! - Eliminate [`Box::pin`] requirement (maybe add type alias for dynamic version).
//! - Beef up [`StreamSink`] functionality (right now it's kinda experimental).
//!
//! ## `no-std` Support
//!
//! Currently, this crate requires `alloc` (because of [`Box`] and such).
//! But it's perfectly usable on platforms like WASM.
//!
//! # Examples
//!
//! Using [`ScopedStream`]:
//! ```
//! use std::time::Duration;
//!
//! use futures_util::{SinkExt, StreamExt};
//!
//! use scoped_stream_sink::*;
//!
//! #[tokio::main]
//! async fn main() {
//!     // Create new scoped stream
//!     let mut stream = ScopedStream::new(|mut sink| Box::pin(async move {
//!         // We have to Box::pin it because otherwise the trait bounds is too complex
//!         // Interior sink cannot outlast the lifetime of it's outer stream
//!
//!         // This will not work
//!         // tokio::spawn(async move { sink.send(10000).await.unwrap() }).await.unwrap();
//!
//!         // Assume this is a complex task
//!         let (mut a, mut b) = (1usize, 1);
//!         for _ in 0..10 {
//!             sink.send(a).await.unwrap();
//!             (a, b) = (b, a + b);
//!             tokio::time::sleep(Duration::from_millis(100)).await;
//!         }
//!     }));
//!
//!     let mut v = Vec::new();
//!     while let Some(i) = stream.next().await {
//!         v.push(i);
//!     }
//!     println!("{v:?}");
//! }
//! ```
//!
//! Using [`ScopedSink`]:
//! ```
//! use std::time::Duration;
//!
//! use anyhow::Error;
//! use futures_util::{SinkExt, StreamExt};
//!
//! use scoped_stream_sink::*;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Error> {
//!     // Create new sink
//!     let mut sink = <ScopedSink<usize, Error>>::new(|mut stream| Box::pin(async move {
//!         // Unlike ScopedStream, this closure will be called over and over again,
//!         // until all values are consumed
//!
//!         // Assume this is a complex task
//!         tokio::time::sleep(Duration::from_millis(100)).await;
//!         if let Some(v) = stream.next().await {
//!             println!("Value: {v}");
//!         }
//!
//!         Ok(())
//!     }));
//!
//!     for i in 0..10 {
//!         sink.send(i).await?;
//!     }
//!     sink.close().await?;
//!
//!     Ok(())
//! }
//! ```
//!
//! These following examples will fail to compile:
//! ```compile_fail
//! # use anyhow::Error;
//! # use futures_util::{SinkExt, StreamExt};
//! # use scoped_stream_sink::*;
//! let sink = <ScopedSink<usize, Error>>::new(|mut stream| Box::pin(async move {
//!     // Moving inner stream into another thread will fail
//!     // because it might live for longer than the sink.
//!     tokio::spawn(async move {
//!         if let Some(v) = stream.next().await {
//!             println!("Value: {v}");
//!         }
//!     }).await?;
//!
//!     Ok(())
//! }));
//! ```
//!
//! ```compile_fail
//! # use anyhow::Error;
//! # use futures_util::{SinkExt, StreamExt};
//! # use scoped_stream_sink::*;
//! let stream = <ScopedTryStream<usize, Error>>::new(|mut sink| Box::pin(async move {
//!     // Moving inner sink into another thread will fail
//!     // because it might live for longer than the stream.
//!     tokio::spawn(async move {
//!         sink.send(1).await.unwrap();
//!     }).await?;
//!
//!     Ok(())
//! }));
//! ```
//!
//! Some very hacky generator out of [`ScopedStream`]:
//! ```
//! use core::pin::pin;
//! use core::ptr::NonNull;
//! use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
//!
//! use futures_util::{SinkExt, StreamExt};
//! use scoped_stream_sink::*;
//!
//! /// Create a null waker. It does nothing when waken.
//! fn nil_waker() -> Waker {
//!     fn raw() -> RawWaker {
//!         RawWaker::new(NonNull::dangling().as_ptr(), &VTABLE)
//!     }
//!
//!     unsafe fn clone(_: *const ()) -> RawWaker {
//!         raw()
//!     }
//!     unsafe fn wake(_: *const ()) {}
//!     unsafe fn wake_by_ref(_: *const ()) {}
//!     unsafe fn drop(_: *const ()) {}
//!
//!     static VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop);
//!
//!     unsafe { Waker::from_raw(raw()) }
//! }
//!
//! // Create a generator
//! let mut stream = ScopedStream::new(|mut sink| Box::pin(async move {
//!     for i in 0usize..10 {
//!         sink.send(i).await.unwrap();
//!     }
//! }));
//! let mut stream = pin!(stream);
//!
//! // Setup waker and context
//! let waker = nil_waker();
//! let mut cx = Context::from_waker(&waker);
//!
//! // The loop
//! loop {
//!     let v = match stream.as_mut().poll_next(&mut cx) {
//!         Poll::Pending => continue, // Should not happen, but continue anyways
//!         Poll::Ready(None) => break, // Stop iteration
//!         Poll::Ready(Some(v)) => v, // Process value
//!     };
//!
//!     println!("{v}");
//! }
//! ```

mod scoped_sink;
mod scoped_stream;
mod scoped_stream_sink;
mod stream_sink;
mod stream_sink_ext;

#[cfg(all(feature = "std", debug_assertions))]
use std::thread::{current, ThreadId};
#[cfg(feature = "std")]
use std::{
    marker::PhantomData,
    ops::{Deref, DerefMut},
    ptr::NonNull,
    sync::atomic::{AtomicU8, Ordering},
};

pub use crate::scoped_sink::*;
pub use crate::scoped_stream::*;
pub use crate::scoped_stream_sink::*;
pub use crate::stream_sink::*;
pub use crate::stream_sink_ext::*;

pub use futures_core::Stream;
pub use futures_sink::Sink;

pub(crate) mod sealed {
    pub(crate) trait Sealed {}
}

pub mod prelude {
    pub use crate::{
        LocalScopedSink, LocalScopedStream, LocalScopedStreamSink, LocalScopedTryStream,
        StreamSink as _, StreamSinkExt as _,
    };

    pub use futures_core::Stream as _;
    pub use futures_sink::Sink as _;

    #[cfg(feature = "std")]
    pub use crate::{ScopedSink, ScopedStream, ScopedStreamSink, ScopedTryStream};
    #[cfg(feature = "std")]
    pub use futures_util::{SinkExt as _, StreamExt as _};
}

#[cfg(feature = "std")]
const STATE_OFF: u8 = 0;
#[cfg(feature = "std")]
const STATE_ENTER: u8 = 1;
#[cfg(feature = "std")]
const STATE_LOCKED: u8 = 2;

#[cfg(feature = "std")]
/// Protects value within local thread. Call [`Self::get_inner()`] to protect inner access
/// to within thread. Call [`Self::set_inner_ctx()`] to set the context.
pub(crate) struct LocalThread<T> {
    #[cfg(debug_assertions)]
    thread: ThreadId,
    lock: AtomicU8,

    inner: T,
}

#[cfg(feature = "std")]
fn panic_expected(expect: u8, value: u8) {
    panic!("Inconsistent internal state! (expected lock state to be {:02X}, got {:02X})\nNote: Your code might use inner value across thread", expect, value);
}

#[cfg(feature = "std")]
/// Guard for inner context.
pub(crate) struct LocalThreadInnerGuard<'a, T> {
    ptr: NonNull<LocalThread<T>>,
    phantom: PhantomData<&'a LocalThread<T>>,
}

#[cfg(feature = "std")]
impl<'a, T> Drop for LocalThreadInnerGuard<'a, T> {
    fn drop(&mut self) {
        // SAFETY: Pointer is valid.
        let p = unsafe { self.ptr.as_ref() };
        if let Err(v) = p.lock.compare_exchange(
            STATE_LOCKED,
            STATE_ENTER,
            Ordering::Release,
            Ordering::Relaxed,
        ) {
            panic_expected(STATE_LOCKED, v);
        }
    }
}

#[cfg(feature = "std")]
impl<'a, T> Deref for LocalThreadInnerGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        // SAFETY: It is locked.
        unsafe { &self.ptr.as_ref().inner }
    }
}

#[cfg(feature = "std")]
impl<'a, T> DerefMut for LocalThreadInnerGuard<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // SAFETY: It is locked.
        unsafe { &mut self.ptr.as_mut().inner }
    }
}

#[cfg(feature = "std")]
/// Guard for outer context.
pub(crate) struct LocalThreadInnerCtxGuard<'a, T> {
    ptr: NonNull<LocalThread<T>>,
    phantom: PhantomData<&'a LocalThread<T>>,
}

#[cfg(feature = "std")]
impl<'a, T> Drop for LocalThreadInnerCtxGuard<'a, T> {
    fn drop(&mut self) {
        // SAFETY: Pointer is valid.
        let p = unsafe { self.ptr.as_ref() };
        if let Err(v) =
            p.lock
                .compare_exchange(STATE_ENTER, STATE_OFF, Ordering::Release, Ordering::Relaxed)
        {
            panic_expected(STATE_ENTER, v);
        }
    }
}

#[cfg(feature = "std")]
impl<'a, T> Deref for LocalThreadInnerCtxGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        // SAFETY: It is locked.
        unsafe { &self.ptr.as_ref().inner }
    }
}

#[cfg(feature = "std")]
impl<'a, T> DerefMut for LocalThreadInnerCtxGuard<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // SAFETY: It is locked.
        unsafe { &mut self.ptr.as_mut().inner }
    }
}

#[cfg(feature = "std")]
impl<T> LocalThread<T> {
    pub(crate) fn new(inner: T) -> Self {
        Self {
            #[cfg(debug_assertions)]
            thread: current().id(),
            lock: AtomicU8::new(STATE_OFF),

            inner,
        }
    }

    /// Enters inner context.
    pub(crate) fn get_inner(&self) -> LocalThreadInnerGuard<'_, T> {
        if let Err(v) = self.lock.compare_exchange(
            STATE_ENTER,
            STATE_LOCKED,
            Ordering::SeqCst,
            Ordering::Relaxed,
        ) {
            panic_expected(STATE_ENTER, v);
        }

        #[cfg(debug_assertions)]
        if self.thread != current().id() {
            panic!("Called from other thread!");
        }

        LocalThreadInnerGuard {
            ptr: self.into(),
            phantom: PhantomData,
        }
    }

    /// Enters outer context.
    pub(crate) fn set_inner_ctx(&mut self) -> LocalThreadInnerCtxGuard<'_, T> {
        if let Err(v) =
            self.lock
                .compare_exchange(STATE_OFF, STATE_ENTER, Ordering::SeqCst, Ordering::Relaxed)
        {
            panic_expected(STATE_OFF, v);
        }

        #[cfg(debug_assertions)]
        {
            self.thread = current().id();
        }

        LocalThreadInnerCtxGuard {
            ptr: self.into(),
            phantom: PhantomData,
        }
    }
}