parcode 0.6.1

A high-performance, lazy load and parallelized caching library for complex Rust data structures.
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
//! Runtime utilities for generated code (Macros).
//! Do not use directly.

use crate::error::Result;
use crate::format::ChildRef;
use crate::graph::{JobConfig, SerializationJob};
use crate::reader::{ChunkNode, ParcodeItem};
use serde::de::DeserializeOwned;
use std::borrow::Cow;
use std::collections::HashMap;
use std::hash::Hash;
use std::io::Cursor;
use std::marker::PhantomData;
use std::vec::IntoIter;

/// Wrapper that injects configuration into an existing Job.
#[derive(Debug)]
pub struct ConfiguredJob<'a, J>
where
    J: ?Sized,
{
    config: JobConfig,
    inner: Box<J>,
    _marker: PhantomData<&'a ()>,
}

impl<'a, J> ConfiguredJob<'a, J>
where
    J: SerializationJob<'a> + ?Sized,
{
    pub fn new(inner: Box<J>, config: JobConfig) -> Self {
        Self {
            inner,
            config,
            _marker: PhantomData,
        }
    }
}

impl<'a, J> SerializationJob<'a> for ConfiguredJob<'a, J>
where
    J: SerializationJob<'a> + ?Sized,
{
    fn execute(&self, children_refs: &[ChildRef]) -> Result<Vec<u8>> {
        self.inner.execute(children_refs)
    }

    fn estimated_size(&self) -> usize {
        self.inner.estimated_size()
    }

    fn config(&self) -> JobConfig {
        self.config
    }
}

/// Trait implemented by types that support Lazy Mirroring.
///
/// This trait acts as a bridge between the original type `T` and its generated
/// lazy counterpart `T::Lazy`.
pub trait ParcodeLazyRef<'a>: Sized {
    /// The Mirror Type.
    /// For primitives, it is `ParcodePromise<'a, T>`.
    /// For structs deriving `ParcodeObject`, it is `StructNameLazy<'a>`.
    type Lazy;

    /// Creates the lazy view from a graph node.
    fn create_lazy(node: ChunkNode<'a>) -> Result<Self::Lazy>;

    /// Creates the lazy view from a data stream (Inside Vec access).
    /// This allows scanning headers without full deserialization.
    fn read_lazy_from_stream(
        reader: &mut Cursor<&[u8]>,
        children: &mut IntoIter<ChunkNode<'a>>,
    ) -> Result<Self::Lazy>;
}

/// A terminal promise for a single value.
///
/// Use `.load()` to trigger deserialization.
#[derive(Debug, Clone)]
pub struct ParcodePromise<'a, T> {
    node: ChunkNode<'a>,
    _m: PhantomData<T>,
}

impl<'a, T> ParcodePromise<'a, T>
where
    T: DeserializeOwned,
{
    /// Internal constructor.
    pub fn new(node: ChunkNode<'a>) -> Self {
        Self {
            node,
            _m: PhantomData,
        }
    }

    /// Loads the data from disk/memory.
    pub fn load(&self) -> Result<T> {
        self.node.decode()
    }
}

/// A promise for a collection (Vector).
///
/// Supports partial loading and random access via `.get(index)`.
/// Specialized lazy field for collections (Vec) allowing partial access.
#[derive(Debug, Clone)]
pub struct ParcodeCollectionPromise<'a, T> {
    node: ChunkNode<'a>,
    _m: PhantomData<T>,
}

impl<'a, T> ParcodeCollectionPromise<'a, T>
where
    T: ParcodeItem + Send + Sync + 'a,
{
    /// Internal constructor.
    pub fn new(node: ChunkNode<'a>) -> Self {
        Self {
            node,
            _m: PhantomData,
        }
    }

    /// Loads the entire collection into memory.
    pub fn load(&self) -> Result<Vec<T>> {
        self.node.decode_parallel_collection()
    }

    /// Retrieves a single item without loading the whole collection.
    /// Uses O(1) arithmetic navigation.
    pub fn get(&self, index: usize) -> Result<T> {
        self.node.get(index)
    }

    /// Returns the number of items in the collection.
    ///
    /// This is an O(1) operation that reads the length from the chunk header.
    pub fn len(&self) -> usize {
        usize::try_from(self.node.len()).unwrap_or(usize::MAX)
    }

    /// Returns true if the collection has no items.
    pub fn is_empty(&self) -> bool {
        self.node.is_empty()
    }

    /// Returns a streaming iterator.
    pub fn iter(&self) -> Result<impl Iterator<Item = Result<T>> + 'a> {
        self.node.clone().iter() // TODO: This must be checked to optimize.
    }
}

