structio 0.8.0

High performance JSON and BEVE for Rust structs. No dependencies, no proc-macros, no intermediate representation.
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Writing the aligned typed-array form.
//!
//! The form itself was always read, skipped, indexed and framed; what is new is
//! that this crate can write it. So the property that carries this file is that
//! turning it on changes the layout and nothing else: the same documents come
//! back as the same values, through the same readers, whatever offset an array
//! happens to land on.
//!
//! The layout claim is asserted directly rather than through a reader, because
//! a reader that ignored the padding would agree with one that got it right.
//! Every array here is written last in its document, so the payload starts at
//! `len() - elements * width` and its alignment can be read off the bytes.

use structio::beve::{self, Documents, Feed, Mode, Writer, header};
use structio::{
    Complex, ErrorCode, Matrix, MatrixLayout, append_beve_aligned, beve_to_json, from_beve,
    from_beve_at, read_beve_array_into, to_beve, to_beve_aligned, to_string, validate_beve,
};

#[derive(Default, Debug, PartialEq, Clone)]
struct Reading {
    sensor: String,
    ok: bool,
    samples: Vec<f64>,
}
structio::object!(Reading {
    sensor,
    ok,
    samples
});

fn reading() -> Reading {
    Reading {
        sensor: "thermocouple".into(),
        ok: true,
        samples: (0..37).map(|i| i as f64 * 0.5).collect(),
    }
}

/// Where an array's payload begins, given that it is the last thing in `doc`.
fn payload_start<T>(doc: &[u8], elements: usize) -> usize {
    doc.len() - elements * size_of::<T>()
}

// ---------------------------------------------------------------------------
// The form on the wire
// ---------------------------------------------------------------------------

#[test]
fn the_preamble_is_the_marker_the_element_header_the_count_and_the_padding() {
    let doc = to_beve_aligned(&vec![1.0f64, 2.0]);
    #[rustfmt::skip]
    assert_eq!(
        doc,
        [
            0x5c, // the aligned marker
            0x64, // a typed array of eight-byte floats
            0x08, // two elements
            0x04, // four bytes of padding, which lands the payload on 8
            0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0xf0, 0x3f, // 1.0
            0, 0, 0, 0, 0, 0, 0x00, 0x40, // 2.0
        ]
    );
    assert_eq!(from_beve::<Vec<f64>>(&doc).unwrap(), vec![1.0, 2.0]);
}

/// Write `values` at every offset a preamble in front of it can produce, and
/// require the payload to land on its element width each time.
fn lands_on_its_width<T>(values: Vec<T>)
where
    T: beve::Write + for<'de> beve::Read<'de> + Default + Clone + PartialEq + std::fmt::Debug,
{
    for shift in 0..40 {
        let doc = to_beve_aligned(&(vec![0u8; shift], values.clone()));
        let start = payload_start::<T>(&doc, values.len());
        assert_eq!(
            start % size_of::<T>(),
            0,
            "{} bytes wide, shifted by {shift}: payload at {start}",
            size_of::<T>()
        );
        let (_, back) = from_beve::<(Vec<u8>, Vec<T>)>(&doc).unwrap();
        assert_eq!(back, values);
    }
}

#[test]
fn the_payload_lands_on_a_multiple_of_its_element_width() {
    lands_on_its_width(vec![1u16, 2, 3]);
    lands_on_its_width(vec![1.5f32, 2.5]);
    lands_on_its_width(vec![-1i32, 2, 3, 4, 5]);
    lands_on_its_width(vec![1.0f64, 2.0, 3.0]);
    lands_on_its_width(vec![i128::MIN, i128::MAX]);
    lands_on_its_width(vec![7usize; 9]);
}

#[test]
fn an_array_with_no_elements_is_padded_the_same_way() {
    // Nothing to align, but the form is the form: a reader that computes a
    // pointer before it looks at the count must still be handed one.
    for shift in 0..40 {
        let doc = to_beve_aligned(&(vec![0u8; shift], Vec::<f64>::new()));
        assert_eq!(payload_start::<f64>(&doc, 0) % 8, 0);
        let (_, back) = from_beve::<(Vec<u8>, Vec<f64>)>(&doc).unwrap();
        assert!(back.is_empty());
    }
}

