thruput-xdrs 0.17.1

XDR Serialization Library
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
mod common;
use common::*;
use core::ops::Deref;
use xdrs::{ReadResult, ReadXDR, Reader, WriteXDR, read_xdr};

#[cfg(feature = "repr")]
mod xdr_field_rename {
    use xdrs::xdr_repr::*;

    #[test]
    fn struct_field_xdr_name_honored() {
        #[allow(dead_code)]
        #[derive(xdrs::XDR)]
        struct Packet {
            #[xdr(name = "wire_count")]
            rust_count: u32,
        }

        let type_def = <Packet as HasTypeDef>::type_def();
        let formatted = Definition::Type(type_def.clone()).to_string();

        let TypeDef::Struct(_, body) = &type_def else {
            panic!("derive should emit a struct type definition");
        };
        let fields = body.fields();

        assert_eq!(fields.len(), 1);
        let Declaration::Named { name, .. } = &fields[0] else {
            panic!("expected a named declaration");
        };

        assert_eq!(name.0, "wire_count");
        assert!(formatted.contains("unsigned int wire_count;"));
        assert!(!formatted.contains("rust_count"));
    }

    #[test]
    fn struct_field_without_rename_uses_rust_name() {
        #[allow(dead_code)]
        #[derive(xdrs::XDR)]
        struct Packet {
            count: u32,
        }

        let type_def = <Packet as HasTypeDef>::type_def();
        let formatted = Definition::Type(type_def).to_string();
        assert!(formatted.contains("unsigned int count;"));
    }
}

#[test]
fn struct_foo() {
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Foo {
        bytes: [u8; 3],
        blerg: i32,
    }

    // Given: a value and its expected encoding
    let val: Foo = Foo {
        bytes: [0xAA, 0xBB, 0xCC],
        blerg: 7,
    };

    let expected = &hex!("aabbcc07");
    CXS::check_codec(&val, expected);

    let expected = &hex!("aabbcc0000000007");
    TXS::check_codec(&val, expected);
}

#[test]
fn struct_boo() {
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Boo<'a> {
        blerg: i32,
        bytes: &'a [u8; 9],
    }

    let bytes = [7u8; 9];
    let val = Boo {
        blerg: -5,
        bytes: &bytes,
    };

    let expected = &hex!("3a070707070707070707");
    CXS::check_codec(&val, expected);

    let expected = &hex!("fffffffb070707070707070707000000");
    TXS::check_codec(&val, expected);
}

#[test]
fn struct_unit() {
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Unity();

    let val = Unity();

    CXS::check_codec(&val, b"");
    TXS::check_codec(&val, b"");
}

#[test]
fn struct_bare() {
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Bare;

    let val = Bare;

    CXS::check_codec(&val, b"");
    TXS::check_codec(&val, b"");
}

#[test]
fn newtype() {
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct New<'a>(&'a [u8; 6]);

    let bytes = [0, 1, 2, 3, 4, 5];
    let val = New(&bytes);

    let expected = &hex!("000102030405");
    CXS::check_codec(&val, expected);

    let expected = &hex!("0001020304050000");
    TXS::check_codec(&val, expected);
}

#[test]
fn struct_into_from_type() {
    // Serialize a u8 as a u32

    #[derive(Debug, Clone, PartialEq, Eq, ReadXDR, WriteXDR)]
    #[xdr(into_type = "u32", from_type = "u32")]
    struct U32(u8);

    impl Into<u32> for U32 {
        fn into(self) -> u32 {
            self.0.into()
        }
    }

    impl From<u32> for U32 {
        fn from(n: u32) -> Self {
            Self(n.try_into().unwrap())
        }
    }

    let val = U32(7);

    let expected = &hex!("07");
    CXS::check_codec(&val, expected);

    let expected = &hex!("00000007");
    TXS::check_codec(&val, expected);
}

#[test]
fn struct_via_type() {
    // Serialize a u8 as a u32

    #[derive(Debug, Clone, PartialEq, Eq, ReadXDR, WriteXDR)]
    #[xdr(via = "u32")]
    struct U32(u8);

    impl Into<u32> for U32 {
        fn into(self) -> u32 {
            self.0.into()
        }
    }

    impl From<u32> for U32 {
        fn from(n: u32) -> Self {
            Self(n.try_into().unwrap())
        }
    }

    let val = U32(7);

    let expected = &hex!("07");
    CXS::check_codec(&val, expected);

    let expected = &hex!("00000007");
    TXS::check_codec(&val, expected);
}

#[test]
fn field_into_from_type() {
    // Something that doesn't impl *XDR, perhaps a foreign type
    #[derive(Debug, Clone, PartialEq, Eq)]
    struct U32(u32);

    impl From<u32> for U32 {
        fn from(n: u32) -> Self {
            Self(n)
        }
    }

    impl Into<u32> for U32 {
        fn into(self) -> u32 {
            self.0
        }
    }

    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Container {
        #[xdr(into_type = "u32", from_type = "u32")]
        item: U32,
    }

    let val = Container { item: U32(7) };

    let expected = &hex!("07");
    CXS::check_codec(&val, expected);

    let expected = &hex!("00000007");
    TXS::check_codec(&val, expected);
}

