pub struct OpOutput { /* private fields */ }Expand description
The buffered output side an crate::Operator writes through.
Sends accumulate in the order they were made and are read out with
OpOutput::drain; nothing is delivered until the host does that, so an
operator can call send/send_batch/send_bytes any number of times
within one crate::Operator::on_event call without touching a
connection.
use astrs_data::prelude::*;
use astrs_operator_api::OpOutput;
use astrs_time::HlcTimestamp;
use astrs_wire::Metadata;
#[derive(Debug, PartialEq)]
struct Ping;
impl astrs_data::AstrsMessage for Ping {
const URN: &'static str = "std/core/v1/PingMsg";
fn data_type() -> DataType {
DataType::strukt([Field::required("tick", DataType::UInt8)])
}
fn to_record_batch(&self) -> astrs_data::Result<RecordBatch> {
let DataType::Struct(fields) = Self::data_type() else { unreachable!() };
let column = UInt8Array::from_values([1u8]).into_array_ref();
let strukt = StructArray::try_new(fields, vec![column], None)?;
Ok(RecordBatch::from_payload(strukt.into_array_ref()))
}
fn from_record_batch(_: &RecordBatch) -> astrs_data::Result<Self> {
Ok(Self)
}
}
let mut out = OpOutput::new();
assert!(out.is_empty());
out.send("tick", Metadata::new(HlcTimestamp::EPOCH), &Ping)?;
assert_eq!(out.len(), 1);
let sends = out.drain();
assert_eq!(sends[0].id().as_str(), "tick");
assert!(out.is_empty(), "drain empties the buffer");Implementations§
Source§impl OpOutput
impl OpOutput
Sourcepub fn send_bytes(
&mut self,
id: &str,
metadata: Metadata,
payload: Vec<u8>,
) -> OpResult<()>
pub fn send_bytes( &mut self, id: &str, metadata: Metadata, payload: Vec<u8>, ) -> OpResult<()>
Buffers a send of already-encoded bytes.
The escape hatch for a payload an operator built itself (or is
forwarding unchanged from an input) — send/send_batch are the
typed front doors that end up calling this.
§Errors
OpError::InvalidOutputId when id is not a valid
astrs_wire::DataId.
Sourcepub fn send_batch(
&mut self,
id: &str,
metadata: Metadata,
batch: &RecordBatch,
) -> OpResult<()>
pub fn send_batch( &mut self, id: &str, metadata: Metadata, batch: &RecordBatch, ) -> OpResult<()>
Buffers a send of an already-built RecordBatch.
§Errors
OpError::InvalidOutputId as OpOutput::send_bytes, or
OpError::Ipc when Arrow IPC encoding fails (blueprint §6.1’s
256 MB payload cap, for example).
Sourcepub fn send<M: AstrsMessage>(
&mut self,
id: &str,
metadata: Metadata,
message: &M,
) -> OpResult<()>
pub fn send<M: AstrsMessage>( &mut self, id: &str, metadata: Metadata, message: &M, ) -> OpResult<()>
Buffers a send of a typed AstrsMessage value.
Encodes message to its one-row RecordBatch and then to Arrow
IPC bytes — the same two steps a hand-written send_batch call
would make, saved for the common case of sending a
#[derive(AstrsMessage)] type directly.
§Errors
OpError::InvalidOutputId as OpOutput::send_bytes,
OpError::Encode when AstrsMessage::to_record_batch fails, or
OpError::Ipc as OpOutput::send_batch.