#[test]
fn only_numbers_wider_than_a_byte_change() {
    // Booleans and strings have no aligned form, one-byte elements are aligned
    // wherever they land, and a scalar is not an array at all.
    assert_eq!(
        to_beve(&vec![true, false]),
        to_beve_aligned(&vec![true, false])
    );
    assert_eq!(
        to_beve(&vec!["a".to_string(), "b".to_string()]),
        to_beve_aligned(&vec!["a".to_string(), "b".to_string()])
    );
    assert_eq!(to_beve(&vec![1u8, 2, 3]), to_beve_aligned(&vec![1u8, 2, 3]));
    assert_eq!(to_beve(&vec![-1i8, 2]), to_beve_aligned(&vec![-1i8, 2]));
    assert_eq!(to_beve(&1.0f64), to_beve_aligned(&1.0f64));
    assert_eq!(to_beve(&"text"), to_beve_aligned(&"text"));
}

#[test]
fn a_complex_array_takes_the_extension_s_aligned_form() {
    // The specification's own example: two `complex128` values at the start
    // of a message. The class header says "aligned" in its low bits, and an
    // aligned typed array of the four components follows it, padded so the
    // payload begins at 8.
    let signal = vec![Complex::new(1.0f64, 2.0), Complex::new(3.0, 4.0)];
    let doc = to_beve_aligned(&signal);
    #[rustfmt::skip]
    assert_eq!(
        doc,
        [
            0x1e, // the complex extension
            0x62, // eight-byte floats, in the aligned form
            0x5c, // the aligned marker
            0x64, // a typed array of eight-byte floats
            0x10, // four components
            0x02, // two bytes of padding, which lands the payload on 8
            0, 0,
            0, 0, 0, 0, 0, 0, 0xf0, 0x3f, // 1.0
            0, 0, 0, 0, 0, 0, 0x00, 0x40, // 2.0
            0, 0, 0, 0, 0, 0, 0x08, 0x40, // 3.0
            0, 0, 0, 0, 0, 0, 0x10, 0x40, // 4.0
        ]
    );
    assert_eq!(from_beve::<Vec<Complex<f64>>>(&doc).unwrap(), signal);
}

#[test]
fn a_complex_array_of_one_byte_components_keeps_the_plain_form() {
    // One-byte components are aligned wherever they land, as one-byte
    // elements are, so the aligned form would spend four bytes on nothing.
    let bytes = vec![Complex::new(1u8, 2), Complex::new(3, 4)];
    assert_eq!(to_beve(&bytes), to_beve_aligned(&bytes));
    let signed = vec![Complex::new(-1i8, 2)];
    assert_eq!(to_beve(&signed), to_beve_aligned(&signed));
    // And a lone complex number is not an array at all.
    let one = Complex::new(1.0f64, 2.0);
    assert_eq!(to_beve(&one), to_beve_aligned(&one));
}

/// Write `values` at every offset a preamble in front of it can produce, and
/// require the components to land on their width each time.
fn complex_lands_on_its_width<T>(values: Vec<Complex<T>>)
where
    Complex<T>: beve::Write + for<'de> beve::Read<'de> + Default + Clone + PartialEq,
    T: std::fmt::Debug,
{
    for shift in 0..40 {
        let doc = to_beve_aligned(&(vec![0u8; shift], values.clone()));
        let start = payload_start::<Complex<T>>(&doc, values.len());
        assert_eq!(
            start % size_of::<T>(),
            0,
            "{} bytes wide, shifted by {shift}: payload at {start}",
            size_of::<T>()
        );
        // The count and the element type are the inner array's, which says
        // components rather than pairs.
        let (_, back) = from_beve::<(Vec<u8>, Vec<Complex<T>>)>(&doc).unwrap();
        assert_eq!(back, values);
        validate_beve(&doc).unwrap();
        assert_eq!(
            beve_to_json(&doc).unwrap(),
            beve_to_json(&to_beve(&(vec![0u8; shift], values.clone()))).unwrap()
        );
    }
}

