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, Deinitable, Movable, WriterBackend

fn def __init__(out self)

Initialize MemoryWriter with an empty buffer.

Args:

  • self (Self)

Returns:

Self

fn def __init__(out self, *, deinit move: Self)

Move constructor.

Args:

  • move (Self)
  • self (Self)

Returns:

Self

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

Write bytes from buffer to memory.

Args:

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

Returns:

UInt64

Raises:

fn def get_data(self) -> List[UInt8]

Get a copy of the written data.

Args:

  • self (Self)

Returns:

List[UInt8]

fn def 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 def clear(mut self)

Clear the buffer.

Args:

  • self (Self)