impl<'a, T> ParcodeCollectionPromise<'a, T>
where
    T: ParcodeItem + Send + Sync + ParcodeLazyRef<'a> + 'a,
{
    /// Retrieves a Lazy Proxy for the item at `index`.
    ///
    /// This allows accessing local fields of the item without triggering the loading
    /// of its heavy dependencies.
    pub fn get_lazy(&self, index: usize) -> Result<T::Lazy> {
        // 1. Resolve shard using internal helper exposed via crate
        let (shard_node, index_in_shard) = self.node.locate_shard_item(index)?;

        // 2. Prepare readers
        let payload = shard_node.read_raw()?;
        let mut cursor = Cursor::new(payload.as_ref());
        let children_vec = shard_node.children()?;
        let mut children_iter = children_vec.into_iter();

        // Skip Vector Length (u64)
        // Shards created by ParcodeVisitor::serialize_slice always have a length prefix.
        let _len: u64 =
            bincode::serde::decode_from_std_read(&mut cursor, bincode::config::standard())
                .map_err(|e| crate::ParcodeError::Serialization(e.to_string()))?;

        // 3. Scan and Skip previous items
        // We read them as Lazy objects and discard them.
        for _ in 0..index_in_shard {
            let _ = T::read_lazy_from_stream(&mut cursor, &mut children_iter)?;
        }

        // 4. Read the target item as Lazy
        T::read_lazy_from_stream(&mut cursor, &mut children_iter)
    }

    /// Returns the first item as a lazy mirror, or None if empty.
    pub fn first(&self) -> Result<Option<T::Lazy>> {
        if self.is_empty() {
            Ok(None)
        } else {
            Ok(Some(self.get_lazy(0)?))
        }
    }

    /// Returns the last item as a lazy mirror, or None if empty.
    pub fn last(&self) -> Result<Option<T::Lazy>> {
        let len = self.len();
        if len == 0 {
            Ok(None)
        } else {
            Ok(Some(self.get_lazy(len - 1)?))
        }
    }

    /// Returns a lazy streaming iterator over the collection.
    ///
    /// This is highly efficient: it loads shards sequentially and processes items
    /// without re-reading bytes. Ideal for scanning large datasets.
    pub fn iter_lazy(&self) -> Result<ParcodeLazyIterator<'a, T>> {
        ParcodeLazyIterator::new(self.node.clone())
    }
}

/// A promise for a `HashMap` that supports lazy loading and efficient lookups.
///
/// This type wraps a `ChunkNode` representing a `HashMap` and provides methods to:
/// - Load the entire map into memory
/// - Perform O(1) lookups by key without loading the entire map
#[derive(Debug)]
pub struct ParcodeMapPromise<'a, K, V> {
    node: ChunkNode<'a>,
    _m: PhantomData<(K, V)>,
}

