Skip to main content

PrintWriter

Enum PrintWriter 

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

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 Vec<(PrintStream, String)>) -> 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 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.

Source

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.

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.