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[Byte])

AnyType, ImplicitlyDestructible, Movable, WriterBackend

def __init__(out self)

Initialize MemoryWriter with an empty buffer.

Args:

  • self (Self)

Returns:

Self

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

Move constructor.

Args:

  • take (Self)
  • self (Self)

Returns:

Self

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

Write bytes from buffer to memory.

Args:

  • self (Self)
  • buf (Span)
  • amt (Int)
  • pos (Int)

Returns:

UInt64

Raises:

def get_data(self) -> List[Byte]

Get a copy of the written data.

Args:

  • self (Self)

Returns:

List

def get_data_ref(self) -> Span[Byte, origin_of(self.data)]

Get owned reference to the written data.

Args:

  • self (Self)

Returns:

Span

def clear(mut self)

Clear the buffer.

Args:

  • self (Self)