Skip to main content

Mojo function

write_buffered

write_buffered[W: MovableWriter, //, *Ts: Writable, *, buffer_size: Int](owned writer: W, args: VariadicPack[origin, Writable, Ts], *, sep: StringSlice[StaticConstantOrigin] = StringSlice(""), end: StringSlice[StaticConstantOrigin] = StringSlice(""))

Use a buffer on the stack to minimize expensive calls to the writer. When the buffer would overflow it writes to the writer passed in. You can also add seperators between the args, and end characters.

Example

import sys
from utils import write_buffered

fn print_err_buffered[*Ts: Writable](
*args: *Ts, sep: StringLiteral, end: StringLiteral
):
var stdout = sys.stderr
write_buffered[buffer_size=4096](stdout, args, sep=sep, end=end)

print_err_buffered(3, "total", "args", sep=",", end="[end]")
import sys
from utils import write_buffered

fn print_err_buffered[*Ts: Writable](
*args: *Ts, sep: StringLiteral, end: StringLiteral
):
var stdout = sys.stderr
write_buffered[buffer_size=4096](stdout, args, sep=sep, end=end)

print_err_buffered(3, "total", "args", sep=",", end="[end]")
3, total, args[end]
3, total, args[end]

.

Parameters:

  • W (MovableWriter): The type of the Writer to write to.
  • *Ts (Writable): The types of each arg to write. Each type must satisfy Writable.
  • buffer_size (Int): How many bytes to write to a buffer before writing out.

Args:

  • writer (W): The Writer to write to.
  • args (VariadicPack[origin, Writable, Ts]): A VariadicPack of Writable arguments.
  • sep (StringSlice[StaticConstantOrigin]): The separator used between elements.
  • end (StringSlice[StaticConstantOrigin]): The String to write after printing the elements.