revision 0.20.0

A serialization and deserialization implementation which allows for schema-evolution.
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! Structured, selective decoding of revisioned encodings.
//!
//! The [`WalkRevisioned`] trait lets callers progress element-by-element through
//! revisioned bytes, deciding per-element whether to **decode**, **skip**, or
//! **walk into** further structure. This sits between [`DeserializeRevisioned`]
//! (decode everything into a value) and [`SkipRevisioned`] (consume bytes
//! without producing a value).
//!
//! [`DeserializeRevisioned`]: crate::DeserializeRevisioned
//! [`SkipRevisioned`]: crate::SkipRevisioned
//!
//! # Shapes
//!
//! Walkers are shape-specific:
//!
//! - [`LeafWalker`] — primitives that have no internal structure (e.g.
//!   `u32`, `String`); only `decode` or `skip` are meaningful.
//! - [`OptionWalker`] — `Option<T>`; peek the tag, then optionally walk into
//!   the payload.
//! - [`ResultWalker`] — `Result<T, E>`; peek the variant, then walk into
//!   either side.
//! - [`SeqWalker`] — homogeneous sequences (`Vec`, `HashSet`, …); iterate
//!   items, skip/decode/walk per item.
//! - [`MapWalker`] — keyed sequences (`HashMap`, `BTreeMap`, …); iterate
//!   entries, skip/decode key + value individually.
//! - Per-type walkers generated by `#[revisioned(...)]` for structs/enums.
//!
//! # Wire format invariants
//!
//! - `#[revisioned(...)]` types prefix their body with a `u16` revision header.
//! - Enums then encode a `u32` variant discriminant.
//! - Sequences encode `usize len` then elements.
//! - Maps encode `usize len` then `(key, value)` pairs.
//!
//! Walkers honour these invariants and read the relevant prefix in
//! `walk_revisioned` before returning so the caller can step over the body.

use std::cmp::Ordering;
use std::io::Read;
use std::marker::PhantomData;

use crate::slice_reader::BorrowedReader;
use crate::{DeserializeRevisioned, Error, Revisioned, SkipRevisioned};

// -----------------------------------------------------------------------------
// Trait
// -----------------------------------------------------------------------------

/// Structured, element-by-element traversal of revisioned bytes.
///
/// `walk_revisioned` consumes the revision-level prefix (e.g. the `u16`
/// revision tag emitted by the derive macro for `#[revisioned(...)]` types)
/// and returns a shape-specific walker positioned at the start of the body.
///
/// The associated walker type carries `&'r mut R` and any decoding state. Once
/// dropped (or `finish`-ed), the reader is positioned past the encoded value.
///
/// # Example
///
/// ```ignore
/// use revision::{DeserializeRevisioned, WalkRevisioned, prelude::*};
///
/// let mut reader = bytes.as_slice();
/// let mut entries = <std::collections::BTreeMap<String, u32>>::walk_revisioned(&mut reader)?;
/// while let Some(mut entry) = entries.next_entry()? {
///     let key: String = entry.decode_key()?;
///     if key == "score" {
///         let value: u32 = entry.decode_value()?;
///         println!("score = {}", value);
///     } else {
///         entry.skip_value()?;
///     }
/// }
/// ```
pub trait WalkRevisioned: Revisioned {
	/// Walker produced by [`walk_revisioned`](Self::walk_revisioned).
	///
	/// The walker borrows the reader for `'r` and exposes shape-specific
	/// progression methods.
	type Walker<'r, R: Read + 'r>;

	/// Read the type's revision-level prefix and return a walker positioned at
	/// the start of the body.
	fn walk_revisioned<'r, R: Read>(reader: &'r mut R) -> Result<Self::Walker<'r, R>, Error>;
}

// -----------------------------------------------------------------------------
// Zero-copy peek primitives
// -----------------------------------------------------------------------------

/// Marker for revisioned types whose wire format is `usize len || raw bytes`.
///
/// Types implementing this trait can have their wire payload **peeked as
/// `&[u8]`** from a [`BorrowedReader`] without allocating. Walker methods
/// such as [`LeafWalker::with_bytes`], [`MapWalker::find_bytes`],
/// [`MapEntry::with_key_bytes`] / [`MapEntry::with_value_bytes`], and
/// [`SeqItem::with_bytes`] are gated on this trait so callers can compare,
/// hash, or copy the wire bytes without paying for an intermediate
/// `String` / `Vec<u8>` / `Bytes`.
///
/// The trait does **not** apply to derived `#[revisioned(...)]` types,
/// which prepend a `u16` revision header to their body. Only types whose
/// `SerializeRevisioned` impl writes `usize` then raw bytes qualify; this
/// includes the standard string- and bytes-shaped types
/// (`String`, `&str`, `Box<str>`, `Arc<str>`, `Cow<'_, str>`, `Vec<u8>`,
/// `Vec<i8>`, `PathBuf`) and feature-gated types like `bytes::Bytes`.
///
/// Downstream crates can opt their newtypes in by adding a one-line impl
/// (e.g. `impl LengthPrefixedBytes for MyId {}`) when the newtype's
/// `SerializeRevisioned` ultimately writes `usize len || raw bytes`.
///
/// Marking a type incorrectly — when its encoding is **not** exactly that
/// layout — will mis-parse under [`LeafWalker::with_bytes`],
/// [`MapWalker::find_bytes`], and related helpers; the trait is a **trusted**
/// wire-shape contract, not validated at runtime beyond normal deserialization.
/// That is the same class of hazard as an incorrect manual [`Revisioned`]
/// implementation: the library cannot prove the marker matches the bytes.
pub trait LengthPrefixedBytes: Revisioned {}

