zipora 2.1.5

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
//! Smart pointer serialization for reference-counted objects
//!
//! This module provides automatic serialization and deserialization support for
//! Rust's smart pointer types including Box<T>, Arc<T>, Rc<T>, and custom smart pointers.
//! Supports null handling, reference counting, and circular reference detection.

use crate::error::{Result, ZiporaError};
use crate::io::{DataInput, DataOutput};
use std::collections::HashMap;
// Hash import removed as it's not used in this module
use std::rc::{Rc, Weak as RcWeak};
use std::sync::{Arc, Weak as ArcWeak};

/// Serialization context for tracking shared objects and preventing cycles
#[derive(Debug)]
pub struct SerializationContext {
    /// Object ID counter for reference tracking
    next_id: u32,
    /// Map from object pointer to assigned ID
    object_ids: HashMap<usize, u32>,
    /// Enable circular reference detection
    detect_cycles: bool,
}

impl SerializationContext {
    /// Create a new serialization context
    pub fn new() -> Self {
        Self {
            next_id: 1, // Start from 1, reserve 0 for null
            object_ids: HashMap::new(),
            detect_cycles: true,
        }
    }
    
    /// Create context with cycle detection disabled (for performance)
    pub fn without_cycle_detection() -> Self {
        Self {
            next_id: 1,
            object_ids: HashMap::new(),
            detect_cycles: false,
        }
    }
    
    /// Get or assign an ID for an object
    fn get_or_assign_id<T>(&mut self, ptr: &T) -> u32 {
        let addr = ptr as *const T as usize;
        
        if let Some(&id) = self.object_ids.get(&addr) {
            id
        } else {
            let id = self.next_id;
            self.next_id += 1;
            if self.detect_cycles {
                self.object_ids.insert(addr, id);
            }
            id
        }
    }
    
    /// Check if an object has been seen before (for cycle detection)
    fn has_seen<T>(&self, ptr: &T) -> bool {
        if !self.detect_cycles {
            return false;
        }
        
        let addr = ptr as *const T as usize;
        self.object_ids.contains_key(&addr)
    }
    
    /// Clear the context for reuse
    pub fn clear(&mut self) {
        self.next_id = 1;
        self.object_ids.clear();
    }
}

impl Default for SerializationContext {
    fn default() -> Self {
        Self::new()
    }
}

/// Deserialization context for reconstructing shared objects
#[derive(Debug)]
pub struct DeserializationContext<T> {
    /// Map from object ID to reconstructed object
    objects: HashMap<u32, T>,
}

impl<T> DeserializationContext<T> {
    /// Create a new deserialization context
    pub fn new() -> Self {
        Self {
            objects: HashMap::new(),
        }
    }
    
    /// Store an object with its ID
    pub fn store_object(&mut self, id: u32, object: T) {
        self.objects.insert(id, object);
    }
    
    /// Get an object by its ID
    pub fn get_object(&self, id: u32) -> Option<&T> {
        self.objects.get(&id)
    }
    
    /// Clear the context for reuse
    pub fn clear(&mut self) {
        self.objects.clear();
    }
}

impl<T> Default for DeserializationContext<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Trait for types that can be serialized as smart pointers
pub trait SmartPtrSerialize<T> {
    /// Serialize the smart pointer with context
    fn serialize_with_context<O: DataOutput>(
        &self,
        output: &mut O,
        context: &mut SerializationContext,
    ) -> Result<()>;
    
    /// Deserialize the smart pointer with context
    fn deserialize_with_context<I: DataInput>(
        input: &mut I,
        _context: &mut DeserializationContext<Self>,
    ) -> Result<Self>
    where
        Self: Sized;
    
    /// Serialize without context (simple case)
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        let mut context = SerializationContext::new();
        self.serialize_with_context(output, &mut context)
    }
    
    /// Deserialize without context (simple case)
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self>
    where
        Self: Sized,
    {
        let mut context = DeserializationContext::new();
        Self::deserialize_with_context(input, &mut context)
    }
}

/// Marker trait for types that are serializable
pub trait SerializableType {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()>;
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self>
    where
        Self: Sized;
}

// Implementation for Box<T>
impl<T: SerializableType> SmartPtrSerialize<T> for Box<T> {
    fn serialize_with_context<O: DataOutput>(
        &self,
        output: &mut O,
        _context: &mut SerializationContext,
    ) -> Result<()> {
        // Box is always unique, no need for reference tracking
        output.write_u8(1)?; // Non-null marker
        self.as_ref().serialize(output)
    }
    
