Mojo trait
Writer
Describes a type that can be written to by any type that implements the write_to
function.
This enables you to write one implementation that can be written to a
variety of types such as file descriptors, strings, network locations etc.
The types are written as a Span[Byte]
, so the Writer
can avoid
allocations depending on the requirements. There is also a general write
that takes multiple args that implement write_to
.
Example:
from utils import Span
@value
struct NewString(Writer, Writable):
var s: String
# Writer requirement to write a Span of Bytes
fn write_bytes(inout self, bytes: Span[Byte, _]):
self.s._iadd[False](bytes)
# Writer requirement to take multiple args
fn write[*Ts: Writable](inout self, *args: *Ts):
@parameter
fn write_arg[T: Writable](arg: T):
arg.write_to(self)
args.each[write_arg]()
# Also make it Writable to allow `print` to write the inner String
fn write_to[W: Writer](self, inout writer: W):
writer.write(self.s)
@value
struct Point(Writable):
var x: Int
var y: Int
# Pass multiple args to the Writer. The Int and StringLiteral types
# call `writer.write_bytes` in their own `write_to` implementations.
fn write_to[W: Writer](self, inout writer: W):
writer.write("Point(", self.x, ", ", self.y, ")")
# Enable conversion to a String using `str(point)`
fn __str__(self) -> String:
return String.write(self)
fn main():
var point = Point(1, 2)
var new_string = NewString(str(point))
new_string.write("\n", Point(3, 4))
print(new_string)
from utils import Span
@value
struct NewString(Writer, Writable):
var s: String
# Writer requirement to write a Span of Bytes
fn write_bytes(inout self, bytes: Span[Byte, _]):
self.s._iadd[False](bytes)
# Writer requirement to take multiple args
fn write[*Ts: Writable](inout self, *args: *Ts):
@parameter
fn write_arg[T: Writable](arg: T):
arg.write_to(self)
args.each[write_arg]()
# Also make it Writable to allow `print` to write the inner String
fn write_to[W: Writer](self, inout writer: W):
writer.write(self.s)
@value
struct Point(Writable):
var x: Int
var y: Int
# Pass multiple args to the Writer. The Int and StringLiteral types
# call `writer.write_bytes` in their own `write_to` implementations.
fn write_to[W: Writer](self, inout writer: W):
writer.write("Point(", self.x, ", ", self.y, ")")
# Enable conversion to a String using `str(point)`
fn __str__(self) -> String:
return String.write(self)
fn main():
var point = Point(1, 2)
var new_string = NewString(str(point))
new_string.write("\n", Point(3, 4))
print(new_string)
Output:
Point(1, 2)
Point(3, 4)
Point(1, 2)
Point(3, 4)
Implemented traits
AnyType
Methods
write_bytes
write_bytes(inout self: _Self, bytes: Span[SIMD[uint8, 1], origin])
Write a Span[Byte]
to this Writer
.
Args:
- bytes (
Span[SIMD[uint8, 1], origin]
): The string slice to write to this Writer. Must NOT be null-terminated.
write
write[*Ts: Writable](inout self: _Self, *args: *Ts)
Write a sequence of Writable arguments to the provided Writer.
Parameters:
- *Ts (
Writable
): Types of the provided argument sequence.
Args:
- *args (
*Ts
): Sequence of arguments to write to this Writer.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!