impl LengthPrefixedBytes for String {}
// `str` is `?Sized`, so a `LeafWalker<'_, str, R>` cannot exist in
// practice — this impl is here so the `Cow<'_, T>` blanket impl below
// can apply to `Cow<'_, str>` (whose `T::Owned` is `String`).
impl LengthPrefixedBytes for str {}
impl LengthPrefixedBytes for std::sync::Arc<str> {}
impl LengthPrefixedBytes for Box<str> {}
impl LengthPrefixedBytes for std::path::PathBuf {}
impl LengthPrefixedBytes for Vec<u8> {}
impl LengthPrefixedBytes for Vec<i8> {}

impl<T> LengthPrefixedBytes for std::borrow::Cow<'_, T>
where
	T: ToOwned + Revisioned,
	T::Owned: LengthPrefixedBytes,
{
}

#[cfg(feature = "bytes")]
impl LengthPrefixedBytes for bytes::Bytes {}

/// Peek a length-prefixed byte payload at the reader's current position
/// and pass it to `f` as a borrowed slice; advance the reader past the
/// bytes once `f` returns.
///
/// Used by [`LeafWalker::with_bytes`], [`MapEntry::with_key_bytes`] /
/// [`with_value_bytes`](MapEntry::with_value_bytes), [`SeqItem::with_bytes`],
/// and [`MapWalker::find_bytes`]. Each of those methods is just a thin
/// wrapper around this helper plus the appropriate cursor / counter
/// bookkeeping.
///
/// **Panic safety:** if `f` panics, the reader is left positioned between
/// the consumed length prefix and the un-advanced body bytes. The reader is
/// in a corrupt state and should not be reused after the unwind. In the
/// usual case where a panic ends use of the reader this does not matter.
#[inline]
pub(crate) fn with_length_prefixed_bytes<R, F, T>(reader: &mut R, f: F) -> Result<T, Error>
where
	R: BorrowedReader,
	F: FnOnce(&[u8]) -> T,
{
	let len = usize::deserialize_revisioned(reader)?;
	let result = {
		let bytes = reader.peek_bytes(len)?;
		f(bytes)
	};
	reader.advance(len)?;
	Ok(result)
}

// -----------------------------------------------------------------------------
// LeafWalker — primitives and length-prefixed scalars
// -----------------------------------------------------------------------------

/// Walker for a primitive (or otherwise structurally opaque) revisioned type.
///
/// Only `decode` and `skip` are meaningful; there is no internal structure to
/// step through.
pub struct LeafWalker<'r, T, R: Read + 'r> {
	reader: &'r mut R,
	_marker: PhantomData<fn() -> T>,
}

impl<'r, T, R: Read + 'r> LeafWalker<'r, T, R> {
	/// Construct a leaf walker; intended for use by [`WalkRevisioned`] impls.
	#[inline]
	pub fn new(reader: &'r mut R) -> Self {
		Self {
			reader,
			_marker: PhantomData,
		}
	}

	/// Decode the value, advancing the reader past it.
	#[inline]
	pub fn decode(self) -> Result<T, Error>
	where
		T: DeserializeRevisioned,
	{
		T::deserialize_revisioned(self.reader)
	}

	/// Skip the value, advancing the reader past it.
	#[inline]
	pub fn skip(self) -> Result<(), Error>
	where
		T: SkipRevisioned,
	{
		T::skip_revisioned(self.reader)
	}

	/// Walk into the value as a structured walker. Equivalent to calling
	/// `T::walk_revisioned` directly on the underlying reader; provided so
	/// `LeafWalker` doubles as a "pre-walk handle" that lets the caller
	/// choose between decoding, skipping, or descending without committing
	/// up front.
	#[inline]
	pub fn walk(self) -> Result<T::Walker<'r, R>, Error>
	where
		T: WalkRevisioned,
	{
		T::walk_revisioned(self.reader)
	}

	/// Borrow the underlying reader. Useful for hand-rolled walkers that need
	/// to read auxiliary state before continuing.
	#[inline]
	pub fn reader(&mut self) -> &mut R {
		self.reader
	}

	/// Surrender the underlying reader without consuming the value.
	#[inline]
	pub fn into_reader(self) -> &'r mut R {
		self.reader
	}
}