    fn deserialize_with_context<I: DataInput>(
        input: &mut I,
        _context: &mut DeserializationContext<Self>,
    ) -> Result<Self> {
        let marker = input.read_u8()?;
        match marker {
            0 => Err(ZiporaError::invalid_data("Box cannot be null")),
            1 => {
                let value = T::deserialize(input)?;
                Ok(Box::new(value))
            }
            _ => Err(ZiporaError::invalid_data("Invalid Box marker")),
        }
    }
}

// Implementation for Option<Box<T>>
impl<T: SerializableType> SmartPtrSerialize<T> for Option<Box<T>> {
    fn serialize_with_context<O: DataOutput>(
        &self,
        output: &mut O,
        context: &mut SerializationContext,
    ) -> Result<()> {
        match self {
            Some(boxed) => {
                output.write_u8(1)?; // Non-null marker
                boxed.serialize_with_context(output, context)
            }
            None => {
                output.write_u8(0)?; // Null marker
                Ok(())
            }
        }
    }
    
    fn deserialize_with_context<I: DataInput>(
        input: &mut I,
        _context: &mut DeserializationContext<Self>,
    ) -> Result<Self> {
        let marker = input.read_u8()?;
        match marker {
            0 => Ok(None),
            1 => {
                let boxed = Box::<T>::deserialize_with_context(input, 
                    &mut DeserializationContext::new())?;
                Ok(Some(boxed))
            }
            _ => Err(ZiporaError::invalid_data("Invalid Option<Box> marker")),
        }
    }
}

// Implementation for Rc<T>
impl<T: SerializableType> SmartPtrSerialize<T> for Rc<T> {
    fn serialize_with_context<O: DataOutput>(
        &self,
        output: &mut O,
        context: &mut SerializationContext,
    ) -> Result<()> {
        let obj_ref = self.as_ref();
        
        if context.has_seen(obj_ref) {
            // Object already serialized, write reference
            let id = context.get_or_assign_id(obj_ref);
            output.write_u8(2)?; // Reference marker
            output.write_u32(id)?;
        } else {
            // First time seeing this object, serialize it
            let id = context.get_or_assign_id(obj_ref);
            output.write_u8(1)?; // New object marker
            output.write_u32(id)?;
            obj_ref.serialize(output)?;
        }
        
        Ok(())
    }
    
    fn deserialize_with_context<I: DataInput>(
        input: &mut I,
        context: &mut DeserializationContext<Self>,
    ) -> Result<Self> {
        let marker = input.read_u8()?;
        match marker {
            0 => Err(ZiporaError::invalid_data("Rc cannot be null")),
            1 => {
                // New object
                let id = input.read_u32()?;
                let value = T::deserialize(input)?;
                let rc = Rc::new(value);
                context.store_object(id, rc.clone());
                Ok(rc)
            }
            2 => {
                // Reference to existing object
                let id = input.read_u32()?;
                context.get_object(id)
                    .cloned()
                    .ok_or_else(|| ZiporaError::invalid_data("Referenced object not found"))
            }
            _ => Err(ZiporaError::invalid_data("Invalid Rc marker")),
        }
    }
}

// Implementation for Arc<T>
impl<T: SerializableType + Send + Sync> SmartPtrSerialize<T> for Arc<T> {
    fn serialize_with_context<O: DataOutput>(
        &self,
        output: &mut O,
        context: &mut SerializationContext,
    ) -> Result<()> {
        let obj_ref = self.as_ref();
        
        if context.has_seen(obj_ref) {
            // Object already serialized, write reference
            let id = context.get_or_assign_id(obj_ref);
            output.write_u8(2)?; // Reference marker
            output.write_u32(id)?;
        } else {
            // First time seeing this object, serialize it
            let id = context.get_or_assign_id(obj_ref);
            output.write_u8(1)?; // New object marker
            output.write_u32(id)?;
            obj_ref.serialize(output)?;
        }
        
        Ok(())
    }
    
    fn deserialize_with_context<I: DataInput>(
        input: &mut I,
        context: &mut DeserializationContext<Self>,
    ) -> Result<Self> {
        let marker = input.read_u8()?;
        match marker {
            0 => Err(ZiporaError::invalid_data("Arc cannot be null")),
            1 => {
                // New object
                let id = input.read_u32()?;
                let value = T::deserialize(input)?;
                let arc = Arc::new(value);
                context.store_object(id, arc.clone());
                Ok(arc)
            }
            2 => {
                // Reference to existing object
                let id = input.read_u32()?;
                context.get_object(id)
                    .cloned()
                    .ok_or_else(|| ZiporaError::invalid_data("Referenced object not found"))
            }
            _ => Err(ZiporaError::invalid_data("Invalid Arc marker")),
        }
    }
}