#[test]
fn a_complex_payload_lands_on_a_multiple_of_its_component_width() {
    complex_lands_on_its_width(vec![Complex::new(1.5f32, -2.5), Complex::new(0.0, 1.0)]);
    complex_lands_on_its_width(vec![Complex::new(1.0f64, 2.0); 3]);
    complex_lands_on_its_width(vec![Complex::new(-1i16, 2), Complex::new(3, -4)]);
    complex_lands_on_its_width(vec![Complex::new(1u16, 2)]);
    complex_lands_on_its_width(vec![Complex::new(-1i32, i32::MAX)]);
    complex_lands_on_its_width(vec![Complex::new(7u32, 8); 5]);
    complex_lands_on_its_width(vec![Complex::new(i64::MIN, i64::MAX)]);
    complex_lands_on_its_width(vec![Complex::new(u64::MAX, 0)]);
    complex_lands_on_its_width(vec![Complex::new(i128::MIN, i128::MAX)]);
    complex_lands_on_its_width(vec![Complex::new(u128::MAX, 1)]);
    complex_lands_on_its_width(Vec::<Complex<f64>>::new());
}

// ---------------------------------------------------------------------------
// The same documents, read the same ways
// ---------------------------------------------------------------------------

#[test]
fn an_element_read_widens_from_the_padded_payload() {
    // The bulk path takes the payload whole; a stored width the destination
    // does not share walks it element by element, from the same place.
    let doc = to_beve_aligned(&(vec![0u8; 3], vec![1u16, 2, 3]));
    let (_, back) = from_beve::<(Vec<u8>, Vec<u64>)>(&doc).unwrap();
    assert_eq!(back, vec![1u64, 2, 3]);
}

#[test]
fn a_pointer_indexes_into_a_padded_payload() {
    let value = reading();
    let doc = to_beve_aligned(&value);
    for (i, want) in value.samples.iter().enumerate() {
        let at = format!("/samples/{i}");
        assert_eq!(from_beve_at::<f64>(&doc, &at).unwrap(), *want);
    }
}

/// Append `values` behind prefixes of every length up to twice the widest
/// element, and require every walk to read it back from where it starts.
///
/// The padding is chosen against the whole buffer, so the array read on its
/// own is padded for an offset its reader cannot see. What every reader
/// requires of the padding is that it be shorter than the element is wide,
/// which the writer never breaks, wherever the array lands.
fn reads_wherever_it_starts<T>(values: Vec<T>)
where
    T: beve::Write
        + for<'de> beve::Read<'de>
        + beve::NumericBytes
        + Default
        + Clone
        + structio::json::Write
        + PartialEq
        + std::fmt::Debug,
{
    let width = size_of::<T>();
    for prefix in 0..32 {
        let mut buf = vec![0xEE; prefix];
        append_beve_aligned(&values, &mut buf);
        let doc = &buf[prefix..];
        let label = format!("{width} bytes wide, behind {prefix}");
        // The marker, the element header, a one-byte count, then the length.
        assert_eq!(doc[0], header::ALIGNED_ARRAY, "{label}");
        assert!(usize::from(doc[3]) < width, "{label}: padding {}", doc[3]);

        assert_eq!(from_beve::<Vec<T>>(doc).unwrap(), values, "{label}");
        validate_beve(doc).unwrap_or_else(|e| panic!("{label}: {e:?}"));
        assert_eq!(beve_to_json(doc).unwrap(), to_string(&values), "{label}");
        let last = values.len() - 1;
        let at = format!("/{last}");
        assert_eq!(
            from_beve_at::<T>(doc, &at).unwrap(),
            values[last],
            "{label}"
        );
        let mut streamed = Vec::<T>::new();
        read_beve_array_into(&mut streamed, doc).unwrap();
        assert_eq!(streamed, values, "{label}");
        let framed: Vec<T> = Documents::array(doc)
            .iter::<T>()
            .map(Result::unwrap)
            .collect();
        assert_eq!(framed, values, "{label}");
    }
}

#[test]
fn a_canonical_array_reads_wherever_it_starts() {
    reads_wherever_it_starts(vec![1u16, 2, 3]);
    reads_wherever_it_starts(vec![-1i16, 2]);
    reads_wherever_it_starts(vec![1.5f32, 2.5]);
    reads_wherever_it_starts(vec![-1i32, 2, 3, 4, 5]);
    reads_wherever_it_starts(vec![7u32; 3]);
    reads_wherever_it_starts(vec![1.0f64, 2.0, 3.0]);
    reads_wherever_it_starts(vec![u64::MAX, 0]);
    reads_wherever_it_starts(vec![i64::MIN, 1]);
    reads_wherever_it_starts(vec![i128::MIN, i128::MAX]);
    reads_wherever_it_starts(vec![u128::MAX, 1]);
}