impl<'r, T, R> LeafWalker<'r, T, R>
where
	T: LengthPrefixedBytes,
	R: BorrowedReader + 'r,
{
	/// Inspect the leaf's wire bytes without allocating, calling `f` with
	/// a slice borrowed from the underlying buffer; the reader is
	/// advanced past the bytes after `f` returns.
	///
	/// Only available when the underlying reader is slice-backed
	/// ([`BorrowedReader`]) and the value's wire format is exactly
	/// `usize len || raw bytes` ([`LengthPrefixedBytes`]). For decoded
	/// access use [`decode`](Self::decode); for fall-through advancement
	/// use [`skip`](Self::skip).
	#[inline]
	pub fn with_bytes<F, U>(self, f: F) -> Result<U, Error>
	where
		F: FnOnce(&[u8]) -> U,
	{
		with_length_prefixed_bytes(self.reader, f)
	}
}

// -----------------------------------------------------------------------------
// OptionWalker — Option<T>
// -----------------------------------------------------------------------------

/// Walker for `Option<T>`.
///
/// The wire format is a `u8` tag (`0` = `None`, `1` = `Some`) followed by an
/// inner `T` payload when present.
pub struct OptionWalker<'r, T, R: Read + 'r> {
	reader: &'r mut R,
	tag: u8,
	_marker: PhantomData<fn() -> T>,
}

impl<'r, T, R: Read + 'r> OptionWalker<'r, T, R> {
	/// Construct an option walker. Reads the `u8` tag from `reader`.
	#[inline]
	pub fn new(reader: &'r mut R) -> Result<Self, Error> {
		let tag = u8::deserialize_revisioned(reader)?;
		match tag {
			0 | 1 => Ok(Self {
				reader,
				tag,
				_marker: PhantomData,
			}),
			v => Err(Error::Deserialize(format!("Invalid option value {v}"))),
		}
	}

	/// `true` when the value is `Some(_)` and the inner walker can be entered.
	#[inline]
	pub fn is_some(&self) -> bool {
		self.tag == 1
	}

	/// `true` when the value is `None`.
	#[inline]
	pub fn is_none(&self) -> bool {
		self.tag == 0
	}

	/// Decode the value, advancing the reader past it.
	#[inline]
	pub fn decode(self) -> Result<Option<T>, Error>
	where
		T: DeserializeRevisioned,
	{
		match self.tag {
			0 => Ok(None),
			1 => Ok(Some(T::deserialize_revisioned(self.reader)?)),
			v => Err(Error::Deserialize(format!("Invalid option value {v}"))),
		}
	}

	/// Skip the value, advancing the reader past it.
	#[inline]
	pub fn skip(self) -> Result<(), Error>
	where
		T: SkipRevisioned,
	{
		if self.tag == 1 {
			T::skip_revisioned(self.reader)?;
		}
		Ok(())
	}

	/// Walk into the `Some(_)` payload. Returns an error if the value is `None`.
	#[inline]
	pub fn into_some(self) -> Result<T::Walker<'r, R>, Error>
	where
		T: WalkRevisioned,
	{
		match self.tag {
			0 => Err(Error::Deserialize("Cannot walk into None payload".into())),
			1 => T::walk_revisioned(self.reader),
			v => Err(Error::Deserialize(format!("Invalid option value {v}"))),
		}
	}
}

// -----------------------------------------------------------------------------
// ResultWalker — Result<T, E>
// -----------------------------------------------------------------------------

/// Walker for `Result<T, E>`.
///
/// The wire format is a `u32` tag (`0` = `Ok(T)`, `1` = `Err(E)`) followed by
/// the corresponding payload.
pub struct ResultWalker<'r, T, E, R: Read + 'r> {
	reader: &'r mut R,
	tag: u32,
	_marker: PhantomData<fn() -> Result<T, E>>,
}

impl<'r, T, E, R: Read + 'r> ResultWalker<'r, T, E, R> {
	/// Construct a result walker. Reads the `u32` variant tag from `reader`.
	#[inline]
	pub fn new(reader: &'r mut R) -> Result<Self, Error> {
		let tag = u32::deserialize_revisioned(reader)?;
		match tag {
			0 | 1 => Ok(Self {
				reader,
				tag,
				_marker: PhantomData,
			}),
			v => Err(Error::Deserialize(format!("Unknown Result variant index {v}"))),
		}
	}

	/// `true` when the value is `Ok(_)`.
	#[inline]
	pub fn is_ok(&self) -> bool {
		self.tag == 0
	}

	/// `true` when the value is `Err(_)`.
	#[inline]
	pub fn is_err(&self) -> bool {
		self.tag == 1
	}

	/// Decode the value, advancing the reader past it.
	#[inline]
	pub fn decode(self) -> Result<Result<T, E>, Error>
	where
		T: DeserializeRevisioned,
		E: DeserializeRevisioned,
	{
		match self.tag {
			0 => Ok(Ok(T::deserialize_revisioned(self.reader)?)),
			1 => Ok(Err(E::deserialize_revisioned(self.reader)?)),
			v => Err(Error::Deserialize(format!("Unknown Result variant index {v}"))),
		}
	}

	/// Skip the value, advancing the reader past it.
	#[inline]
	pub fn skip(self) -> Result<(), Error>
	where
		T: SkipRevisioned,
		E: SkipRevisioned,
	{
		match self.tag {
			0 => T::skip_revisioned(self.reader),
			1 => E::skip_revisioned(self.reader),
			v => Err(Error::Deserialize(format!("Unknown Result variant index {v}"))),
		}
	}

	/// Walk into the `Ok(_)` payload. Errors if the value is `Err`.
	#[inline]
	pub fn into_ok(self) -> Result<T::Walker<'r, R>, Error>
	where
		T: WalkRevisioned,
	{
		match self.tag {
			0 => T::walk_revisioned(self.reader),
			1 => Err(Error::Deserialize("Cannot walk into Err as Ok".into())),
			v => Err(Error::Deserialize(format!("Unknown Result variant index {v}"))),
		}
	}

	/// Walk into the `Err(_)` payload. Errors if the value is `Ok`.
	#[inline]
	pub fn into_err(self) -> Result<E::Walker<'r, R>, Error>
	where
		E: WalkRevisioned,
	{
		match self.tag {
			0 => Err(Error::Deserialize("Cannot walk into Ok as Err".into())),
			1 => E::walk_revisioned(self.reader),
			v => Err(Error::Deserialize(format!("Unknown Result variant index {v}"))),
		}
	}
}

