foundation_core 0.0.4

Central crate for all foundation crates for the ewe-platform project
Documentation
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Stream types and traits for valtron stream processing.
//!
//! This module contains the core stream abstractions used throughout valtron:
//! - [`Stream<D, P>`] - Enum representing stream states (Init, Ignore, Delayed, Pending, Next)
//! - [`StreamIterator`] - Trait for iterators yielding Stream items
//! - [`ConcurrentQueueStreamIterator<D, P>`] - Default iterator with configurable polling (max_turns)
//! - [`StreamRecvIterator<D, P>`] - Legacy iterator for channel-based streams (deprecated)
//! - [`StreamIteratorExt`] - Extension trait with stream combinators
//!
//! # Default Iterator
//!
//! The [`ConcurrentQueueStreamIterator`] is the default iterator used by valtron executors.
//! It provides configurable polling behavior via `max_turns` parameter, balancing
//! responsiveness (checking other tasks) against throughput (busy polling).
//!
//! ## Constants
//!
//! - [`DEFAULT_MAX_TURNS`](crate::valtron::executors::DEFAULT_MAX_TURNS) - Default poll attempts before yielding (10)
//! - [`DEFAULT_PARK_DURATION`](crate::valtron::executors::DEFAULT_PARK_DURATION) - Default park duration (20ns)

use std::sync::Arc;

use concurrent_queue::ConcurrentQueue;

use crate::synca::mpp::RecvIterator;

#[derive(Clone)]
pub enum StreamSpread<D, P> {
    Pending(P),
    Done(D),
}

impl<D: PartialEq, P: PartialEq> PartialEq for StreamSpread<D, P> {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Done(me), Self::Done(them)) => me == them,
            (Self::Pending(me), Self::Pending(them)) => me == them,
            _ => false,
        }
    }
}

impl<D: core::fmt::Debug, P: core::fmt::Debug> core::fmt::Display for StreamSpread<D, P> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pending(p) => write!(f, "Pending({p:?})"),
            Self::Done(d) => write!(f, "Next({d:?})"),
        }
    }
}

impl<D: core::fmt::Debug, P: core::fmt::Debug> core::fmt::Debug for StreamSpread<D, P> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pending(p) => f.debug_tuple("Pending").field(p).finish(),
            Self::Done(d) => f.debug_tuple("Next").field(d).finish(),
        }
    }
}

/// [`Stream<D, P>`] represents the state of a stream in the valtron execution model.
///
/// Valtron uses a progress-driven execution model where the executor polls iterators
/// frequently to observe intermediate states and make scheduling decisions. The variants
/// of this enum communicate different stream states to the executor:
///
/// - [`Stream::Init`] - Stream is initializing
/// - [`Stream::Ignore`] - Internal system operations occurring, executor should ignore
/// - [`Stream::Delayed`] - Next response will be delayed by the contained duration
/// - [`Stream::Pending`] - Stream is pending with the contained context value
/// - [`Stream::Next`] - Stream has produced the next value
///
/// # Type Parameters
///
/// * `D` - The data type yielded by the stream when complete
/// * `P` - The progress/context type yielded while pending
#[derive(PartialEq, Clone)]
pub enum Stream<D, P> {
    /// Indicative the stream is instantiating.
    Init,

    /// Indicative of internal system operations occuring that should be ignored.
    Ignore,

    /// Indicative that the stream next response will be delayed by this much duration
    Delayed(std::time::Duration),

    /// Indicating that the stream is a pending state with giving context value.
    Pending(P),

    /// Indicative the stream just issued its next value.
    Next(D),

    /// No data available, queue still open.
    /// Propagates to executor as a yield signal — on JS, maps to a short setTimeout (~4ms).
    Wait,

    /// Emit multiple next values at once.
    /// Each element is delivered individually as `Stream::Next(value)` at the delivery point.
    Spread(Vec<StreamSpread<D, P>>),
}

impl<D: core::fmt::Debug, P: core::fmt::Debug> core::fmt::Display for Stream<D, P> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Stream::Init => write!(f, "Init"),
            Stream::Ignore => write!(f, "Ignore"),
            Stream::Delayed(d) => write!(f, "Delayed({d:?})"),
            Stream::Pending(p) => write!(f, "Pending({p:?})"),
            Stream::Next(d) => write!(f, "Next({d:?})"),
            Stream::Wait => write!(f, "Wait"),
            Stream::Spread(items) => write!(f, "Spread({items:?})"),
        }
    }
}

impl<D: core::fmt::Debug, P: core::fmt::Debug> core::fmt::Debug for Stream<D, P> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Stream::Init => f.write_str("Init"),
            Stream::Ignore => f.write_str("Ignore"),
            Stream::Delayed(d) => f.debug_tuple("Delayed").field(d).finish(),
            Stream::Pending(p) => f.debug_tuple("Pending").field(p).finish(),
            Stream::Next(d) => f.debug_tuple("Next").field(d).finish(),
            Stream::Wait => f.write_str("Wait"),
            Stream::Spread(items) => f.debug_tuple("Spread").field(items).finish(),
        }
    }
}

