gobwire 0.1.0

Go encoding/gob wire format for Rust: encoder, merge-decoding decoder, derive, and dynamic values
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! `GobType`, `Encode` and `Decode` for the standard types.
//!
//! Go ↔ Rust correspondence:
//!
//! | Go | Rust |
//! |---|---|
//! | `bool` | `bool` |
//! | `int`, `int8`…`int64` | `i8`…`i64`, `isize` (range-checked on decode) |
//! | `uint`, `uint8`…`uint64`, `uintptr` | `u8`…`u64`, `usize` |
//! | `float32`, `float64` | `f32`, `f64` |
//! | `complex128` | `(f64, f64)` is *not* used; see [`Complex`] |
//! | `string` | `String` (`&str` to encode) |
//! | `[]byte` | `Vec<u8>` |
//! | `[]T` | `Vec<T>` |
//! | `[N]T` | `[T; N]` |
//! | `map[K]V` | `HashMap<K, V>`, `BTreeMap<K, V>` |
//! | `*T` | `Option<T>` (or `Box<T>` when never nil) |
//! | `any`, `error`, other interfaces | `Option<Interface>` |

use std::collections::{BTreeMap, HashMap};
use std::hash::Hash;

use crate::decode::{Decode, ValueDecoder};
use crate::encode::{Encode, GobType, ValueEncoder};
use crate::error::{Error, Result};
use crate::types::{Describer, TypeTable, WireType};
use crate::wire::ids;

macro_rules! simple_type {
    ($t:ty, $id:expr) => {
        impl GobType for $t {
            fn describe(_: &mut Describer<'_>) -> Result<i64> {
                Ok($id)
            }
            fn compatible(_: &TypeTable, wire: i64) -> bool {
                wire == $id
            }
        }
    };
}

impl GobType for bool {
    fn describe(_: &mut Describer<'_>) -> Result<i64> {
        Ok(ids::BOOL)
    }
    fn compatible(_: &TypeTable, wire: i64) -> bool {
        wire == ids::BOOL
    }
}

impl Encode for bool {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    fn is_zero(&self) -> bool {
        !*self
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.bool(*self);
        Ok(())
    }
}

impl Decode for bool {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if wire != ids::BOOL {
            return Err(d.mismatch::<Self>(wire));
        }
        *self = d.read_uint()? != 0;
        Ok(())
    }
}

macro_rules! signed {
    ($($t:ty),*) => {$(
        simple_type!($t, ids::INT);
        impl Encode for $t {
            fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> { Self::describe(d) }
            fn is_zero(&self) -> bool { *self == 0 }
            fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> { e.int(*self as i64); Ok(()) }
        }
        impl Decode for $t {
            fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
                if wire != ids::INT {
                    return Err(d.mismatch::<Self>(wire));
                }
                let v = d.read_int()?;
                *self = <$t>::try_from(v).map_err(|_| Error::Overflow(stringify!($t)))?;
                Ok(())
            }
        }
    )*};
}

macro_rules! unsigned {
    ($($t:ty),*) => {$(
        simple_type!($t, ids::UINT);
        impl Encode for $t {
            fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> { Self::describe(d) }
            fn is_zero(&self) -> bool { *self == 0 }
            fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> { e.uint(*self as u64); Ok(()) }
        }
        impl Decode for $t {
            fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
                if wire != ids::UINT {
                    return Err(d.mismatch::<Self>(wire));
                }
                let v = d.read_uint()?;
                *self = <$t>::try_from(v).map_err(|_| Error::Overflow(stringify!($t)))?;
                Ok(())
            }
        }
    )*};
}

signed!(i8, i16, i32, i64, isize);
unsigned!(u16, u32, u64, usize);

// `u8` separately: its slices are Go's built-in `[]byte`.