// -----------------------------------------------------------------------------
// SeqWalker — Vec<T>, HashSet<T>, BTreeSet<T>, BinaryHeap<T>, …
// -----------------------------------------------------------------------------

/// Walker for a homogeneous sequence (length-prefix followed by `T` elements).
///
/// All collections whose [`SerializeRevisioned`] writes `usize len || T per
/// element` use this walker — `HashSet<T>`, `BTreeSet<T>`, `BinaryHeap<T>`,
/// `imbl::{Vector, OrdSet, HashSet}`, and `Vec<T>` for non-bulk element
/// types.
///
/// **Caveat for `Vec<T>` only:** with the `specialised-vectors` feature
/// (enabled by default), [`Vec<T>`](crate::implementations::vecs) uses a
/// compact bulk layout for primitive numeric types, `bool`, and (when the
/// optional `uuid` / `rust_decimal` features are enabled) `uuid::Uuid` /
/// `rust_decimal::Decimal`. [`Vec<T>::walk_revisioned`] rejects those
/// element types up front; sets and heaps are unaffected because they
/// always use per-element framing.
///
/// [`SerializeRevisioned`]: crate::SerializeRevisioned
pub struct SeqWalker<'r, T, R: Read + 'r> {
	reader: &'r mut R,
	remaining: usize,
	_marker: PhantomData<fn() -> T>,
}

impl<'r, T, R: Read + 'r> SeqWalker<'r, T, R> {
	/// Construct a sequence walker. Reads the `usize` length prefix.
	#[inline]
	pub fn new(reader: &'r mut R) -> Result<Self, Error> {
		let len = usize::deserialize_revisioned(reader)?;
		Ok(Self {
			reader,
			remaining: len,
			_marker: PhantomData,
		})
	}

	/// Number of items still to be visited.
	#[inline]
	pub fn remaining(&self) -> usize {
		self.remaining
	}

	/// Yield the next item handle, if any.
	///
	/// The returned [`SeqItem`] borrows the walker until it is consumed
	/// (`decode`, `skip`, or `walk`).
	#[inline]
	pub fn next_item(&mut self) -> Option<SeqItem<'_, 'r, T, R>> {
		if self.remaining == 0 {
			None
		} else {
			Some(SeqItem {
				walker: self,
			})
		}
	}

	/// Skip every remaining item.
	#[inline]
	pub fn skip_remaining(mut self) -> Result<(), Error>
	where
		T: SkipRevisioned,
	{
		while self.remaining > 0 {
			T::skip_revisioned(self.reader)?;
			self.remaining -= 1;
		}
		Ok(())
	}
}

/// One item position inside a [`SeqWalker`].
pub struct SeqItem<'a, 'r, T, R: Read + 'r> {
	walker: &'a mut SeqWalker<'r, T, R>,
}

impl<'a, 'r, T, R: Read + 'r> SeqItem<'a, 'r, T, R> {
	/// Decode this item.
	#[inline]
	pub fn decode(self) -> Result<T, Error>
	where
		T: DeserializeRevisioned,
	{
		let v = T::deserialize_revisioned(self.walker.reader)?;
		self.walker.remaining -= 1;
		Ok(v)
	}

	/// Skip this item.
	#[inline]
	pub fn skip(self) -> Result<(), Error>
	where
		T: SkipRevisioned,
	{
		T::skip_revisioned(self.walker.reader)?;
		self.walker.remaining -= 1;
		Ok(())
	}

	/// Walk into this item, returning a sub-walker borrowing the parent.
	///
	/// The parent walker cannot advance until the returned walker is dropped
	/// or consumed. After it is dropped, the next call to
	/// [`SeqWalker::next_item`] yields the following item.
	#[inline]
	pub fn walk(self) -> Result<T::Walker<'a, R>, Error>
	where
		T: WalkRevisioned,
	{
		let walker = T::walk_revisioned(self.walker.reader)?;
		self.walker.remaining -= 1;
		Ok(walker)
	}
}