// Weak pointer support for Rc<T>
impl<T: SerializableType> SmartPtrSerialize<T> for RcWeak<T> {
    fn serialize_with_context<O: DataOutput>(
        &self,
        output: &mut O,
        context: &mut SerializationContext,
    ) -> Result<()> {
        match self.upgrade() {
            Some(rc) => {
                output.write_u8(1)?; // Valid weak reference
                rc.serialize_with_context(output, context)
            }
            None => {
                output.write_u8(0)?; // Dangling weak reference
                Ok(())
            }
        }
    }
    
    fn deserialize_with_context<I: DataInput>(
        input: &mut I,
        _context: &mut DeserializationContext<Self>,
    ) -> Result<Self> {
        let marker = input.read_u8()?;
        match marker {
            0 => {
                // Dangling weak reference - create a weak that will never upgrade
                let dummy = Rc::new(T::deserialize(input)?);
                Ok(Rc::downgrade(&dummy))
            }
            1 => {
                // Valid weak reference
                let mut rc_context = DeserializationContext::new();
                let rc = Rc::<T>::deserialize_with_context(input, &mut rc_context)?;
                Ok(Rc::downgrade(&rc))
            }
            _ => Err(ZiporaError::invalid_data("Invalid Weak<Rc> marker")),
        }
    }
}

// Weak pointer support for Arc<T>
impl<T: SerializableType + Send + Sync> SmartPtrSerialize<T> for ArcWeak<T> {
    fn serialize_with_context<O: DataOutput>(
        &self,
        output: &mut O,
        context: &mut SerializationContext,
    ) -> Result<()> {
        match self.upgrade() {
            Some(arc) => {
                output.write_u8(1)?; // Valid weak reference
                arc.serialize_with_context(output, context)
            }
            None => {
                output.write_u8(0)?; // Dangling weak reference
                Ok(())
            }
        }
    }
    
    fn deserialize_with_context<I: DataInput>(
        input: &mut I,
        _context: &mut DeserializationContext<Self>,
    ) -> Result<Self> {
        let marker = input.read_u8()?;
        match marker {
            0 => {
                // Dangling weak reference - create a weak that will never upgrade
                let dummy = Arc::new(T::deserialize(input)?);
                Ok(Arc::downgrade(&dummy))
            }
            1 => {
                // Valid weak reference
                let mut arc_context = DeserializationContext::new();
                let arc = Arc::<T>::deserialize_with_context(input, &mut arc_context)?;
                Ok(Arc::downgrade(&arc))
            }
            _ => Err(ZiporaError::invalid_data("Invalid Weak<Arc> marker")),
        }
    }
}

/// Configuration for smart pointer serialization
#[derive(Debug, Clone)]
pub struct SmartPtrConfig {
    /// Enable cycle detection and shared object optimization
    pub cycle_detection: bool,
    /// Maximum recursion depth to prevent stack overflow
    pub max_depth: usize,
    /// Compress object IDs for smaller serialized size
    pub compress_ids: bool,
}

impl SmartPtrConfig {
    /// Create a new configuration with default settings
    pub fn new() -> Self {
        Self {
            cycle_detection: true,
            max_depth: 1000,
            compress_ids: false,
        }
    }
    
    /// Configuration optimized for performance
    pub fn performance_optimized() -> Self {
        Self {
            cycle_detection: false, // Faster but no cycle detection
            max_depth: 500,
            compress_ids: false,
        }
    }
    
    /// Configuration optimized for space efficiency
    pub fn space_optimized() -> Self {
        Self {
            cycle_detection: true,
            max_depth: 2000,
            compress_ids: true, // Smaller serialized size
        }
    }
    
    /// Configuration for robust handling of complex graphs
    pub fn robust() -> Self {
        Self {
            cycle_detection: true,
            max_depth: 5000,
            compress_ids: false,
        }
    }
}

impl Default for SmartPtrConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// High-level smart pointer serialization utilities
pub struct SmartPtrSerializer {
    config: SmartPtrConfig,
}

impl SmartPtrSerializer {
    /// Create a new serializer with the given configuration
    pub fn new(config: SmartPtrConfig) -> Self {
        Self { config }
    }
    
    /// Create a serializer with default configuration
    pub fn default() -> Self {
        Self::new(SmartPtrConfig::default())
    }
    