impl<'a, K, V> ParcodeMapPromise<'a, K, V>
where
    K: Hash + Eq + DeserializeOwned,
    V: DeserializeOwned,
{
    /// Internal constructor.
    pub fn new(node: ChunkNode<'a>) -> Self {
        Self {
            node,
            _m: PhantomData,
        }
    }

    /// Loads the full map by iterating all shards.
    ///
    /// This reconstructs the entire `HashMap` in memory by:
    /// 1. Reading the number of shards from the container
    /// 2. Iterating over each shard and deserializing its entries
    /// 3. Merging all entries into a single `HashMap`
    pub fn load(&self) -> Result<HashMap<K, V>> {
        // 1. Read number of shards from container
        let container_payload = self.node.read_raw()?;
        let num_shards = if container_payload.len() >= 4 {
            u32::from_le_bytes(
                container_payload
                    .get(0..4)
                    .ok_or_else(|| crate::ParcodeError::Format("Payload too short".into()))?
                    .try_into()
                    .map_err(|_| crate::ParcodeError::Format("Failed to read num_shards".into()))?,
            )
        } else {
            0
        };

        let mut map = HashMap::new();
        if num_shards == 0 {
            return Ok(map);
        }

        // 2. Iterate over shards
        // Use children() which returns Vec<ChunkNode>
        let shards = self.node.children()?;
        for shard in shards {
            let payload = shard.read_raw()?;
            if payload.len() < 8 {
                continue;
            }

            // Parse SOA header
            let count = u32::from_le_bytes(
                payload
                    .get(0..4)
                    .ok_or_else(|| {
                        crate::ParcodeError::Format("Payload too short for count".into())
                    })?
                    .try_into()
                    .map_err(|_| crate::ParcodeError::Format("Failed to read count".into()))?,
            ) as usize;

            // Layout: Count(4) + Padding(4) + Hashes(8*N) + Offsets(4*N) + Data
            let offsets_start = 8 + (count * 8);
            let data_start = offsets_start + (count * 4);

            // Read offsets to iterate data
            let offsets_bytes = payload
                .get(offsets_start..data_start)
                .ok_or_else(|| crate::ParcodeError::Format("Offsets out of bounds".into()))?;

            for i in 0..count {
                let off_bytes = offsets_bytes.get(i * 4..(i + 1) * 4).ok_or_else(|| {
                    crate::ParcodeError::Format("Offset index out of bounds".into())
                })?;
                let offset = u32::from_le_bytes(
                    off_bytes
                        .try_into()
                        .map_err(|_| crate::ParcodeError::Format("Failed to read offset".into()))?,
                ) as usize;
                let data_slice = payload.get(data_start + offset..).ok_or_else(|| {
                    crate::ParcodeError::Format("Data slice out of bounds".into())
                })?;

                // Deserialize (K, V) pair
                let (k, v): (K, V) =
                    bincode::serde::decode_from_slice(data_slice, bincode::config::standard())
                        .map_err(|e| crate::ParcodeError::Serialization(e.to_string()))?
                        .0;

                map.insert(k, v);
            }
        }
        Ok(map)
    }

    /// Performs a fast lookup for a single key without loading the entire map.
    ///
    /// This method:
    /// 1. Computes the hash of the key
    /// 2. Determines which shard contains the key (via modulo)
    /// 3. Loads only that shard
    /// 4. Scans the shard's hash array for matches
    /// 5. Verifies the key on hash collision and returns the value
    ///
    /// # Performance
    /// - O(1) shard selection
    /// - O(N/S) linear scan within shard (where S = number of shards)
    /// - SIMD-optimized hash comparison on supported platforms
    pub fn get(&self, key: &K) -> Result<Option<V>> {
        // 1. Read Container Payload (Num Shards)
        let container_payload = self.node.read_raw()?;
        if container_payload.len() < 4 {
            return Ok(None);
        } // Empty
        let num_shards = u32::from_le_bytes(
            container_payload
                .get(0..4)
                .ok_or_else(|| crate::ParcodeError::Format("Payload too short".into()))?
                .try_into()
                .map_err(|_| crate::ParcodeError::Format("Failed to read num_shards".into()))?,
        );

        // 2. Hash & Select Shard
        let target_hash = crate::map::hash_key(key);
        let shard_idx = usize::try_from(target_hash).unwrap_or(0) % (num_shards as usize);

        // 3. Load Shard
        let shard = self.node.get_child_by_index(shard_idx)?;
        let payload = shard.read_raw()?;

        if payload.len() < 8 {
            return Ok(None);
        } // Empty shard

        let count = u32::from_le_bytes(
            payload
                .get(0..4)
                .ok_or_else(|| crate::ParcodeError::Format("Payload too short for count".into()))?
                .try_into()
                .map_err(|_| crate::ParcodeError::Format("Failed to read count".into()))?,
        ) as usize;
        // Skip 4 bytes padding -> Offset 8

        let hashes_start = 8;
        let hashes_end = hashes_start + (count * 8);
        let offsets_start = hashes_end;
        let data_start = offsets_start + (count * 4);

        // 4. Fast Scan (SIMD Optimized via chunks_exact)
        let hashes_slice = payload
            .get(hashes_start..hashes_end)
            .ok_or_else(|| crate::ParcodeError::Format("Hashes slice out of bounds".into()))?;

        for (i, chunk) in hashes_slice.chunks_exact(8).enumerate() {
            let h = u64::from_le_bytes(
                chunk
                    .try_into()
                    .map_err(|_| crate::ParcodeError::Format("Failed to read hash".into()))?,
            );

            if h == target_hash {
                // Candidate found. Verify key to handle hash collisions.
                let offset_bytes = payload
                    .get(offsets_start + (i * 4)..offsets_start + (i * 4) + 4)
                    .ok_or_else(|| {
                        crate::ParcodeError::Format("Offset bytes out of bounds".into())
                    })?;
                let offset = u32::from_le_bytes(
                    offset_bytes
                        .try_into()
                        .map_err(|_| crate::ParcodeError::Format("Failed to read offset".into()))?,
                ) as usize;

                let data_slice = payload.get(data_start + offset..).ok_or_else(|| {
                    crate::ParcodeError::Format("Data slice out of bounds".into())
                })?;

                // Deserialize (K, V)
                // Use bincode::deserialize_from slice. Bincode knows when to stop.
                let (stored_key, stored_val): (K, V) =
                    bincode::serde::decode_from_slice(data_slice, bincode::config::standard())
                        .map_err(|e| crate::ParcodeError::Serialization(e.to_string()))?
                        .0;

                if &stored_key == key {
                    return Ok(Some(stored_val));
                }
                // If no match, it's a hash collision (rare). Continue searching.
            }
        }

        Ok(None)
    }
}