impl<'a, 'r, T, R> SeqItem<'a, 'r, T, R>
where
	T: LengthPrefixedBytes,
	R: BorrowedReader + 'r,
{
	/// Inspect the item's wire bytes without allocating, calling `f` with
	/// a slice borrowed from the underlying buffer; the reader is
	/// advanced past the item once `f` returns and the seq walker's
	/// remaining counter is decremented.
	///
	/// Useful for searching sorted sequences (`Vec<String>`,
	/// `BTreeSet<Bytes>`, …) by byte compare without paying for a
	/// per-item decode allocation.
	#[inline]
	pub fn with_bytes<F, U>(self, f: F) -> Result<U, Error>
	where
		F: FnOnce(&[u8]) -> U,
	{
		let result = with_length_prefixed_bytes(self.walker.reader, f)?;
		self.walker.remaining -= 1;
		Ok(result)
	}
}

// -----------------------------------------------------------------------------
// MapWalker — HashMap<K, V>, BTreeMap<K, V>, …
// -----------------------------------------------------------------------------

/// Walker for a `(K, V)` map (length-prefix followed by alternating
/// key/value encodings).
pub struct MapWalker<'r, K, V, R: Read + 'r> {
	reader: &'r mut R,
	remaining: usize,
	_marker: PhantomData<fn() -> (K, V)>,
}

/// Where the cursor sits within a single [`MapEntry`] iteration.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum MapCursor {
	/// Reader is positioned at the start of the key.
	BeforeKey,
	/// Reader is positioned at the start of the value.
	BeforeValue,
}

impl<'r, K, V, R: Read + 'r> MapWalker<'r, K, V, R> {
	/// Construct a map walker. Reads the `usize` length prefix.
	#[inline]
	pub fn new(reader: &'r mut R) -> Result<Self, Error> {
		let len = usize::deserialize_revisioned(reader)?;
		Ok(Self {
			reader,
			remaining: len,
			_marker: PhantomData,
		})
	}

	/// Number of entries still to be visited.
	#[inline]
	pub fn remaining(&self) -> usize {
		self.remaining
	}

	/// Borrow the next entry, if any.
	///
	/// The caller must consume the returned [`MapEntry`] (call one of
	/// `decode_value`, `skip_value`, or `walk_value`) before calling
	/// `next_entry` again. Forgetting to consume the entry's value is a
	/// programming error: the next entry will start mid-payload.
	#[inline]
	pub fn next_entry(&mut self) -> Option<MapEntry<'_, 'r, K, V, R>> {
		if self.remaining == 0 {
			None
		} else {
			Some(MapEntry {
				walker: self,
				cursor: MapCursor::BeforeKey,
			})
		}
	}

	/// Skip every remaining entry.
	#[inline]
	pub fn skip_remaining(mut self) -> Result<(), Error>
	where
		K: SkipRevisioned,
		V: SkipRevisioned,
	{
		while self.remaining > 0 {
			K::skip_revisioned(self.reader)?;
			V::skip_revisioned(self.reader)?;
			self.remaining -= 1;
		}
		Ok(())
	}

	/// Linear search by **decoded** key, returning a value handle for the
	/// matching entry or `None` if no entry passes `predicate`.
	///
	/// **Sorted maps only:** The predicate is compared with **encoded key order**
	/// as visited on the wire (the order [`BTreeMap`](std::collections::BTreeMap)
	/// serialises). Using this on bytes produced from a [`HashMap`](std::collections::HashMap)
	/// while assuming lexicographic or sorted-key ordering is invalid: you may match
	/// the wrong entry or drop the tail under `Ordering::Greater` while the true key
	/// still appears later in the stream.
	///
	/// The returned [`LeafWalker`] is positioned at the start of the value's
	/// encoding (i.e. the type-level prefix has not been read yet); the
	/// caller can then call `decode`, `skip`, or `walk` on it.
	///
	/// Every strictly preceding entry (full key and value) is skipped.
	/// For the matching entry, only the key has been read off the wire;
	/// the value remains for the leaf walker. Entries that sort **after**
	/// the matched key stay on the reader, but this method consumes `self`,
	/// so you cannot call [`next_entry`](Self::next_entry) afterward — there
	/// is no way to resume the same [`MapWalker`]. Prefer streaming iteration
	/// if you must handle one hit then walk the tail.
	///
	/// On `None` (no match) the entire remainder of the map is consumed.
	#[inline]
	pub fn find<F>(mut self, mut predicate: F) -> Result<Option<LeafWalker<'r, V, R>>, Error>
	where
		K: DeserializeRevisioned + SkipRevisioned,
		V: SkipRevisioned,
		F: FnMut(&K) -> Ordering,
	{
		while self.remaining > 0 {
			let key = K::deserialize_revisioned(self.reader)?;
			match predicate(&key) {
				Ordering::Less => {
					V::skip_revisioned(self.reader)?;
					self.remaining -= 1;
				}
				Ordering::Equal => {
					return Ok(Some(LeafWalker::new(self.reader)));
				}
				Ordering::Greater => {
					V::skip_revisioned(self.reader)?;
					self.remaining -= 1;
					while self.remaining > 0 {
						K::skip_revisioned(self.reader)?;
						V::skip_revisioned(self.reader)?;
						self.remaining -= 1;
					}
					return Ok(None);
				}
			}
		}
		Ok(None)
	}
}