    /// Serialize a smart pointer to bytes
    pub fn serialize_to_bytes<T, P>(&self, ptr: &P) -> Result<Vec<u8>>
    where
        P: SmartPtrSerialize<T>,
        T: SerializableType,
    {
        let mut output = crate::io::VecDataOutput::new();
        let mut context = if self.config.cycle_detection {
            SerializationContext::new()
        } else {
            SerializationContext::without_cycle_detection()
        };
        
        ptr.serialize_with_context(&mut output, &mut context)?;
        Ok(output.into_vec())
    }
    
    /// Deserialize a smart pointer from bytes
    pub fn deserialize_from_bytes<T, P>(&self, bytes: &[u8]) -> Result<P>
    where
        P: SmartPtrSerialize<T>,
        T: SerializableType,
    {
        let mut input = crate::io::SliceDataInput::new(bytes);
        let mut context = DeserializationContext::new();
        
        P::deserialize_with_context(&mut input, &mut context)
    }
}

// Implementations for basic types to make them SerializableType
impl SerializableType for i32 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u32(*self as u32)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        Ok(input.read_u32()? as i32)
    }
}

impl SerializableType for u32 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u32(*self)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        input.read_u32()
    }
}

impl SerializableType for String {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_length_prefixed_string(self)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        input.read_length_prefixed_string()
    }
}

impl<T: SerializableType> SerializableType for Vec<T> {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u32(self.len() as u32)?;
        for item in self {
            item.serialize(output)?;
        }
        Ok(())
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        let len = input.read_u32()? as usize;
        let mut vec = Vec::with_capacity(len);
        for _ in 0..len {
            vec.push(T::deserialize(input)?);
        }
        Ok(vec)
    }
}

// Bridge implementation for Rc<T>
impl<T: SerializableType> SerializableType for Rc<T> {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        <Self as SmartPtrSerialize<T>>::serialize(self, output)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        <Self as SmartPtrSerialize<T>>::deserialize(input)
    }
}

// Bridge implementation for Arc<T> - requires Send + Sync for thread safety
impl<T: SerializableType + Send + Sync> SerializableType for Arc<T> {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        <Self as SmartPtrSerialize<T>>::serialize(self, output)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        <Self as SmartPtrSerialize<T>>::deserialize(input)
    }
}

// Bridge implementation for Box<T>
impl<T: SerializableType> SerializableType for Box<T> {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        <Self as SmartPtrSerialize<T>>::serialize(self, output)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        <Self as SmartPtrSerialize<T>>::deserialize(input)
    }
}

// Implement for primitive types using available DataInput/DataOutput methods
impl SerializableType for u8 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u8(*self)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        input.read_u8()
    }
}

impl SerializableType for u16 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u16(*self)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        input.read_u16()
    }
}

impl SerializableType for u64 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u64(*self)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        input.read_u64()
    }
}

impl SerializableType for i8 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u8(*self as u8)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        Ok(input.read_u8()? as i8)
    }
}

impl SerializableType for i16 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u16(*self as u16)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        Ok(input.read_u16()? as i16)
    }
}

impl SerializableType for i64 {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u64(*self as u64)
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        Ok(input.read_u64()? as i64)
    }
}

impl SerializableType for bool {
    fn serialize<O: DataOutput>(&self, output: &mut O) -> Result<()> {
        output.write_u8(if *self { 1 } else { 0 })
    }
    
