Skip to main content

Crate gobwire

Crate gobwire 

Source
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 Vec is at least as long as the incoming slice (Go uses capacity); strings must be UTF-8; nesting deeper than MAX_DEPTH is 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§

BinaryBytes
A value of a Go type implementing encoding.BinaryMarshaler (such as url.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.
DynamicRef
A borrowed dynamic value, for encoding.
Encoder
Encodes values onto one gob stream.
FieldRef
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.
StreamDecoder
A Decoder reading from a blocking source (Go’s gob.NewDecoder(r)).
StreamEncoder
An Encoder writing to a blocking sink (Go’s gob.NewEncoder(w)).
StructDecoder
Walks one struct value’s fields. Created by ValueDecoder::structure.
StructEncoder
Writes a struct’s fields as (delta, value) pairs. Created by ValueEncoder::structure.
StructPlan
How a remote struct’s fields map onto a local struct (decode.go, compileDec).
StructType
TypeTable
The decoder’s record of every type the remote side has defined on this stream.
ValueDecoder
Reads one value’s bytes. Handed to Decode::decode_into.
ValueEncoder
Writes one value’s bytes. Handed to Encode::encode.
WireField
One field of a remote struct type.

Enums§

Error
Everything that can go wrong encoding or decoding a gob stream.
MarshalKind
Which marshaling interface a type’s bytes came from (type.go, wireType).
Progress
What Decoder::push_message made 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 Type travels alongside it.
WireType
A type definition as received on the wire (encoding/gob/type.go, wireType).
Zone
Where a GoTime is.

Constants§

MAX_DEPTH
Deepest nesting of values the decoder follows before giving up.
MAX_MESSAGE_LEN
Go’s tooBig on 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) if buf does 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.

Type Aliases§

Result

Derive Macros§

Gob