/// [`StreamIterator`] defines an iterator that yields [`Stream<D, P>`] items.
///
/// This trait is automatically implemented for any type that implements
/// `Iterator<Item = Stream<D, P>>` where `D` and `P` are `Send + 'static`.
///
/// # Examples
///
/// ```
/// use foundation_core::valtron::Stream;
///
/// // Any iterator yielding Stream<D, P> automatically implements StreamIterator
/// let items = vec![
///     Stream::Init,
///     Stream::Pending("loading..."),
///     Stream::Next(42),
/// ];
/// let mut iter = items.into_iter();
/// assert!(matches!(iter.next(), Some(Stream::Init)));
/// ```
pub trait StreamIterator: Iterator<Item = Stream<Self::D, Self::P>> {
    /// The data type yielded when the stream completes
    type D;
    /// The progress/context type yielded while pending
    type P;
}

/// Newtype wrapper that provides a clean entry point for chaining valtron
/// [`StreamIteratorExt`] combinators.  Construct via
/// [`StreamIteratorExt::into_stream_iter`].
///
/// `StreamIterator` requires `Iterator` as a supertrait, so this wrapper
/// provides an `Iterator` impl that delegates to the inner value.  The
/// value-add is ergonomic — `.into_stream_iter().map_done(…)` is
/// unambiguously the valtron combinator.
pub struct StreamIter<S: StreamIterator>(pub S);

impl<S: StreamIterator> Iterator for StreamIter<S> {
    type Item = Stream<S::D, S::P>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

// Blanket implementation for any compatible iterator (Send variant — multi-threaded)
#[cfg(feature = "multi")]
impl<M, D, P> StreamIterator for M
where
    M: Iterator<Item = Stream<D, P>> + ?Sized,
    D: Send + 'static,
    P: Send + 'static,
{
    type D = D;
    type P = P;
}

// Blanket implementation for any compatible iterator (non-Send variant — wasm32 / single-threaded)
#[cfg(not(feature = "multi"))]
impl<M, D, P> StreamIterator for M
where
    M: Iterator<Item = Stream<D, P>> + ?Sized,
    D: 'static,
    P: 'static,
{
    type D = D;
    type P = P;
}

/// [`StreamRecvIterator<D, P>`] wraps a [`RecvIterator<Stream<D, P>>`] to provide
/// stream iteration over channel-based streams.
///
/// # Deprecation
///
/// **Deprecated:** Use [`ConcurrentQueueStreamIterator`] instead.
///
/// `StreamRecvIterator` uses blocking reads which can starve other tasks in multi-task
/// scenarios. `ConcurrentQueueStreamIterator` provides configurable polling behavior
/// with `max_turns` for better task scheduling.
///
/// ## Migration
///
/// Replace:
/// ```ignore
/// StreamRecvIterator::new(recv_iter)
/// ```
///
/// With:
/// ```ignore
/// ConcurrentQueueStreamIterator::new(chan, DEFAULT_MAX_TURNS, DEFAULT_PARK_DURATION)
/// ```
///
/// This type is used when receiving [`Stream`] items from a concurrent channel,
/// adapting the blocking channel receiver to the valtron stream model.
#[deprecated(since = "1.3.0", note = "Use ConcurrentQueueStreamIterator instead")]
pub struct StreamRecvIterator<D, P>(RecvIterator<Stream<D, P>>);

#[allow(deprecated)]
impl<D, P> StreamRecvIterator<D, P> {
    /// Returns true if the underlying channel is empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the number of items in the underlying channel
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true if the underlying channel is closed
    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.0.is_closed()
    }
}

#[allow(deprecated)]
impl<D, P> StreamRecvIterator<D, P> {
    /// Creates a new StreamRecvIterator wrapping the given RecvIterator
    #[must_use]
    pub fn new(iter: RecvIterator<Stream<D, P>>) -> Self {
        Self(iter)
    }
}