impl GobType for u8 {
    fn describe(_: &mut Describer<'_>) -> Result<i64> {
        Ok(ids::UINT)
    }
    fn compatible(_: &TypeTable, wire: i64) -> bool {
        wire == ids::UINT
    }
    fn describe_slice(_: &mut Describer<'_>) -> Result<i64> {
        Ok(ids::BYTES)
    }
    fn compatible_slice(_: &TypeTable, wire: i64) -> bool {
        wire == ids::BYTES
    }
}

impl Encode for u8 {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    fn is_zero(&self) -> bool {
        *self == 0
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.uint(u64::from(*self));
        Ok(())
    }
    fn encode_slice(items: &[Self], e: &mut ValueEncoder<'_>) -> Result<()> {
        e.bytes(items);
        Ok(())
    }
}

impl Decode for u8 {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if wire != ids::UINT {
            return Err(d.mismatch::<Self>(wire));
        }
        *self = u8::try_from(d.read_uint()?).map_err(|_| Error::Overflow("u8"))?;
        Ok(())
    }
    fn decode_vec(vec: &mut Vec<Self>, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if wire != ids::BYTES {
            return Err(d.mismatch::<Vec<u8>>(wire));
        }
        let b = d.read_bytes()?;
        vec.clear();
        vec.extend_from_slice(b);
        Ok(())
    }
}

simple_type!(f64, ids::FLOAT);
simple_type!(f32, ids::FLOAT);

impl Encode for f64 {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    /// `f != 0` in Go, so `-0.0` is omitted too.
    fn is_zero(&self) -> bool {
        *self == 0.0
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.float(*self);
        Ok(())
    }
}

impl Decode for f64 {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if wire != ids::FLOAT {
            return Err(d.mismatch::<Self>(wire));
        }
        *self = d.read_float()?;
        Ok(())
    }
}

impl Encode for f32 {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    fn is_zero(&self) -> bool {
        *self == 0.0
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.float(f64::from(*self));
        Ok(())
    }
}

/// decode.go, `float32FromBits`: a finite value beyond `MaxFloat32` overflows; infinities and
/// underflow do not.
fn to_f32(v: f64) -> Result<f32> {
    let av = v.abs();
    if f64::from(f32::MAX) < av && av <= f64::MAX {
        return Err(Error::Overflow("f32"));
    }
    Ok(v as f32)
}

impl Decode for f32 {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if wire != ids::FLOAT {
            return Err(d.mismatch::<Self>(wire));
        }
        *self = to_f32(d.read_float()?)?;
        Ok(())
    }
}

/// Go's `complex128`.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Complex {
    pub re: f64,
    pub im: f64,
}

simple_type!(Complex, ids::COMPLEX);

impl Encode for Complex {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    fn is_zero(&self) -> bool {
        self.re == 0.0 && self.im == 0.0
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.complex(self.re, self.im);
        Ok(())
    }
}

impl Decode for Complex {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if wire != ids::COMPLEX {
            return Err(d.mismatch::<Self>(wire));
        }
        self.re = d.read_float()?;
        self.im = d.read_float()?;
        Ok(())
    }
}

simple_type!(String, ids::STRING);
simple_type!(str, ids::STRING);

impl Encode for String {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    fn is_zero(&self) -> bool {
        self.is_empty()
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.bytes(self.as_bytes());
        Ok(())
    }
}

impl Encode for str {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    fn is_zero(&self) -> bool {
        self.is_empty()
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.bytes(self.as_bytes());
        Ok(())
    }
}

impl Decode for String {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if wire != ids::STRING {
            return Err(d.mismatch::<Self>(wire));
        }
        *self = d.read_string()?;
        Ok(())
    }
}

// ─── containers ────────────────────────────────────────────────────────────────────────────

impl<T: GobType> GobType for Vec<T> {
    fn describe(d: &mut Describer<'_>) -> Result<i64> {
        T::describe_slice(d)
    }
    fn compatible(types: &TypeTable, wire: i64) -> bool {
        T::compatible_slice(types, wire)
    }
}