#[test]
fn field_via_type() {
    // Something that doesn't impl *XDR, perhaps a foreign type
    #[derive(Debug, Clone, PartialEq, Eq)]
    struct U32(u32);

    impl From<u32> for U32 {
        fn from(n: u32) -> Self {
            Self(n)
        }
    }

    impl Into<u32> for U32 {
        fn into(self) -> u32 {
            self.0
        }
    }

    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Container {
        #[xdr(via = "u32")]
        item: U32,
    }

    let val = Container { item: U32(7) };

    let expected = &hex!("07");
    CXS::check_codec(&val, expected);

    let expected = &hex!("00000007");
    TXS::check_codec(&val, expected);
}

#[test]
fn ref_newtype() {
    #[derive(Debug, PartialEq, Eq, WriteXDR)]
    #[repr(transparent)]
    struct Buf([u8; 3]);

    impl Deref for Buf {
        type Target = [u8; 3];

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl<'a> From<&'a [u8; 3]> for &'a Buf {
        fn from(arr: &[u8; 3]) -> Self {
            // SAFETY: #[repr(transparent)] ensures these have the same representation
            let ptr = arr as *const [u8; 3] as *const Buf;
            unsafe { &*ptr }
        }
    }

    impl<'r: 'a, 'a> ReadXDR<'r> for &'a Buf {
        fn read_xdr<R: Reader<'r>>(r: &mut R) -> ReadResult<Self> {
            let arr: &[u8; 3] = read_xdr(r)?;
            let val = Self::from(arr);
            Ok(val)
        }
    }

    let arr = [0, 1, 2];
    let buf = Buf(arr);

    CXS::check_codec(&&buf, &hex!("000102"));
    TXS::check_codec(&&buf, &hex!("00010200"));

    // make sure we aren't being foxed out by auto(de)ref magic
    let expected = hex!("000102");
    let buf2: &Buf = xdrs::cxs::decode(&expected).unwrap();
    assert_eq!(&buf2.0, &arr);
}

#[test]
fn phantom_data() {
    #[derive(Debug, PartialEq, Eq)]
    struct Erik;

    #[derive(Debug, PartialEq, Eq, ReadXDR, WriteXDR)]
    struct Opera<T> {
        phantom: core::marker::PhantomData<T>,
        christine: u32,
    }

    let opera: Opera<Erik> = Opera {
        phantom: core::marker::PhantomData,
        christine: 5,
    };

    CXS::check_codec(&opera, &hex!("05"));
    TXS::check_codec(&opera, &hex!("00000005"));
}

#[test]
fn struct_with_tuple_field() {
    // Test struct with a simple tuple field
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Foo {
        x: (u32, u64),
    }

    let val = Foo { x: (42, 1000) };

    // u32: 42 (≤63, no prefix) and u64: 1000 (0x03E8, needs 0x7F+2-byte)
    // CXS: 42="2a", 1000="7fe803" (little-endian)
    let expected = &hex!("2a7fe803");
    CXS::check_codec(&val, expected);

    // TXS: traditional format - padded to 4-byte boundaries
    let expected = &hex!("0000002a00000000000003e8");
    TXS::check_codec(&val, expected);
}

#[test]
fn struct_with_mixed_fields_and_tuple() {
    // Test struct with multiple fields including a tuple
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Bar {
        id: u32,
        coords: (i32, i32),
        active: bool,
    }

    let val = Bar {
        id: 100,
        coords: (-10, 20),
        active: true,
    };

    // u32: 100 (0x64, range 32-255 needs 0x3F prefix), i32: -10 (compact: 63-10=53=0x35),
    // i32: 20 (0x14, single byte), bool: true (0x01)
    // CXS: 100="3f64", -10="35", 20="14", true="01"
    let expected = &hex!("3f64351401");
    CXS::check_codec(&val, expected);

    // TXS: traditional format
    let expected = &hex!("00000064fffffff60000001400000001");
    TXS::check_codec(&val, expected);
}

#[test]
fn struct_with_nested_tuple() {
    // Test struct with a nested tuple
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Nested {
        data: (u32, (i32, bool)),
    }

    let val = Nested {
        data: (5, (-3, false)),
    };

    // u32: 5 (0x05), i32: -3 (compact: 63-3=60=0x3C), bool: false (0x00)
    // CXS: 5="05", -3="3c", false="00"
    let expected = &hex!("053c00");
    CXS::check_codec(&val, expected);

    // TXS: traditional format
    let expected = &hex!("00000005fffffffd00000000");
    TXS::check_codec(&val, expected);
}

#[test]
fn struct_with_different_tuple_types() {
    // Test struct with tuples of different primitive types
    #[derive(Debug, PartialEq, Eq, WriteXDR, ReadXDR)]
    struct Multi {
        pair1: (u32, u32),
        pair2: (i64, bool),
    }

    let val = Multi {
        pair1: (0xAABBCCDD, 0x11223344),
        pair2: (9223372036854775807, true), // i64::MAX
    };

    // u32: 0xAABBCCDD (4-byte: 0xBF+little-endian), u32: 0x11223344 (4-byte),
    // i64: 0x7FFFFFFFFFFFFFFF (8-byte: 0xFF+little-endian), bool: 0x01
    // CXS: 0xBF+DDCCBBAA, 0xBF+44332211, 0xFF+FFFFFFFFFFFFFF7F, 01
    let expected = &hex!("bfddccbbaabf44332211ffffffffffffffff7f01");
    CXS::check_codec(&val, expected);

    // TXS: traditional format
    let expected = &hex!("aabbccdd112233447fffffffffffffff00000001");
    TXS::check_codec(&val, expected);
}