impl<'r, K, V, R> MapWalker<'r, K, V, R>
where
	K: LengthPrefixedBytes + SkipRevisioned,
	V: SkipRevisioned,
	R: BorrowedReader + 'r,
{
	/// Linear search by **byte-borrowed** key, returning a value handle for
	/// the matching entry or `None` if no entry passes `predicate`.
	///
	/// Inherits the **sorted-map** requirement from [`find`](Self::find): wire
	/// visit order must match the predicate's assumed ordering (see [`find`]).
	///
	/// Sibling of [`find`](Self::find) for keys whose wire format is
	/// exactly `usize len || raw bytes` ([`LengthPrefixedBytes`]). Each
	/// visited key is presented to `predicate` as a borrowed `&[u8]`
	/// without being decoded into `K`, so a 128-key scan over a
	/// `BTreeMap<Strand, _>` does zero allocations regardless of key
	/// length.
	///
	/// Behaviour identical to [`find`](Self::find) otherwise: the
	/// `Less / Equal / Greater` control flow assumes the underlying map
	/// is sorted by key; `Equal` returns a leaf at the matched value and
	/// consumes `self` (see [`find`](Self::find)); on no-match the entire
	/// remainder is consumed.
	#[inline]
	pub fn find_bytes<F>(mut self, mut predicate: F) -> Result<Option<LeafWalker<'r, V, R>>, Error>
	where
		F: FnMut(&[u8]) -> Ordering,
	{
		while self.remaining > 0 {
			let key_len = usize::deserialize_revisioned(self.reader)?;
			let cmp = {
				let key_bytes = self.reader.peek_bytes(key_len)?;
				predicate(key_bytes)
			};
			self.reader.advance(key_len)?;
			match cmp {
				Ordering::Less => {
					V::skip_revisioned(self.reader)?;
					self.remaining -= 1;
				}
				Ordering::Equal => {
					return Ok(Some(LeafWalker::new(self.reader)));
				}
				Ordering::Greater => {
					V::skip_revisioned(self.reader)?;
					self.remaining -= 1;
					while self.remaining > 0 {
						K::skip_revisioned(self.reader)?;
						V::skip_revisioned(self.reader)?;
						self.remaining -= 1;
					}
					return Ok(None);
				}
			}
		}
		Ok(None)
	}
}

/// One key+value pair inside a [`MapWalker`].
///
/// The cursor is positioned at the start of the key. The caller must first
/// handle the key (`decode_key` or `skip_key`) and then the value
/// (`decode_value`, `skip_value`, or `walk_value`). `decode_pair` and
/// `skip_pair` shortcut both at once.
///
/// Calling methods out of order returns [`Error::Deserialize`] in all builds
/// (not only debug builds), without advancing the reader when the check fails
/// before I/O.
pub struct MapEntry<'a, 'r, K, V, R: Read + 'r> {
	walker: &'a mut MapWalker<'r, K, V, R>,
	cursor: MapCursor,
}

impl<'a, 'r, K, V, R: Read + 'r> MapEntry<'a, 'r, K, V, R> {
	fn expect_cursor(&self, expected: MapCursor, operation: &'static str) -> Result<(), Error> {
		if self.cursor != expected {
			return Err(Error::Deserialize(format!(
				"MapEntry: invalid cursor for {operation} (expected {expected:?}, found {:?})",
				self.cursor,
			)));
		}
		Ok(())
	}

	/// Decode the key. The cursor advances to the value.
	#[inline]
	pub fn decode_key(&mut self) -> Result<K, Error>
	where
		K: DeserializeRevisioned,
	{
		self.expect_cursor(MapCursor::BeforeKey, "decode_key")?;
		let k = K::deserialize_revisioned(self.walker.reader)?;
		self.cursor = MapCursor::BeforeValue;
		Ok(k)
	}

	/// Skip the key. The cursor advances to the value.
	#[inline]
	pub fn skip_key(&mut self) -> Result<(), Error>
	where
		K: SkipRevisioned,
	{
		self.expect_cursor(MapCursor::BeforeKey, "skip_key")?;
		K::skip_revisioned(self.walker.reader)?;
		self.cursor = MapCursor::BeforeValue;
		Ok(())
	}

	/// Decode the value (key must already be consumed).
	#[inline]
	pub fn decode_value(self) -> Result<V, Error>
	where
		V: DeserializeRevisioned,
	{
		self.expect_cursor(MapCursor::BeforeValue, "decode_value")?;
		let v = V::deserialize_revisioned(self.walker.reader)?;
		self.walker.remaining -= 1;
		Ok(v)
	}

	/// Skip the value (key must already be consumed).
	#[inline]
	pub fn skip_value(self) -> Result<(), Error>
	where
		V: SkipRevisioned,
	{
		self.expect_cursor(MapCursor::BeforeValue, "skip_value")?;
		V::skip_revisioned(self.walker.reader)?;
		self.walker.remaining -= 1;
		Ok(())
	}

	/// Walk into the value. The parent map walker is borrowed for the
	/// returned walker's lifetime.
	#[inline]
	pub fn walk_value(self) -> Result<V::Walker<'a, R>, Error>
	where
		V: WalkRevisioned,
	{
		self.expect_cursor(MapCursor::BeforeValue, "walk_value")?;
		let w = V::walk_revisioned(self.walker.reader)?;
		self.walker.remaining -= 1;
		Ok(w)
	}

	/// Decode key and value together, advancing past the entry.
	#[inline]
	pub fn decode_pair(mut self) -> Result<(K, V), Error>
	where
		K: DeserializeRevisioned,
		V: DeserializeRevisioned,
	{
		let key = self.decode_key()?;
		let value = self.decode_value()?;
		Ok((key, value))
	}

	/// Skip key and value together, advancing past the entry.
	#[inline]
	pub fn skip_pair(mut self) -> Result<(), Error>
	where
		K: SkipRevisioned,
		V: SkipRevisioned,
	{
		self.skip_key()?;
		self.skip_value()
	}
}