#[allow(deprecated)]
impl<D, P> Iterator for StreamRecvIterator<D, P> {
    type Item = Stream<D, P>;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

// ============================================================================
// ConcurrentQueueStreamIterator
// ============================================================================

use concurrent_queue::PopError;
use std::time::Duration;

/// An iterator that polls a concurrent queue with configurable max_turns behavior.
///
/// `ConcurrentQueueStreamIterator` is designed for valtron executors where multiple
/// tasks share CPU time. It polls the queue up to `max_turns` times before yielding
/// `Stream::Ignore`, allowing the executor to check other tasks.
///
/// ## Polling Strategy
///
/// - **Turns 1 to max_turns**: Poll queue, yield thread briefly between attempts
/// - **After max_turns**: Return `Stream::Ignore` (signals executor to check other tasks)
/// - **Queue closed**: Return `None` (iterator exhausted)
///
/// ## std vs no_std
///
/// - **std feature**: Uses `std::thread::park_timeout(self.park_duration)`
/// - **no_std**: Uses `std::hint::spin_loop()` (busy-wait spin)
///
/// ## Type Parameters
///
/// * `D` - The "Done" type (successful result)
/// * `P` - The "Pending" type (intermediate state)
///
/// ## Example
///
/// ```ignore
/// use crate::valtron::streams::{Stream, ConcurrentQueueStreamIterator};
/// use concurrent_queue::ConcurrentQueue;
/// use std::sync::Arc;
/// use std::time::Duration;
///
/// let queue: Arc<ConcurrentQueue<Stream<i32, &str>>> = Arc::new(ConcurrentQueue::bounded(10));
/// let iter = ConcurrentQueueStreamIterator::new(queue.clone(), 10, Duration::from_nanos(20));
///
/// for item in iter {
///     match item {
///         Stream::Next(value) => println!("Got: {}", value),
///         Stream::Ignore => println!("Yielding to check other tasks..."),
///         Stream::Pending(ctx) => println!("Pending: {}", ctx),
///         _ => {}
///     }
/// }
/// ```
pub struct ConcurrentQueueStreamIterator<D, P> {
    chan: Arc<ConcurrentQueue<Stream<D, P>>>,
    max_turns: usize,
    park_duration: Duration,
}

impl<D, P> ConcurrentQueueStreamIterator<D, P> {
    /// Creates a new ConcurrentQueueStreamIterator.
    ///
    /// ## Arguments
    ///
    /// * `chan` - The concurrent queue to poll
    /// * `max_turns` - Number of poll attempts before yielding Stream::Ignore
    /// * `park_duration` - Duration to park thread when queue is empty (std mode)
    ///
    /// ## Panics
    ///
    /// Panics if `max_turns` is 0 (must be at least 1)
    pub fn new(
        chan: Arc<ConcurrentQueue<Stream<D, P>>>,
        max_turns: usize,
        park_duration: Duration,
    ) -> Self {
        assert!(max_turns > 0, "max_turns must be greater than 0");
        tracing::trace!(
            max_turns = max_turns,
            park_duration_ns = park_duration.as_nanos(),
            "created ConcurrentQueueStreamIterator"
        );
        Self {
            chan,
            max_turns,
            park_duration,
        }
    }

    /// Returns a reference to the underlying channel.
    #[must_use]
    pub fn chan(&self) -> &Arc<ConcurrentQueue<Stream<D, P>>> {
        &self.chan
    }

    /// Returns the configured max_turns value.
    #[must_use]
    pub fn max_turns(&self) -> usize {
        self.max_turns
    }

    /// Returns the configured park_duration value.
    #[must_use]
    pub fn park_duration(&self) -> Duration {
        self.park_duration
    }

    /// Returns true if the underlying queue is closed.
    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.chan.is_closed()
    }

    /// Returns the current number of items in the queue.
    #[must_use]
    pub fn len(&self) -> usize {
        self.chan.len()
    }

    /// Returns true if the queue is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.chan.is_empty()
    }
}

impl<D, P> Iterator for ConcurrentQueueStreamIterator<D, P> {
    type Item = Stream<D, P>;

    fn next(&mut self) -> Option<Self::Item> {
        tracing::trace!(max_turns = self.max_turns, "starting poll cycle");

        for turn in 0..self.max_turns {
            match self.chan.pop() {
                Ok(value) => {
                    tracing::debug!(turn = turn, "received value from queue");
                    return Some(value);
                }
                Err(PopError::Empty) => {
                    #[cfg(feature = "std")]
                    std::thread::park_timeout(self.park_duration);
                    #[cfg(not(feature = "std"))]
                    std::hint::spin_loop();
                }
                Err(PopError::Closed) => {
                    tracing::debug!("queue closed, ending iteration");
                    return None;
                }
            }
        }

        tracing::trace!("max_turns reached, yielding Ignore");
        Some(Stream::Ignore)
    }
}

// ============================================================================
// StreamIterator Extension Trait and Combinators
// ============================================================================

/// Control enum for the `map_circuit` combinator.
///
/// Allows short-circuiting iterator chains with three possible outcomes:
/// - `Continue(item)` - Continue iteration with the transformed item
/// - `ReturnAndStop(item)` - Return this item and then stop iteration permanently
/// - `Stop` - Stop iteration without returning anything
///
/// This is useful for error handling patterns where you want to:
/// - Return an error value and immediately stop when an error is encountered
/// - Stop silently without returning anything in certain conditions
/// - Continue normal iteration otherwise
///
/// ## Example
///
/// ```ignore
/// let stream = execute(my_task)?
///     .map_circuit(|item| {
///         match item {
///             Stream::Next(err_result) if err_result.is_error() => {
///                 ShortCircuit::ReturnAndStop(Stream::Next(err_result))
///             }
///             Stream::Pending(_) | Stream::Next(_) => {
///                 ShortCircuit::Continue(item)
///             }
///             _ => ShortCircuit::Stop,
///         }
///     });
/// ```
pub enum ShortCircuit<D, P> {
    /// Continue iteration with the wrapped item
    Continue(Stream<D, P>),
    /// Return this item and then stop iteration permanently
    ReturnAndStop(Stream<D, P>),
    /// Stop iteration without returning anything
    Stop,
}