/// [`reads_wherever_it_starts`] for a run of complex numbers, whose padding is
/// held below a component's width rather than a pair's.
fn complex_reads_wherever_it_starts<T>(values: Vec<Complex<T>>)
where
    Complex<T>: beve::Write
        + for<'de> beve::Read<'de>
        + beve::NumericBytes
        + Default
        + Clone
        + structio::json::Write
        + PartialEq
        + std::fmt::Debug,
{
    let width = size_of::<T>();
    for prefix in 0..32 {
        let mut buf = vec![0xEE; prefix];
        append_beve_aligned(&values, &mut buf);
        let doc = &buf[prefix..];
        let label = format!("{width} bytes a component, behind {prefix}");
        // The extension, the class, the marker, the element header, a one-byte
        // count, then the length.
        assert_eq!(doc[1] & 0b111, header::COMPLEX_ALIGNED, "{label}");
        assert_eq!(doc[2], header::ALIGNED_ARRAY, "{label}");
        assert!(usize::from(doc[5]) < width, "{label}: padding {}", doc[5]);

        assert_eq!(
            from_beve::<Vec<Complex<T>>>(doc).unwrap(),
            values,
            "{label}"
        );
        validate_beve(doc).unwrap_or_else(|e| panic!("{label}: {e:?}"));
        assert_eq!(beve_to_json(doc).unwrap(), to_string(&values), "{label}");
        let mut streamed = Vec::<Complex<T>>::new();
        read_beve_array_into(&mut streamed, doc).unwrap();
        assert_eq!(streamed, values, "{label}");
        let framed: Vec<Complex<T>> = Documents::array(doc)
            .iter::<Complex<T>>()
            .map(Result::unwrap)
            .collect();
        assert_eq!(framed, values, "{label}");
    }
}

#[test]
fn a_canonical_complex_array_reads_wherever_it_starts() {
    complex_reads_wherever_it_starts(vec![Complex::new(1u16, 2), Complex::new(3, 4)]);
    complex_reads_wherever_it_starts(vec![Complex::new(1.5f32, -2.5)]);
    complex_reads_wherever_it_starts(vec![Complex::new(-1i32, 2); 3]);
    complex_reads_wherever_it_starts(vec![Complex::new(1.0f64, 2.0), Complex::new(3.0, 4.0)]);
    complex_reads_wherever_it_starts(vec![Complex::new(u64::MAX, 0)]);
    complex_reads_wherever_it_starts(vec![Complex::new(i128::MIN, i128::MAX)]);
}

#[test]
fn it_validates_and_transcodes_to_the_json_the_typed_writer_would_produce() {
    let value = reading();
    let doc = to_beve_aligned(&value);
    assert_eq!(payload_start::<f64>(&doc, value.samples.len()) % 8, 0);
    assert_eq!(from_beve::<Reading>(&doc).unwrap(), value);
    validate_beve(&doc).unwrap();
    assert_eq!(beve_to_json(&doc).unwrap(), to_string(&value));
}

#[test]
fn it_frames_as_one_value_however_the_bytes_arrive() {
    let doc = to_beve_aligned(&reading());
    let mut feed = Feed::new(Mode::Values);
    let mut got = Vec::new();
    for &b in &doc {
        feed.push(&[b]);
        while let Some(v) = feed.next_value::<Reading>() {
            got.push(v.unwrap());
        }
    }
    feed.end();
    assert_eq!(got, vec![reading()]);
}

#[test]
fn a_stream_hands_out_its_elements_one_at_a_time() {
    let samples: Vec<f64> = (0..64).map(|i| i as f64).collect();
    let doc = to_beve_aligned(&samples);
    let mut docs = Documents::array(&doc[..]);
    let got: Vec<f64> = docs.iter::<f64>().map(Result::unwrap).collect();
    assert_eq!(got, samples);
}

#[derive(Default, Debug, PartialEq, Clone)]
struct Vec3 {
    x: f64,
    y: f64,
    z: f64,
}
structio::array!(Vec3 [f64; x, y, z]);

