Skip to main content

PrintWriter

Enum PrintWriter 

Source
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 target String for programmatic access. No stream labels are preserved; every fragment is appended in the order it was emitted. The Option<usize> is an optional byte cap (None = unlimited); constructors collect_string / default Python collectors use DEFAULT_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 as CollectString.
  • Callback — delegates to a user-provided PrintWriterCallback implementation.

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<'_>

Source

pub fn collect_string(buf: &mut String) -> PrintWriter<'_>

Collect into buf with the default DEFAULT_MAX_PRINT_COLLECT_BYTES cap.

Source

pub fn collect_streams(buf: &mut CollectedStreams) -> PrintWriter<'_>

Collect into buf with the default DEFAULT_MAX_PRINT_COLLECT_BYTES cap.

Source

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.

Source

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.

Source

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.

Source

pub fn stdout_write( &mut self, output: Cow<'_, str>, ) -> Result<(), MontyException>

Source

pub fn stdout_push(&mut self, end: char) -> Result<(), MontyException>

Source

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.

Source

pub fn poll_flush(&mut self) -> Result<(), MontyException>

Forwards the VM’s periodic checkpoint to a buffering callback.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for PrintWriter<'a>

§

impl<'a> !Send for PrintWriter<'a>

§

impl<'a> !Sync for PrintWriter<'a>

§

impl<'a> !UnwindSafe for PrintWriter<'a>

§

impl<'a> Freeze for PrintWriter<'a>

§

impl<'a> Unpin for PrintWriter<'a>

§

impl<'a> UnsafeUnpin for PrintWriter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.