pub enum PrintWriter<'a> {
Disabled,
Stdout,
CollectString(&'a mut String, Option<usize>),
CollectStreams(&'a mut Vec<(PrintStream, String)>, 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 Vec<(PrintStream, String)>, Option<usize>)
Collect all output as (stream, text) tuples.
The builtin print() implementation calls stdout_write for each argument
and stdout_push for each separator/terminator. To avoid one tuple per
fragment, this variant appends to the trailing tuple when it already matches
the current stream; a new tuple is only pushed when the stream changes.
So long as every write targets the same stream (the status quo today, since
print() only writes to stdout), a single print(a, b) call produces one
(Stdout, "a b\n") entry — and consecutive prints with end='' likewise
merge into a single trailing entry.
Second field: max collected bytes across all tuples (None = unlimited).
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 Vec<(PrintStream, String)>) -> PrintWriter<'_>
pub fn collect_streams(buf: &mut Vec<(PrintStream, String)>) -> 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 stdout_write(
&mut self,
output: Cow<'_, str>,
) -> Result<(), MontyException>
pub fn stdout_write( &mut self, 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 stdout_push.
Sourcepub fn stdout_push(&mut self, end: char) -> Result<(), MontyException>
pub fn stdout_push(&mut self, end: char) -> Result<(), MontyException>
Appends a single character to the output.
Generally called to add spaces (separators) and newlines (terminators) within print output.