impl<'a, 'r, K, V, R> MapEntry<'a, 'r, K, V, R>
where
	R: BorrowedReader + 'r,
{
	/// Inspect the entry's key as raw wire bytes without allocating.
	///
	/// Available when the key type's wire format is `usize len || raw
	/// bytes` ([`LengthPrefixedBytes`]) and the reader is slice-backed
	/// ([`BorrowedReader`]). The cursor advances past the key once `f`
	/// returns; the caller must then handle the value
	/// (`decode_value` / `skip_value` / `walk_value`).
	///
	/// Cheaper sibling of [`decode_key`](Self::decode_key) for the
	/// common case "compare key bytes against a needle and decide what
	/// to do with the value".
	#[inline]
	pub fn with_key_bytes<F, U>(&mut self, f: F) -> Result<U, Error>
	where
		K: LengthPrefixedBytes,
		F: FnOnce(&[u8]) -> U,
	{
		self.expect_cursor(MapCursor::BeforeKey, "with_key_bytes")?;
		let result = with_length_prefixed_bytes(self.walker.reader, f)?;
		self.cursor = MapCursor::BeforeValue;
		Ok(result)
	}

	/// Inspect the entry's value as raw wire bytes without allocating
	/// (key must already be consumed).
	///
	/// Available when the value type's wire format is `usize len || raw
	/// bytes` ([`LengthPrefixedBytes`]) and the reader is slice-backed
	/// ([`BorrowedReader`]). Consumes the entry; the entry's
	/// `remaining` counter is decremented.
	///
	/// Cheaper sibling of [`decode_value`](Self::decode_value) for the
	/// "filter map by value bytes" pattern.
	#[inline]
	pub fn with_value_bytes<F, U>(self, f: F) -> Result<U, Error>
	where
		V: LengthPrefixedBytes,
		F: FnOnce(&[u8]) -> U,
	{
		self.expect_cursor(MapCursor::BeforeValue, "with_value_bytes")?;
		let result = with_length_prefixed_bytes(self.walker.reader, f)?;
		self.walker.remaining -= 1;
		Ok(result)
	}
}

// -----------------------------------------------------------------------------
// StructWalker — generic positional walker for `#[revisioned(...)]` structs.
//
// The walker tracks the wire revision and a logical field index (`position`).
// After [`StructWalker::walk`], that counter advances even though the nested
// walker still owns the remainder of that field on the wire until dropped.
// -----------------------------------------------------------------------------

/// Generic walker for revisioned structs.
///
/// Field-type information is supplied by the caller per call rather than
/// stored in the walker. The caller is responsible for invoking field methods
/// in the wire order and matching the schema at the wire revision.
///
/// [`position`](Self::position) counts fields **started** (`decode`, `skip`, or
/// `walk`), not necessarily bytes fully consumed when the last operation was
/// [`walk`](Self::walk).
pub struct StructWalker<'r, R: Read + 'r> {
	reader: &'r mut R,
	revision: u16,
	position: u32,
}