#[test]
fn a_struct_stored_as_a_typed_array_is_padded_like_one() {
    // Naming an element type says the struct is stored the way a run of that
    // type is, which has to keep holding once the run is padded.
    let v = Vec3 {
        x: 1.5,
        y: -2.0,
        z: 3.25,
    };
    assert_eq!(to_beve_aligned(&v), to_beve_aligned(&[1.5f64, -2.0, 3.25]));
    for shift in 0..24 {
        let doc = to_beve_aligned(&(vec![0u8; shift], v.clone()));
        assert_eq!(payload_start::<f64>(&doc, 3) % 8, 0, "shifted by {shift}");
        assert_eq!(from_beve::<(Vec<u8>, Vec3)>(&doc).unwrap().1, v);
    }
}

#[test]
fn the_block_is_still_taken_in_one_copy() {
    // The form exists so that a reader can take the payload whole. A reader
    // that declined it would still be correct and would read the fastest thing
    // in the format the slowest way there is, one element at a time, so this
    // asks the bulk path directly rather than inferring it from a value.
    //
    // Big-endian is where declining *is* the right answer, and the assertion
    // follows the target rather than being skipped on one: the payload is
    // little-endian, so a block taken whole would hand back byte-swapped
    // numbers. There the contract is the other half of `try_bulk`'s promise --
    // it says no, consumes nothing, and leaves the ordinary path to produce the
    // values -- so that is what gets checked instead of nothing at all.
    let samples = vec![1.5f64; 1000];
    for doc in [to_beve(&samples), to_beve_aligned(&samples)] {
        let mut r = beve::Reader::new(&doc);
        let mut out: Vec<f64> = Vec::new();
        let taken = r.try_bulk(&mut out).unwrap();
        assert_eq!(
            taken,
            cfg!(target_endian = "little"),
            "the bulk path is taken on little-endian and declined on big-endian"
        );
        if taken {
            assert_eq!(out, samples);
        } else {
            assert!(out.is_empty(), "declined but pushed elements anyway");
        }
        // Either way the document reads correctly, which is what declining is
        // allowed to cost: speed, never an answer.
        assert_eq!(from_beve::<Vec<f64>>(&doc).unwrap(), samples);
    }
}

#[test]
fn a_bool_array_is_not_an_aligned_one_however_its_bytes_read() {
    // Booleans, strings and the aligned form share a category and are told
    // apart by the byte-count field alone. The bulk path has to check it: this
    // document is a bool array whose following bytes happen to spell an f64
    // array's aligned preamble, and taking those at face value would hand back
    // two numbers that are not in the document at all.
    let mut doc = vec![header::BOOL_ARRAY, 0x64, 0x08, 0x00];
    doc.extend_from_slice(&1.0f64.to_le_bytes());
    doc.extend_from_slice(&2.0f64.to_le_bytes());
    assert_eq!(
        from_beve::<Vec<f64>>(&doc).unwrap_err().code,
        ErrorCode::ExpectedNumber
    );
}

/// Bits rather than values: no NaN equals itself, and the payload is exactly
/// what is in question here.
fn bits32(v: &[f32]) -> Vec<u32> {
    v.iter().map(|x| x.to_bits()).collect()
}

fn bits64(v: &[f64]) -> Vec<u64> {
    v.iter().map(|x| x.to_bits()).collect()
}

#[test]
fn a_float_keeps_its_exact_bits_in_either_form() {
    // A signalling NaN is what separates taking the payload whole from
    // converting element by element: the conversion quiets it.
    let f32s: Vec<f32> = [
        0x7f80_0001u32,
        0x7fc0_1234,
        0xffc0_0001,
        0x0000_0001,
        0x7f80_0000,
    ]
    .iter()
    .map(|&b| f32::from_bits(b))
    .collect();
    let f64s: Vec<f64> = [
        0x7ff0_0000_0000_0001u64,
        0xfff8_0000_dead_beef,
        0x0000_0000_0000_0001,
    ]
    .iter()
    .map(|&b| f64::from_bits(b))
    .collect();
    for doc in [to_beve(&f32s), to_beve_aligned(&f32s)] {
        assert_eq!(bits32(&from_beve::<Vec<f32>>(&doc).unwrap()), bits32(&f32s));
    }
    for doc in [to_beve(&f64s), to_beve_aligned(&f64s)] {
        assert_eq!(bits64(&from_beve::<Vec<f64>>(&doc).unwrap()), bits64(&f64s));
    }
    // A field reads one number rather than a block, and the two must not
    // disagree about what came off the wire.
    for &v in &f32s {
        assert_eq!(
            from_beve::<f32>(&to_beve(&v)).unwrap().to_bits(),
            v.to_bits()
        );
    }
    for &v in &f64s {
        assert_eq!(
            from_beve::<f64>(&to_beve(&v)).unwrap().to_bits(),
            v.to_bits()
        );
    }
}

