Skip to main content

byteflow/
output.rs

1//! Host-side output for the `print` native.
2//!
3//! Bytecode must not own stdout. Embedders inject an [`OutputSink`]; the
4//! default is [`NullSink`] so a library load is silent. CLI demos pass
5//! [`StdoutSink`].
6
7use crate::bytecode::Value;
8
9/// Receives `print` native arguments. Must not panic or block.
10pub trait OutputSink: Send + Sync + std::fmt::Debug + 'static {
11    fn write(&self, values: &[Value]);
12}
13
14/// Discard output (default for embedders and tests).
15#[derive(Debug, Clone, Copy, Default)]
16pub struct NullSink;
17
18impl OutputSink for NullSink {
19    fn write(&self, _values: &[Value]) {}
20}
21
22/// Space-separated `Display` forms plus a newline on stdout.
23#[derive(Debug, Clone, Copy, Default)]
24pub struct StdoutSink;
25
26impl OutputSink for StdoutSink {
27    fn write(&self, values: &[Value]) {
28        let mut first = true;
29        for value in values {
30            if !first {
31                print!(" ");
32            }
33            print!("{value}");
34            first = false;
35        }
36        println!();
37    }
38}