impl<'r, R: Read + 'r> StructWalker<'r, R> {
	/// Construct a struct walker. The caller must already have read the
	/// type-level revision header.
	#[inline]
	pub fn new(reader: &'r mut R, revision: u16) -> Self {
		Self {
			reader,
			revision,
			position: 0,
		}
	}

	/// Wire revision of the encoded value being walked.
	#[inline]
	pub fn revision(&self) -> u16 {
		self.revision
	}

	/// Count of fields for which [`decode`](Self::decode), [`skip`](Self::skip),
	/// or [`walk`](Self::walk) has succeeded in wire order (starts at `0`;
	/// increments by one per call).
	///
	/// This tracks which schema slot you will touch next; it does **not** imply
	/// the previous field's bytes are fully past on the reader when that field was
	/// opened with [`walk`](Self::walk). In that case [`position`](Self::position)
	/// bumps as soon as the nested walker is constructed, while the child still
	/// borrows the reader for the rest of that field until it is dropped.
	#[inline]
	pub fn position(&self) -> u32 {
		self.position
	}

	/// Decode the next field as type `T`.
	#[inline]
	pub fn decode<T: DeserializeRevisioned>(&mut self) -> Result<T, Error> {
		let v = T::deserialize_revisioned(self.reader)?;
		self.position += 1;
		Ok(v)
	}

	/// Skip the next field as type `T`.
	#[inline]
	pub fn skip<T: SkipRevisioned>(&mut self) -> Result<(), Error> {
		T::skip_revisioned(self.reader)?;
		self.position += 1;
		Ok(())
	}

	/// Walk into the next field as type `T`. The returned walker borrows
	/// from `&mut self`; the parent walker can resume after it is dropped.
	///
	/// [`position`](Self::position) increments when this returns `Ok`, before the
	/// nested walker consumes the field payload — do not treat a higher position
	/// as “the reader has left that field” until the child walker finishes.
	#[inline]
	pub fn walk<T: WalkRevisioned>(&mut self) -> Result<T::Walker<'_, R>, Error> {
		let w = T::walk_revisioned(self.reader)?;
		self.position += 1;
		Ok(w)
	}

	/// Consume the struct walker and walk into the next field as type `T`,
	/// transferring ownership of the underlying reader to the returned
	/// walker. Subsequent fields cannot be walked.
	#[inline]
	pub fn into_walk<T: WalkRevisioned>(self) -> Result<T::Walker<'r, R>, Error> {
		T::walk_revisioned(self.reader)
	}

	/// Borrow the underlying reader. Useful for hand-rolled walkers needing
	/// auxiliary reads.
	#[inline]
	pub fn reader(&mut self) -> &mut R {
		self.reader
	}

	/// Surrender the underlying reader after the caller has manually
	/// consumed every remaining field.
	#[inline]
	pub fn into_reader(self) -> &'r mut R {
		self.reader
	}
}

// -----------------------------------------------------------------------------
// EnumWalker — generic walker for `#[revisioned(...)]` enums.
//
// Reads the `u32` variant discriminant on construction; the caller dispatches
// on it and supplies the payload type to `decode`, `skip`, or `into_walk`.
// -----------------------------------------------------------------------------

/// Generic walker for revisioned enums.
///
/// On construction the walker reads the `u32` variant discriminant. The
/// caller dispatches on `discriminant()` (or via a derive-generated table)
/// and supplies the appropriate payload type to one of the consume methods.
pub struct EnumWalker<'r, R: Read + 'r> {
	reader: &'r mut R,
	revision: u16,
	discriminant: u32,
}

impl<'r, R: Read + 'r> EnumWalker<'r, R> {
	/// Construct an enum walker. Reads the `u32` variant discriminant from
	/// `reader`. The caller must already have read the type-level revision
	/// header.
	#[inline]
	pub fn new(reader: &'r mut R, revision: u16) -> Result<Self, Error> {
		let discriminant = u32::deserialize_revisioned(reader)?;
		Ok(Self {
			reader,
			revision,
			discriminant,
		})
	}

	/// Wire revision of the encoded value being walked.
	#[inline]
	pub fn revision(&self) -> u16 {
		self.revision
	}

	/// Variant discriminant on the wire.
	#[inline]
	pub fn discriminant(&self) -> u32 {
		self.discriminant
	}

	/// Decode the variant payload as type `T`. Caller must supply the type
	/// matching the variant at this discriminant.
	#[inline]
	pub fn decode<T: DeserializeRevisioned>(self) -> Result<T, Error> {
		T::deserialize_revisioned(self.reader)
	}

	/// Skip the variant payload as type `T`.
	#[inline]
	pub fn skip<T: SkipRevisioned>(self) -> Result<(), Error> {
		T::skip_revisioned(self.reader)
	}

	/// Walk into the variant payload as type `T`, transferring ownership
	/// of the underlying reader to the returned walker.
	#[inline]
	pub fn into_walk<T: WalkRevisioned>(self) -> Result<T::Walker<'r, R>, Error> {
		T::walk_revisioned(self.reader)
	}

	/// Borrow the underlying reader.
	#[inline]
	pub fn reader(&mut self) -> &mut R {
		self.reader
	}

	/// Surrender the underlying reader without consuming the payload.
	#[inline]
	pub fn into_reader(self) -> &'r mut R {
		self.reader
	}
}

// -----------------------------------------------------------------------------
// Convenience walkers used by enums (Option/Result already specialised above)
// -----------------------------------------------------------------------------

/// Internal helper that reads the `u32` discriminant of an enum body and
/// hands the reader back. Intended for hand-rolled walkers; the derive macro
/// emits the same prelude inline.
#[inline]
pub fn read_enum_discriminant<R: Read>(reader: &mut R) -> Result<u32, Error> {
	u32::deserialize_revisioned(reader)
}