Skip to main content
Log in

Mojo function

write_buffered

write_buffered[W: Writer, //, *Ts: Writable, *, buffer_size: Int = 4096](mut 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 stderr = sys.stderr
write_buffered(stdout, args, sep=sep, end=end)

# Buffer before allocating a string
var string = String()
write_buffered(string, 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 stderr = sys.stderr
write_buffered(stdout, args, sep=sep, end=end)

# Buffer before allocating a string
var string = String()
write_buffered(string, args, sep=sep, end=end)


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

.

Parameters:

  • W (Writer): 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 to the writer (default 4096).

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.