impl<'a, K, V> ParcodeLazyRef<'a> for HashMap<K, V>
where
    K: Hash + Eq + DeserializeOwned + Send + Sync + 'static,
    V: DeserializeOwned + Send + Sync + 'static,
{
    type Lazy = ParcodeMapPromise<'a, K, V>;
    fn create_lazy(node: ChunkNode<'a>) -> Result<Self::Lazy> {
        Ok(ParcodeMapPromise::new(node))
    }

    fn read_lazy_from_stream(
        _: &mut Cursor<&[u8]>,
        children: &mut IntoIter<ChunkNode<'a>>,
    ) -> Result<Self::Lazy> {
        let child_node = children.next().ok_or_else(|| {
            crate::ParcodeError::Format("Missing child node for HashMap field".into())
        })?;
        Ok(ParcodeMapPromise::new(child_node))
    }
}

impl<'a, K, V> ParcodeMapPromise<'a, K, V>
where
    K: Hash + Eq + DeserializeOwned + Send + Sync + 'static,
    V: ParcodeItem + Send + Sync + ParcodeLazyRef<'a> + 'a,
{
    /// Performs a lazy lookup for a single key.
    ///
    /// This returns a Lazy Mirror of the value, allowing access to its local fields
    /// without loading its heavy dependencies.
    pub fn get_lazy(&self, key: &K) -> Result<Option<V::Lazy>> {
        // 1. Read Container Payload (Num Shards)
        let container_payload = self.node.read_raw()?;
        if container_payload.len() < 4 {
            return Ok(None);
        } // Empty
        let num_shards = u32::from_le_bytes(
            container_payload
                .get(0..4)
                .ok_or_else(|| crate::ParcodeError::Format("Payload too short".into()))?
                .try_into()
                .map_err(|_| crate::ParcodeError::Format("Failed to read num_shards".into()))?,
        );

        // 2. Hash & Select Shard
        let target_hash = crate::map::hash_key(key);
        let shard_idx = usize::try_from(target_hash).unwrap_or(0) % (num_shards as usize);

        // 3. Load Shard
        let shard = self.node.get_child_by_index(shard_idx)?;
        let payload = shard.read_raw()?;

        if payload.len() < 8 {
            return Ok(None);
        } // Empty shard

        let count = u32::from_le_bytes(
            payload
                .get(0..4)
                .ok_or_else(|| crate::ParcodeError::Format("Payload too short for count".into()))?
                .try_into()
                .map_err(|_| crate::ParcodeError::Format("Failed to read count".into()))?,
        ) as usize;
        // Skip 4 bytes padding -> Offset 8

        let hashes_start = 8;
        let hashes_end = hashes_start + (count * 8);
        let offsets_start = hashes_end;
        let data_start = offsets_start + (count * 4);

        // 4. Fast Scan (SIMD Optimized via chunks_exact)
        let hashes_slice = payload
            .get(hashes_start..hashes_end)
            .ok_or_else(|| crate::ParcodeError::Format("Hashes slice out of bounds".into()))?;

        // We need to find the index `i` of the item.
        let mut target_index = None;

        for (i, chunk) in hashes_slice.chunks_exact(8).enumerate() {
            let h = u64::from_le_bytes(
                chunk
                    .try_into()
                    .map_err(|_| crate::ParcodeError::Format("Failed to read hash".into()))?,
            );

            if h == target_hash {
                // Candidate found. Verify key to handle hash collisions.
                let offset_bytes = payload
                    .get(offsets_start + (i * 4)..offsets_start + (i * 4) + 4)
                    .ok_or_else(|| {
                        crate::ParcodeError::Format("Offset bytes out of bounds".into())
                    })?;
                let offset = u32::from_le_bytes(
                    offset_bytes
                        .try_into()
                        .map_err(|_| crate::ParcodeError::Format("Failed to read offset".into()))?,
                ) as usize;

                let data_slice = payload.get(data_start + offset..).ok_or_else(|| {
                    crate::ParcodeError::Format("Data slice out of bounds".into())
                })?;

                // Deserialize K to check equality
                // We use decode_from_slice which returns (T, usize)
                let (stored_key, _): (K, usize) =
                    bincode::serde::decode_from_slice(data_slice, bincode::config::standard())
                        .map_err(|e| crate::ParcodeError::Serialization(e.to_string()))?;

                if &stored_key == key {
                    target_index = Some(i);
                    break;
                }
            }
        }

        let target_index = match target_index {
            Some(idx) => idx,
            None => return Ok(None),
        };

        // 5. We found the item at `target_index`.
        // Now we must advance the children iterator to the correct position.
        // This requires parsing items 0..target_index.

        let children = shard.children()?;
        let mut child_iter = children.into_iter();

        // Iterate 0..target_index
        for i in 0..target_index {
            let offset_bytes = payload
                .get(offsets_start + (i * 4)..offsets_start + (i * 4) + 4)
                .ok_or_else(|| crate::ParcodeError::Format("Offset bytes out of bounds".into()))?;
            let offset = u32::from_le_bytes(
                offset_bytes
                    .try_into()
                    .map_err(|_| crate::ParcodeError::Format("Failed to read offset".into()))?,
            ) as usize;

            let data_slice = payload
                .get(data_start + offset..)
                .ok_or_else(|| crate::ParcodeError::Format("Data slice out of bounds".into()))?;

            let mut cursor = Cursor::new(data_slice);

            // Read K
            let _: K =
                bincode::serde::decode_from_std_read(&mut cursor, bincode::config::standard())
                    .map_err(|e| crate::ParcodeError::Serialization(e.to_string()))?;

            // Read V (Lazy) - this consumes children
            let _ = V::read_lazy_from_stream(&mut cursor, &mut child_iter)?;
        }

        // 6. Read the target item
        let offset_bytes = payload
            .get(offsets_start + (target_index * 4)..offsets_start + (target_index * 4) + 4)
            .ok_or_else(|| crate::ParcodeError::Format("Offset bytes out of bounds".into()))?;
        let offset = u32::from_le_bytes(
            offset_bytes
                .try_into()
                .map_err(|_| crate::ParcodeError::Format("Failed to read offset".into()))?,
        ) as usize;

        let data_slice = payload
            .get(data_start + offset..)
            .ok_or_else(|| crate::ParcodeError::Format("Data slice out of bounds".into()))?;

        let mut cursor = Cursor::new(data_slice);

        // Read K (skip)
        let _: K = bincode::serde::decode_from_std_read(&mut cursor, bincode::config::standard())
            .map_err(|e| crate::ParcodeError::Serialization(e.to_string()))?;

        // Read V (Lazy) and return
        let lazy_val = V::read_lazy_from_stream(&mut cursor, &mut child_iter)?;

        Ok(Some(lazy_val))
    }
}