impl<T: GobType + Encode> Encode for Vec<T> {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    /// Go omits an empty slice whether or not it is nil.
    fn is_zero(&self) -> bool {
        self.is_empty()
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        T::encode_slice(self, e)
    }
}

impl<T: Decode + Default> Decode for Vec<T> {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if !Self::compatible(d.types(), wire) {
            return Err(d.mismatch::<Self>(wire));
        }
        T::decode_vec(self, d, wire)
    }
}

impl<T: GobType> GobType for [T] {
    fn describe(d: &mut Describer<'_>) -> Result<i64> {
        T::describe_slice(d)
    }
    fn compatible(types: &TypeTable, wire: i64) -> bool {
        T::compatible_slice(types, wire)
    }
}

impl<T: GobType + Encode> Encode for [T] {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    fn is_zero(&self) -> bool {
        self.is_empty()
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        T::encode_slice(self, e)
    }
}

impl<T: GobType, const N: usize> GobType for [T; N] {
    fn describe(d: &mut Describer<'_>) -> Result<i64> {
        let elem = T::describe(d)?;
        Ok(d.array(std::any::type_name::<Self>(), elem, N))
    }
    fn compatible(types: &TypeTable, wire: i64) -> bool {
        match types.get(wire).map(|t| &**t) {
            Some(WireType::Array { elem, len, .. }) => {
                *len == N as i64 && T::compatible(types, *elem)
            }
            _ => false,
        }
    }
}

impl<T: GobType + Encode, const N: usize> Encode for [T; N] {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        Self::describe(d)
    }
    /// Arrays are always sent (encode.go: the array op has no zero check).
    fn is_zero(&self) -> bool {
        false
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        e.uint(N as u64);
        for item in self {
            item.encode(e)?;
        }
        Ok(())
    }
}

impl<T: Decode, const N: usize> Decode for [T; N] {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        if !Self::compatible(d.types(), wire) {
            return Err(d.mismatch::<Self>(wire));
        }
        let elem = match d.types().get(wire).map(|t| &**t) {
            Some(WireType::Array { elem, .. }) => *elem,
            _ => return Err(d.mismatch::<Self>(wire)),
        };
        if d.read_uint()? != N as u64 {
            return Err(Error::Corrupt("length mismatch in decodeArray".into()));
        }
        for item in self.iter_mut() {
            d.expect_element(N as u64)?;
            item.decode_into(d, elem)?;
        }
        Ok(())
    }
}

macro_rules! map_impl {
    ($map:ident, $($bound:path),*) => {
        impl<K: GobType, V: GobType> GobType for $map<K, V> {
            fn describe(d: &mut Describer<'_>) -> Result<i64> {
                let k = K::describe(d)?;
                let v = V::describe(d)?;
                Ok(d.map(std::any::type_name::<Self>(), k, v))
            }
            fn compatible(types: &TypeTable, wire: i64) -> bool {
                match types.get(wire).map(|t| &**t) {
                    Some(WireType::Map { key, elem, .. }) => K::compatible(types, *key) && V::compatible(types, *elem),
                    _ => false,
                }
            }
        }

        impl<K: GobType + Encode, V: GobType + Encode> Encode for $map<K, V> {
            fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
                Self::describe(d)
            }
            /// Go omits only a **nil** map and sends an empty non-nil one. A Rust map cannot be
            /// nil, so an empty map is treated as the nil a zero-valued Go struct holds: an empty
            /// non-nil Go map decodes to an empty Rust map and re-encodes as nil. Code that
            /// must tell the two apart has to keep the distinction itself.
            fn is_zero(&self) -> bool {
                self.is_empty()
            }
            fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
                e.uint(self.len() as u64);
                for (k, v) in self {
                    k.encode(e)?;
                    v.encode(e)?;
                }
                Ok(())
            }
        }

        impl<K: Decode + Default $(+ $bound)*, V: Decode + Default> Decode for $map<K, V> {
            /// decode.go, `decodeMap`: each key and element decodes into a fresh zero value and
            /// is then stored, replacing any existing entry — entries are not merged.
            fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
                if !Self::compatible(d.types(), wire) {
                    return Err(d.mismatch::<Self>(wire));
                }
                let (key, elem) = match d.types().get(wire).map(|t| &**t) {
                    Some(WireType::Map { key, elem, .. }) => (*key, *elem),
                    _ => return Err(d.mismatch::<Self>(wire)),
                };
                let n = d.read_uint()?;
                for _ in 0..n {
                    d.expect_element(n)?;
                    let mut k = K::default();
                    k.decode_into(d, key)?;
                    let mut v = V::default();
                    v.decode_into(d, elem)?;
                    self.insert(k, v);
                }
                Ok(())
            }
        }
    };
}