    fn deserialize<I: DataInput>(input: &mut I) -> Result<Self> {
        Ok(input.read_u8()? != 0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::io::{SliceDataInput, VecDataOutput};

    #[test]
    fn test_box_serialization() {
        let boxed_value = Box::new(42i32);
        let mut output = VecDataOutput::new();
        
        <Box<i32> as SerializableType>::serialize(&boxed_value, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <Box<i32> as SerializableType>::deserialize(&mut input).unwrap();
        
        assert_eq!(*deserialized, 42);
    }

    #[test]
    fn test_option_box_serialization() {
        // Test Some case
        let some_value = Some(Box::new(42i32));
        let mut output = VecDataOutput::new();
        
        <Option<Box<i32>> as SmartPtrSerialize<i32>>::serialize(&some_value, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <Option<Box<i32>> as SmartPtrSerialize<i32>>::deserialize(&mut input).unwrap();
        
        assert_eq!(deserialized.map(|b| *b), Some(42));
        
        // Test None case
        let none_value: Option<Box<i32>> = None;
        let mut output = VecDataOutput::new();
        
        <Option<Box<i32>> as SmartPtrSerialize<i32>>::serialize(&none_value, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <Option<Box<i32>> as SmartPtrSerialize<i32>>::deserialize(&mut input).unwrap();
        
        assert!(deserialized.is_none());
    }

    #[test]
    fn test_rc_serialization() {
        let rc_value = Rc::new(42i32);
        let mut output = VecDataOutput::new();
        
        <Rc<i32> as SerializableType>::serialize(&rc_value, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <Rc<i32> as SerializableType>::deserialize(&mut input).unwrap();
        
        assert_eq!(*deserialized, 42);
    }

    #[test]
    fn test_arc_serialization() {
        let arc_value = Arc::new(42i32);
        let mut output = VecDataOutput::new();
        
        <Arc<i32> as SerializableType>::serialize(&arc_value, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <Arc<i32> as SerializableType>::deserialize(&mut input).unwrap();
        
        assert_eq!(*deserialized, 42);
    }

    #[test]
    fn test_shared_object_serialization() {
        let shared_value = Rc::new(42i32);
        let clone1 = shared_value.clone();
        let clone2 = shared_value.clone();
        
        let mut context = SerializationContext::new();
        let mut output = VecDataOutput::new();
        
        // Serialize multiple references to the same object
        <Rc<i32> as SmartPtrSerialize<i32>>::serialize_with_context(&clone1, &mut output, &mut context).unwrap();
        <Rc<i32> as SmartPtrSerialize<i32>>::serialize_with_context(&clone2, &mut output, &mut context).unwrap();
        
        let bytes = output.into_vec();
        let mut input = SliceDataInput::new(&bytes);
        let mut deserialize_context = DeserializationContext::new();
        
        let deserialized1 = <Rc<i32> as SmartPtrSerialize<i32>>::deserialize_with_context(&mut input, &mut deserialize_context).unwrap();
        let deserialized2 = <Rc<i32> as SmartPtrSerialize<i32>>::deserialize_with_context(&mut input, &mut deserialize_context).unwrap();
        
        assert_eq!(*deserialized1, 42);
        assert_eq!(*deserialized2, 42);
        
        // They should be the same object (same allocation)
        assert!(Rc::ptr_eq(&deserialized1, &deserialized2));
    }

    #[test]
    fn test_serialization_context() {
        let mut context = SerializationContext::new();
        
        let value1 = 42i32;
        let value2 = 43i32;
        
        // Same object should get the same ID
        let id1a = context.get_or_assign_id(&value1);
        let id1b = context.get_or_assign_id(&value1);
        assert_eq!(id1a, id1b);
        
        // Different object should get different ID
        let id2 = context.get_or_assign_id(&value2);
        assert_ne!(id1a, id2);
        
        // Should detect already seen objects
        assert!(context.has_seen(&value1));
        assert!(context.has_seen(&value2));
    }

    #[test]
    fn test_smart_ptr_serializer() {
        let serializer = SmartPtrSerializer::default();
        let boxed_value = Box::new(42i32);
        
        let bytes = serializer.serialize_to_bytes(&boxed_value).unwrap();
        let deserialized: Box<i32> = serializer.deserialize_from_bytes(&bytes).unwrap();
        
        assert_eq!(*deserialized, 42);
    }

    #[test]
    fn test_performance_config() {
        let config = SmartPtrConfig::performance_optimized();
        assert!(!config.cycle_detection);
        assert_eq!(config.max_depth, 500);
        
        let config = SmartPtrConfig::space_optimized();
        assert!(config.cycle_detection);
        assert!(config.compress_ids);
    }

    #[test]
    fn test_vec_serialization() {
        let vec_value = vec![1i32, 2, 3, 4, 5];
        let mut output = VecDataOutput::new();
        
        <Vec<i32> as SerializableType>::serialize(&vec_value, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <Vec<i32> as SerializableType>::deserialize(&mut input).unwrap();
        
        assert_eq!(deserialized, vec_value);
    }

    #[test]
    fn test_string_serialization() {
        let string_value = "Hello, World!".to_string();
        let mut output = VecDataOutput::new();
        
        <String as SerializableType>::serialize(&string_value, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <String as SerializableType>::deserialize(&mut input).unwrap();
        
        assert_eq!(deserialized, string_value);
    }

    #[test]
    fn test_complex_nested_structure() {
        let nested = Box::new(vec![
            Rc::new("Hello".to_string()),
            Rc::new("World".to_string()),
        ]);
        
        let mut output = VecDataOutput::new();
        <Box<Vec<Rc<String>>> as SerializableType>::serialize(&nested, &mut output).unwrap();
        let bytes = output.into_vec();
        
        let mut input = SliceDataInput::new(&bytes);
        let deserialized = <Box<Vec<Rc<String>>> as SerializableType>::deserialize(&mut input).unwrap();
        
        assert_eq!(deserialized.len(), 2);
        assert_eq!(*deserialized[0], "Hello");
        assert_eq!(*deserialized[1], "World");
    }
}