pub struct Stream { /* private fields */ }Expand description
Cloneable handle to a lazy combinator stream.
A Stream is a thin shared pointer over a StreamNode: cloning it shares
the same underlying source rather than copying packets. It is the value that
every combinator in this crate consumes and produces, forming pull-based
graphs over the homogeneous sim-stream packet spine.
§Examples
use sim_kernel::{Expr, Symbol};
use sim_lib_stream_core::{
BufferOverflowPolicy, BufferPolicy, StreamDirection, StreamItem, StreamMedia,
StreamMetadata, StreamPacket,
};
use sim_lib_stream_combinators::Stream;
let metadata = StreamMetadata::new(
Symbol::qualified("stream", "doc"),
StreamMedia::Data,
StreamDirection::Source,
Symbol::qualified("clock", "doc"),
BufferPolicy::bounded_with_overflow(8, BufferOverflowPolicy::DropNewest).unwrap(),
);
let item = StreamItem::new(StreamPacket::data(
Symbol::qualified("stream/data", "model-event"),
Expr::Nil,
));
let stream = Stream::pull(metadata, vec![item.clone()]);
assert_eq!(stream.take_packets(8).unwrap(), vec![item]);
assert!(stream.is_done().unwrap());Implementations§
Source§impl Stream
impl Stream
Sourcepub fn new(inner: impl StreamNode + 'static) -> Self
pub fn new(inner: impl StreamNode + 'static) -> Self
Wraps a StreamNode implementation in a shareable Stream handle.
Sourcepub fn from_value(value: Arc<StreamValue>) -> Self
pub fn from_value(value: Arc<StreamValue>) -> Self
Builds a stream that replays the packets held by a stream-core value.
Sourcepub fn pull(metadata: StreamMetadata, items: Vec<StreamItem>) -> Self
pub fn pull(metadata: StreamMetadata, items: Vec<StreamItem>) -> Self
Builds an in-memory pull stream from explicit metadata and packets.
Sourcepub fn metadata(&self) -> &StreamMetadata
pub fn metadata(&self) -> &StreamMetadata
Returns the metadata describing this stream’s media, clock, and buffer.
Sourcepub fn next_packet(&self) -> Result<Option<StreamItem>>
pub fn next_packet(&self) -> Result<Option<StreamItem>>
Pulls the next packet, or Ok(None) when none is currently available.
Sourcepub fn is_done(&self) -> Result<bool>
pub fn is_done(&self) -> Result<bool>
Reports whether the stream has reached its terminal done state.
Sourcepub fn take_packets(&self, limit: usize) -> Result<Vec<StreamItem>>
pub fn take_packets(&self, limit: usize) -> Result<Vec<StreamItem>>
Pulls up to limit packets, stopping early when the source is drained.
Sourcepub fn run_events(
&self,
cx: &mut Cx,
run: Ref,
start_seq: u64,
) -> Result<Vec<Event>>
pub fn run_events( &self, cx: &mut Cx, run: Ref, start_seq: u64, ) -> Result<Vec<Event>>
Drains the stream into kernel events, one chunk event per packet.
Sequence numbers start at start_seq and increase per packet; a final
done event for run is appended once the source reports done.
Sourcepub fn map_data_expr<F>(self, f: F) -> Self
pub fn map_data_expr<F>(self, f: F) -> Self
Returns a stream that rewrites each data packet’s payload expression.
Method form of the free map_data_expr
combinator; non-data packets pass through unchanged.
Sourcepub fn filter_data_kind(self, kind: Symbol) -> Self
pub fn filter_data_kind(self, kind: Symbol) -> Self
Returns a stream keeping only data packets of the given kind.
Method form of the free filter_data_kind
combinator.
Sourcepub fn filter_data_shape<F>(self, matches: F) -> Self
pub fn filter_data_shape<F>(self, matches: F) -> Self
Returns a stream keeping data packets whose payload matches matches.
Method form of the free filter_data_shape
combinator.
Sourcepub fn window_by_count(self, count: usize) -> Self
pub fn window_by_count(self, count: usize) -> Self
Returns a stream that batches packets into windows of count packets.
Method form of the free window_by_count
combinator.
Sourcepub fn tap_diagnostics<F>(self, f: F) -> Self
pub fn tap_diagnostics<F>(self, f: F) -> Self
Returns a stream that observes each diagnostic packet without altering it.
Method form of the free tap_diagnostics
combinator.