map_impl!(HashMap, Hash, Eq);
map_impl!(BTreeMap, Ord);

/// `*T`. (`Option<Interface>` is an interface, not a pointer: see `value.rs`.)
impl<T: GobType> GobType for Option<T> {
    fn describe(d: &mut Describer<'_>) -> Result<i64> {
        T::describe(d)
    }
    fn compatible(types: &TypeTable, wire: i64) -> bool {
        T::compatible(types, wire)
    }
}

impl<T: GobType + Encode> Encode for Option<T> {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        T::describe(d)
    }
    /// A nil pointer is omitted, and so is a pointer to a value Go would omit.
    fn is_zero(&self) -> bool {
        match self {
            None => true,
            Some(v) => v.is_zero(),
        }
    }
    fn frames_as_struct(&self) -> bool {
        self.as_ref().is_some_and(Encode::frames_as_struct)
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        match self {
            Some(v) => v.encode(e),
            None => Err(Error::Encode(format!(
                "cannot encode nil pointer of type {}",
                std::any::type_name::<T>()
            ))),
        }
    }
}

impl<T: Decode + Default> Decode for Option<T> {
    /// decode.go, `decAlloc`: allocate a nil pointer, then decode through it — into the
    /// existing pointee when there is one.
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        self.get_or_insert_with(T::default).decode_into(d, wire)
    }
}

impl<T: GobType + ?Sized> GobType for Box<T> {
    fn describe(d: &mut Describer<'_>) -> Result<i64> {
        T::describe(d)
    }
    fn compatible(types: &TypeTable, wire: i64) -> bool {
        T::compatible(types, wire)
    }
}

impl<T: Encode + ?Sized> Encode for Box<T> {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        (**self).describe_value(d)
    }
    fn is_zero(&self) -> bool {
        (**self).is_zero()
    }
    fn frames_as_struct(&self) -> bool {
        (**self).frames_as_struct()
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        (**self).encode(e)
    }
}

impl<T: Decode + ?Sized> Decode for Box<T> {
    fn decode_into(&mut self, d: &mut ValueDecoder<'_>, wire: i64) -> Result<()> {
        (**self).decode_into(d, wire)
    }
}

impl<T: GobType + ?Sized> GobType for &T {
    fn describe(d: &mut Describer<'_>) -> Result<i64> {
        T::describe(d)
    }
    fn compatible(types: &TypeTable, wire: i64) -> bool {
        T::compatible(types, wire)
    }
}

impl<T: Encode + ?Sized> Encode for &T {
    fn describe_value(&self, d: &mut Describer<'_>) -> Result<i64> {
        (**self).describe_value(d)
    }
    fn is_zero(&self) -> bool {
        (**self).is_zero()
    }
    fn frames_as_struct(&self) -> bool {
        (**self).frames_as_struct()
    }
    fn encode(&self, e: &mut ValueEncoder<'_>) -> Result<()> {
        (**self).encode(e)
    }
}