/// Streaming iterator for lazy collections.
///
/// This iterator efficiently traverses the shards of a vector, maintaining
/// a cursor position to avoid re-parsing previous items.
#[derive(Debug)]
pub struct ParcodeLazyIterator<'a, T>
where
    T: ParcodeLazyRef<'a>,
{
    /// The shards (children of the container node).
    shards: std::vec::IntoIter<ChunkNode<'a>>,

    /// Decompressed payload of the active shard.
    current_payload: Option<Cow<'a, [u8]>>,
    /// Current position in the payload buffer (bytes).
    current_pos: u64,
    /// Iterator over the active shard's children (nested chunks).
    current_children: std::vec::IntoIter<ChunkNode<'a>>,
    /// How many items are left to read in this shard.
    items_left_in_shard: u64,

    total_items: usize,
    items_yielded: usize,

    _marker: PhantomData<T>,
}

impl<'a, T> ParcodeLazyIterator<'a, T>
where
    T: ParcodeLazyRef<'a>,
{
    pub fn new(node: ChunkNode<'a>) -> Result<Self> {
        let total_items = usize::try_from(node.len()).unwrap_or(usize::MAX);
        let shards = node.children()?.into_iter();

        Ok(Self {
            shards,
            current_payload: None,
            current_pos: 0,
            current_children: Vec::new().into_iter(),
            items_left_in_shard: 0,
            total_items,
            items_yielded: 0,
            _marker: PhantomData,
        })
    }

    /// Advances to the next shard if the current one is exhausted.
    fn ensure_shard_loaded(&mut self) -> Result<bool> {
        if self.items_left_in_shard > 0 {
            return Ok(true);
        }

        // Current shard finished. Load next.
        if let Some(shard_node) = self.shards.next() {
            // 1. Load Payload
            let payload = shard_node.read_raw()?;

            // 2. Load Children
            let children = shard_node.children()?;

            // 3. Reset State
            // Read length prefix (standard for Parcode slice serialization)
            let mut cursor = Cursor::new(payload.as_ref());
            let len: u64 =
                bincode::serde::decode_from_std_read(&mut cursor, bincode::config::standard())
                    .map_err(|e| crate::ParcodeError::Serialization(e.to_string()))?;

            self.current_pos = cursor.position();
            self.items_left_in_shard = len;
            self.current_payload = Some(payload);
            self.current_children = children.into_iter();

            Ok(true)
        } else {
            Ok(false) // No more shards
        }
    }
}