// ---------------------------------------------------------------------------
// Matrices
// ---------------------------------------------------------------------------

#[test]
fn a_matrix_pads_the_data_it_holds() {
    let m = Matrix::new(
        MatrixLayout::RowMajor,
        vec![2, 3],
        (0..6).map(f64::from).collect(),
    )
    .unwrap();
    let doc = to_beve_aligned(&m);
    assert_eq!(payload_start::<f64>(&doc, 6) % 8, 0);
    assert_eq!(from_beve::<Matrix<f64>>(&doc).unwrap(), m);
    validate_beve(&doc).unwrap();
    assert_eq!(beve_to_json(&doc).unwrap(), to_string(&m));
}

#[test]
fn a_matrix_of_complex_numbers_pads_the_data_it_holds() {
    let m = Matrix::new(
        MatrixLayout::ColumnMajor,
        vec![1, 2],
        vec![Complex::new(1.0f64, 2.0), Complex::new(3.0, -4.0)],
    )
    .unwrap();
    // Its extents are one byte wide and keep the plain form; its data is a
    // complex array, which takes the aligned one.
    let doc = to_beve_aligned(&m);
    assert_ne!(doc, to_beve(&m));
    assert_eq!(payload_start::<Complex<f64>>(&doc, 2) % 8, 0);
    assert_eq!(from_beve::<Matrix<Complex<f64>>>(&doc).unwrap(), m);
    validate_beve(&doc).unwrap();
    assert_eq!(beve_to_json(&doc).unwrap(), to_string(&m));
}

#[test]
fn wide_extents_are_padded_like_any_other_numbers() {
    let m = Matrix::new(MatrixLayout::RowMajor, vec![70_000, 1], vec![0u8; 70_000]).unwrap();
    let doc = to_beve_aligned(&m);
    assert_eq!(from_beve::<Matrix<u8>>(&doc).unwrap(), m);
    // The extension header, the layout byte, then the extents: the aligned
    // marker, a typed array of four-byte unsigned integers, a count of two,
    // and the padding that lands the two extents on a multiple of four.
    assert_eq!(&doc[..4], &[0x16, 0x00, 0x5c, 0x54]);
    let padding = doc[5] as usize;
    assert_eq!((6 + padding) % 4, 0);
}

// ---------------------------------------------------------------------------
// The offset is the document's, not the buffer's
// ---------------------------------------------------------------------------

#[test]
fn a_sink_writer_pads_against_where_the_document_started() {
    // The buffer holds only the tail once it has drained, so a writer that
    // measured padding from the buffer would go wrong as soon as it did.
    let value = reading();
    let want = to_beve_aligned(&value);
    for capacity in [1usize, 2, 3, 7, 8, 16, 17, 64, 4096] {
        let mut out = Vec::new();
        let mut w =
            Writer::<structio::Standard>::to_sink_with_capacity(&mut out, capacity).aligned();
        beve::Write::write(&value, &mut w);
        w.finish().unwrap();
        assert_eq!(out, want, "capacity {capacity}");
    }
}

#[test]
fn a_block_handed_straight_to_the_sink_is_still_counted() {
    // A payload at least as large as the whole buffer bypasses it, and the
    // arrays after it still have to be padded against the right offset.
    let value = (vec![0u8; 4096], vec![1.0f64; 512], vec![2.0f32; 3]);
    let want = to_beve_aligned(&value);
    let mut out = Vec::new();
    let mut w = Writer::<structio::Standard>::to_sink_with_capacity(&mut out, 64).aligned();
    beve::Write::write(&value, &mut w);
    w.finish().unwrap();
    assert_eq!(out, want);
    assert_eq!(payload_start::<f32>(&want, 3) % 4, 0);
    assert_eq!(
        from_beve::<(Vec<u8>, Vec<f64>, Vec<f32>)>(&out).unwrap(),
        value
    );
}