Expand description
Go’s encoding/gob wire format.
gobwire reads and writes the byte streams Go’s gob.Encoder and gob.Decoder exchange —
the encoding under Go’s net/rpc and HashiCorp’s net/rpc plugin protocol — so a Rust program
can talk to a Go one without either side changing.
use gobwire::{Decoder, Encoder, Gob, Progress, parse_length_prefix};
#[derive(Gob, Debug, Default, PartialEq)]
struct Point {
#[gob(name = "X")]
x: i64,
#[gob(name = "Y")]
y: i64,
}
let bytes = Encoder::new().encode(&Point { x: 22, y: 33 }).unwrap();
let mut decoder = Decoder::new();
let mut rest = &bytes[..];
let point: Point = loop {
let (width, len) = parse_length_prefix(rest).unwrap().unwrap();
let body = &rest[width..width + len];
rest = &rest[width + len..];
if decoder.push_message(body).unwrap() == Progress::Ready {
break decoder.decode().unwrap();
}
};
assert_eq!(point, Point { x: 22, y: 33 });§What matches Go, and what cannot
- Field matching is by Go field name, never by position or JSON tag. Name every field
with
#[gob(name = "...")]unless the snake_case→PascalCase default is exactly right. - Zero values are omitted from structs exactly where Go omits them (
Encode::is_zero). - Decoding merges into the destination (
Decode): absent fields keep their values. - A value may span several messages (
Decoder). - Divergences, each forced by Rust’s types: an empty map is sent as nil; a slice merges
into existing elements only when the
Vecis at least as long as the incoming slice (Go uses capacity); strings must be UTF-8; nesting deeper thanMAX_DEPTHis refused; a nil interface being skipped is parsed correctly where Go’s own skip misreads it.
Modules§
- ids
- Built-in type ids, predefined on every connection (type.go:282-288, doc.go).
- names
- Registered names for Go’s built-in types (type.go,
registerBasics).
Structs§
- Binary
Bytes - A value of a Go type implementing
encoding.BinaryMarshaler(such asurl.URL). - Complex
- Go’s
complex128. - Decoder
- Decodes values from one gob stream, fed one message at a time.
- Describer
- Assigns wire type ids for local types and queues their definitions.
- Dynamic
- An owned dynamic value of any type. Decodes from any remote type (it is not an interface: the remote side sent a concrete value), and re-encodes as the same wire shape.
- Dynamic
Ref - A borrowed dynamic value, for encoding.
- Encoder
- Encodes values onto one gob stream.
- Field
Ref - One field present in a struct value.
- GoTime
- A Go
time.Time: seconds since 0001-01-01 UTC, nanoseconds, and a zone. - GobBytes
- A value of a Go type implementing
gob.GobEncoder(such as*big.Int). - Interface
- A non-nil Go interface value: the name its concrete type was registered under
(
gob.Register), the concrete type, and the value. - Stream
Decoder - A
Decoderreading from a blocking source (Go’sgob.NewDecoder(r)). - Stream
Encoder - An
Encoderwriting to a blocking sink (Go’sgob.NewEncoder(w)). - Struct
Decoder - Walks one struct value’s fields. Created by
ValueDecoder::structure. - Struct
Encoder - Writes a struct’s fields as
(delta, value)pairs. Created byValueEncoder::structure. - Struct
Plan - How a remote struct’s fields map onto a local struct (decode.go,
compileDec). - Struct
Type - Type
Table - The decoder’s record of every type the remote side has defined on this stream.
- Value
Decoder - Reads one value’s bytes. Handed to
Decode::decode_into. - Value
Encoder - Writes one value’s bytes. Handed to
Encode::encode. - Wire
Field - One field of a remote struct type.
Enums§
- Error
- Everything that can go wrong encoding or decoding a gob stream.
- Marshal
Kind - Which marshaling interface a type’s bytes came from (type.go,
wireType). - Progress
- What
Decoder::push_messagemade of a message. - Type
- The shape of a dynamic value, built from the remote side’s type definitions.
- Value
- A dynamically typed gob value. Its
Typetravels alongside it. - Wire
Type - A type definition as received on the wire (encoding/gob/type.go,
wireType). - Zone
- Where a
GoTimeis.
Constants§
- MAX_
DEPTH - Deepest nesting of values the decoder follows before giving up.
- MAX_
MESSAGE_ LEN - Go’s
tooBigon a 64-bit platform (decoder.go:19):(1 << 30) << 3. - UNIX_
TO_ INTERNAL - Seconds from 0001-01-01 to 1970-01-01 (time.go,
unixToInternal).
Traits§
- Decode
- A value that can be decoded from a gob stream into an existing value.
- Encode
- A value that can be written to a gob stream.
- GobType
- A Rust type with a fixed gob wire type.
Functions§
- frame
- Frame a message body with its length prefix.
- parse_
length_ prefix - Parse a message’s length prefix.
Ok(None)ifbufdoes not yet hold the whole prefix; otherwise the prefix width and the body length that follows it. - parse_
uint - Decode an unsigned integer from the front of
buf, returning it and its width. - read_
message - Read one length-prefixed message body.
Ok(None)at a clean end of input.