impl<'a, T> Iterator for ParcodeLazyIterator<'a, T>
where
    T: ParcodeLazyRef<'a>,
{
    type Item = Result<T::Lazy>;

    fn next(&mut self) -> Option<Self::Item> {
        // 1. Ensure we have data
        match self.ensure_shard_loaded() {
            Ok(true) => {}
            Ok(false) => return None, // End of iteration
            Err(e) => return Some(Err(e)),
        }

        // 2. Prepare Cursor at current position
        let payload = self
            .current_payload
            .as_ref()
            .expect("ensure_shard_loaded guaranteed payload");
        let mut cursor = Cursor::new(payload.as_ref());
        cursor.set_position(self.current_pos);

        // 3. Deserialize ONE item (Lazy)
        // This advances the cursor and the children iterator
        let result = T::read_lazy_from_stream(&mut cursor, &mut self.current_children);

        // 4. Update State
        self.current_pos = cursor.position();
        self.items_left_in_shard -= 1;
        self.items_yielded += 1;

        Some(result)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.total_items - self.items_yielded;
        (remaining, Some(remaining))
    }
}

impl<'a, T> ExactSizeIterator for ParcodeLazyIterator<'a, T>
where
    T: ParcodeLazyRef<'a>,
{
    fn len(&self) -> usize {
        self.total_items - self.items_yielded
    }
}

// --- BLANKET IMPLEMENTATIONS FOR PRIMITIVES ---

macro_rules! impl_lazy_primitive {
    ($($t:ty),*) => {
        $(
            impl<'a> ParcodeLazyRef<'a> for $t {
                type Lazy = ParcodePromise<'a, $t>;
                fn create_lazy(node: ChunkNode<'a>) -> Result<Self::Lazy> {
                    Ok(ParcodePromise::new(node))
                }

                fn read_lazy_from_stream(
                    _: &mut Cursor<&[u8]>,
                    children: &mut IntoIter<ChunkNode<'a>>,
                ) -> Result<Self::Lazy> {
                    let child_node = children.next().ok_or_else(|| {
                        crate::ParcodeError::Format("Missing child node for chunkable primitive field".into())
                    })?;
                    Ok(ParcodePromise::new(child_node))
                }
            }
        )*
    }
}

impl_lazy_primitive!(u8, u16, u32, u64, i8, i16, i32, i64, f32, f64, bool, String);

// --- BLANKET IMPLEMENTATION FOR VECTORS ---

impl<'a, T: ParcodeItem + Send + Sync + 'static> ParcodeLazyRef<'a> for Vec<T> {
    type Lazy = ParcodeCollectionPromise<'a, T>;
    fn create_lazy(node: ChunkNode<'a>) -> Result<Self::Lazy> {
        Ok(ParcodeCollectionPromise::new(node))
    }

    fn read_lazy_from_stream(
        _: &mut Cursor<&[u8]>,
        children: &mut IntoIter<ChunkNode<'a>>,
    ) -> Result<Self::Lazy> {
        let child_node = children.next().ok_or_else(|| {
            crate::ParcodeError::Format("Missing child node for Vec field".into())
        })?;

        Ok(ParcodeCollectionPromise::new(child_node))
    }
}