Expand description
ยงstreamweave-vec
Vector producer and consumer for StreamWeave
Produce from and consume to dynamic vectors with flexible size handling.
The streamweave-vec package provides vector-based producers and consumers for StreamWeave. It enables reading from vectors and writing to vectors with dynamic size handling.
ยงโจ Key Features
- VecProducer: Produce items from vectors
- VecConsumer: Consume items into vectors
- Dynamic Size: Vector size determined at runtime
- Capacity Management: Pre-allocate capacity for performance
- Order Preservation: Items preserved in order
ยง๐ฆ Installation
Add this to your Cargo.toml:
[dependencies]
streamweave-vec = "0.3.0"ยง๐ Quick Start
ยงProduce from Vector
use streamweave_vec::VecProducer;
use streamweave_pipeline::PipelineBuilder;
let data = vec![1, 2, 3, 4, 5];
let producer = VecProducer::new(data);
let pipeline = PipelineBuilder::new()
.producer(producer)
.consumer(/* process items */);
pipeline.run().await?;ยงConsume to Vector
use streamweave_vec::VecConsumer;
use streamweave_pipeline::PipelineBuilder;
let consumer = VecConsumer::<i32>::new();
let pipeline = PipelineBuilder::new()
.producer(/* produce items */)
.consumer(consumer);
pipeline.run().await?;
let vec = consumer.into_vec();ยง๐ API Overview
ยงVecProducer
Produces items from a vector:
pub struct VecProducer<T> {
pub data: Vec<T>,
pub config: ProducerConfig<T>,
}Key Methods:
new(data)- Create producer from vectorwith_error_strategy(strategy)- Set error handling strategywith_name(name)- Set component nameproduce()- Generate stream from vector
ยงVecConsumer
Consumes items into a vector:
pub struct VecConsumer<T> {
pub vec: Vec<T>,
pub config: ConsumerConfig<T>,
}Key Methods:
new()- Create consumerwith_capacity(capacity)- Pre-allocate capacitywith_error_strategy(strategy)- Set error handling strategywith_name(name)- Set component nameconsume(stream)- Collect items into vectorinto_vec()- Get collected vector
ยง๐ Usage Examples
ยงVector Transformation Pipeline
Transform vector elements:
use streamweave_vec::{VecProducer, VecConsumer};
use streamweave_pipeline::PipelineBuilder;
let input = vec![1, 2, 3, 4, 5];
let producer = VecProducer::new(input);
let consumer = VecConsumer::<i32>::with_capacity(10);
let pipeline = PipelineBuilder::new()
.producer(producer)
.transformer(|x: i32| x * 2) // Double each element
.consumer(consumer);
pipeline.run().await?;ยงPre-allocated Capacity
Pre-allocate vector capacity:
use streamweave_vec::VecConsumer;
let consumer = VecConsumer::<i32>::with_capacity(1000); // Pre-allocate for 1000 itemsยง๐๏ธ Architecture
Vector processing flow:
Vec<T> โโ> VecProducer โโ> Stream<T> โโ> Transformer โโ> Stream<T> โโ> VecConsumer โโ> Vec<T>Vector Flow:
- VecProducer iterates over vector elements
- Items flow through transformers
- VecConsumer collects items into vector
- Vector size grows dynamically
ยง๐ง Configuration
ยงProducer Configuration
- Error Strategy: Error handling strategy
- Name: Component name for logging
ยงConsumer Configuration
- Capacity: Pre-allocated capacity
- Error Strategy: Error handling strategy
- Name: Component name for logging
ยง๐ Error Handling
Vector errors are handled through the error system:
use streamweave_error::ErrorStrategy;
let producer = VecProducer::new(data)
.with_error_strategy(ErrorStrategy::Skip);ยงโก Performance Considerations
- Pre-allocation: Use
with_capacityfor known sizes - Dynamic Growth: Vector grows as needed
- Memory Efficiency: Efficient vector operations
ยง๐ Examples
For more examples, see:
ยง๐ Dependencies
streamweave-vec depends on:
streamweave- Core traitsstreamweave-error- Error handlingstreamweave-message(optional) - Message envelope supporttokio- Async runtimefutures- Stream utilities
ยง๐ฏ Use Cases
Vector integration is used for:
- Dynamic Data: Process data with unknown size
- Memory Efficiency: Efficient memory usage
- Testing: Test pipelines with dynamic data
- Data Collection: Collect stream items into vectors
ยง๐ Documentation
ยง๐ See Also
- streamweave - Core traits
- streamweave-array - Array-based streaming
- streamweave-error - Error handling
ยง๐ค Contributing
Contributions are welcome! Please see the Contributing Guide for details.
ยง๐ License
This project is licensed under the CC BY-SA 4.0 license.
Re-exportsยง
pub use consumers::VecConsumer;pub use producers::VecProducer;