Skip to content

MemoryWriter

Mojo struct 🡭

@memory_only
struct MemoryWriter

Writer that writes to an in-memory buffer.

Useful for testing, string building, or when you need to collect output in memory before writing to a file.

  • data (List[UInt8])

AnyType, ImplicitlyDestructible, Movable, WriterBackend

fn __init__(out self)

Initialize MemoryWriter with an empty buffer.

Args:

  • self (Self)

Returns:

Self

fn __init__(out self, *, deinit take: Self)

Move constructor.

Args:

  • take (Self)
  • self (Self)

Returns:

Self

fn write_from_buffer(mut self, mut buf: Span[UInt8, MutExternalOrigin], amt: Int, pos: Int = 0) -> UInt64

Write bytes from buffer to memory.

Args:

  • self (Self)
  • buf (Span[UInt8, MutExternalOrigin])
  • amt (Int)
  • pos (Int)

Returns:

UInt64

Raises:

fn get_data(self) -> List[UInt8]

Get a copy of the written data.

Args:

  • self (Self)

Returns:

List[UInt8]

fn get_data_ref(self) -> Span[UInt8, origin_of(self.data)]

Get owned reference to the written data.

Args:

  • self (Self)

Returns:

Span[UInt8, origin_of(self.data)]

fn clear(mut self)

Clear the buffer.

Args:

  • self (Self)