pub enum PrintWriter<'a> {
Disabled,
Stdout,
CollectString(&'a mut String, Option<usize>),
CollectStreams(&'a mut CollectedStreams, Option<usize>),
Callback(&'a mut dyn PrintWriterCallback),
}Expand description
Output handler for the print() builtin function.
Provides common output modes as enum variants to avoid trait object overhead
in the typical cases (stdout, disabled, collect). For custom output handling,
use the Callback variant with a PrintWriterCallback implementation.
§Variants
Disabled— silently discards all output (useful for benchmarking or suppressing output).Stdout— writes to standard output (the default behavior).CollectString— accumulates output into a targetStringfor programmatic access. No stream labels are preserved; every fragment is appended in the order it was emitted. TheOption<usize>is an optional byte cap (None= unlimited); constructorscollect_string/ default Python collectors useDEFAULT_MAX_PRINT_COLLECT_BYTES.CollectStreams— accumulates output as(stream, text)pairs, merging consecutive same-stream fragments into one tuple. Each write to the same stream extends the trailing entry rather than producing a new one; a new tuple is only pushed when the stream changes. Same optional byte cap asCollectString.Callback— delegates to a user-providedPrintWriterCallbackimplementation.
Variants§
Disabled
Silently discard all output.
Stdout
Write to standard output.
CollectString(&'a mut String, Option<usize>)
Collect all output into a single String, in emit order, with no stream labels.
Second field: max collected bytes (None = unlimited). Exceeding raises
MemoryError with the same message as ResourceError::Memory.
CollectStreams(&'a mut CollectedStreams, Option<usize>)
Collect all output as (stream, text) entries.
The builtin print() implementation calls write for each argument and
push for each separator/terminator. To avoid one entry per fragment,
CollectedStreams appends to the trailing entry when it already
matches the current stream; a new entry is only pushed when the stream
changes. So a run that stays on one stream collects a single entry
however many fragments it wrote, and one that alternates collects one
per run.
Second field: the cap the collector’s charge is checked against (None
= unlimited), which counts COLLECT_STREAMS_ENTRY_OVERHEAD per entry
as well as the text.
Callback(&'a mut dyn PrintWriterCallback)
Delegate to a custom callback.
Implementations§
Source§impl PrintWriter<'_>
impl PrintWriter<'_>
Sourcepub fn collect_string(buf: &mut String) -> PrintWriter<'_>
pub fn collect_string(buf: &mut String) -> PrintWriter<'_>
Collect into buf with the default DEFAULT_MAX_PRINT_COLLECT_BYTES cap.
Sourcepub fn collect_streams(buf: &mut CollectedStreams) -> PrintWriter<'_>
pub fn collect_streams(buf: &mut CollectedStreams) -> PrintWriter<'_>
Collect into buf with the default DEFAULT_MAX_PRINT_COLLECT_BYTES cap.
Sourcepub fn reborrow(&mut self) -> PrintWriter<'_>
pub fn reborrow(&mut self) -> PrintWriter<'_>
Creates a new PrintWriter that reborrows the same underlying target.
This is useful in iterative execution (start/resume loops) where each
step takes PrintWriter by value but you want all steps to write to the
same output target. The original writer remains valid after the reborrowed
copy is dropped.
Sourcepub fn write(
&mut self,
stream: PrintStream,
output: Cow<'_, str>,
) -> Result<(), MontyException>
pub fn write( &mut self, stream: PrintStream, output: Cow<'_, str>, ) -> Result<(), MontyException>
Called once for each formatted argument passed to print().
This method writes only the given argument’s text, without adding
separators or a trailing newline. Separators (spaces) and the final
terminator (newline) are emitted via push.
CollectString keeps no stream labels, so a run that prints to both
streams interleaves them in one buffer. Use CollectStreams to tell
them apart.
Sourcepub fn push(
&mut self,
stream: PrintStream,
end: char,
) -> Result<(), MontyException>
pub fn push( &mut self, stream: PrintStream, end: char, ) -> Result<(), MontyException>
Appends a single character to the given stream.
Generally called to add spaces (separators) and newlines (terminators) within print output.
Sourcepub fn stdout_write(
&mut self,
output: Cow<'_, str>,
) -> Result<(), MontyException>
pub fn stdout_write( &mut self, output: Cow<'_, str>, ) -> Result<(), MontyException>
Sourcepub fn stdout_push(&mut self, end: char) -> Result<(), MontyException>
pub fn stdout_push(&mut self, end: char) -> Result<(), MontyException>
Sourcepub fn wants_poll(&self) -> bool
pub fn wants_poll(&self) -> bool
Whether this writer wants poll_flush called at all.
Only Callback can buffer, so the VM hoists this out of its dispatch
loop and skips the poll (and its clock read) entirely for every other
variant.
Sourcepub fn poll_flush(&mut self) -> Result<(), MontyException>
pub fn poll_flush(&mut self) -> Result<(), MontyException>
Forwards the VM’s periodic checkpoint to a buffering callback.