struct Proto::Wire::Writer

Overview

Writer wraps an IO and provides typed protobuf wire-format write primitives.

Typical message encode pattern: writer.write_tag(1, WireType::VARINT) writer.write_int32(value)

For embedded messages use write_embedded which buffers the sub-message, then writes the length prefix followed by the bytes.

Defined in:

proto/wire/writer.cr

Constant Summary

DEFAULT_MAX_FIELD_LENGTH = (8 * 1024) * 1024

Constructors

Instance Method Summary

Constructor Detail

def self.new(io : IO, max_field_length : Int32 = DEFAULT_MAX_FIELD_LENGTH) #

[View source]

Instance Method Detail

def write_bool(value : Bool) : Nil #

[View source]
def write_bool_field(field_number : Int32, value : Bool) : Nil #

Write a bool field only if value is true.


[View source]
def write_bytes(data : Bytes) : Nil #

[View source]
def write_bytes_field(field_number : Int32, value : Bytes) : Nil #

Write a bytes field only if value is not empty.


[View source]
def write_double(value : Float64) : Nil #

[View source]
def write_embedded(field_number : Int32, & : IO -> ) : Nil #

Write a length-delimited field by buffering the block's output. Yields the underlying IO::Memory; the accumulated bytes are length-prefixed and written.


[View source]
def write_fixed32(value : UInt32) : Nil #

[View source]
def write_fixed64(value : UInt64) : Nil #

[View source]
def write_float(value : Float32) : Nil #

[View source]
def write_int32(value : Int32) : Nil #

int32: sign-extends to 64 bits (negative values become 10-byte varints)


[View source]
def write_int32_field(field_number : Int32, value : Int32) : Nil #

Write an int32 field only if value != 0.


[View source]
def write_int64(value : Int64) : Nil #

[View source]
def write_packed(field_number : Int32, & : IO::Memory -> ) : Nil #

Write a packed repeated field using the block to write each element into the buffer. Skips writing the field entirely if the array is empty.


[View source]
def write_sfixed32(value : Int32) : Nil #

[View source]
def write_sfixed64(value : Int64) : Nil #

[View source]
def write_sint32(value : Int32) : Nil #

sint32 / sint64 use zigzag encoding


[View source]
def write_sint64(value : Int64) : Nil #

[View source]
def write_string(value : String) : Nil #

[View source]
def write_string_field(field_number : Int32, value : String) : Nil #

Write a string field only if value is not empty.


[View source]
def write_tag(field_number : Int32, wire_type : Int32) : Nil #

[View source]
def write_uint32(value : UInt32) : Nil #

[View source]
def write_uint64(value : UInt64) : Nil #

[View source]
def write_uint64_field(field_number : Int32, value : UInt64) : Nil #

Write a uint64 field only if value != 0.


[View source]
def write_varint(value : UInt64) : Nil #

[View source]