crdtosphere 0.1.0

Universal embedded CRDTs for distributed coordination across automotive, robotics, IoT, and industrial applications
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
//! Last-Writer-Wins Register CRDT
//!
//! A register that resolves conflicts by keeping the value with the latest timestamp.
//! Uses zero allocation for deterministic memory usage.
//!
//! This module provides both standard and atomic implementations:
//! - Standard: Requires `&mut self` for modifications, single-threaded
//! - Atomic: Allows `&self` for modifications, multi-threaded safe

use crate::clock::CompactTimestamp;
use crate::error::{CRDTError, CRDTResult};
use crate::memory::{MemoryConfig, NodeId};
use crate::traits::{BoundedCRDT, CRDT, RealTimeCRDT};

#[cfg(feature = "hardware-atomic")]
use core::cell::UnsafeCell;
#[cfg(feature = "hardware-atomic")]
use core::sync::atomic::{AtomicU8, AtomicU32, Ordering};

#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Last-Writer-Wins Register
///
/// This register resolves conflicts by keeping the value with the latest timestamp.
/// All memory is statically allocated for deterministic memory usage.
///
/// # Type Parameters
/// - `T`: The value type stored in the register
/// - `C`: Memory configuration that determines limits
///
/// # Memory Usage
/// - Fixed size: ~16-32 bytes (depending on T)
/// - Completely predictable at compile time
///
/// # Feature Comparison
///
/// | Method | Standard | Atomic | Mutability | Thread Safety | Notes |
/// |--------|----------|--------|------------|---------------|-------|
/// | `set()` | ✅ | ✅ | `&mut self` / `&self` | Single / Multi | Update with timestamp |
/// | `get()` | ✅ | ✅ | `&self` | Single / Multi | Read-only |
/// | `merge()` | ✅ | ✅ | `&mut self` | Single / Multi | CRDT merge |
/// | `timestamp()` | ✅ | ✅ | `&self` | Single / Multi | Read-only |
/// | `current_node()` | ✅ | ✅ | `&self` | Single / Multi | Read-only |
/// | `is_empty()` | ✅ | ✅ | `&self` | Single / Multi | Read-only |
///
/// **Feature Requirements:**
/// - **Standard Version**: No additional features required (default)
/// - **Atomic Version**: Requires `hardware-atomic` feature
///
/// # Concurrency Behavior
/// - **Without `hardware-atomic`**: Requires `&mut self` for modifications, single-threaded only
/// - **With `hardware-atomic`**: Allows `&self` for modifications, thread-safe atomic operations
///
/// # Example
/// ```rust
/// use crdtosphere::prelude::*;
///
/// // Create a register for sensor readings
/// let mut sensor = LWWRegister::<f32, DefaultConfig>::new(1);
/// sensor.set(23.5, 1000)?;
///
/// // Merge with another node's data
/// let mut other = LWWRegister::<f32, DefaultConfig>::new(2);
/// other.set(24.1, 1001)?;
/// sensor.merge(&other)?;
///
/// assert_eq!(sensor.get(), Some(&24.1)); // Latest value wins
/// # Ok::<(), crdtosphere::error::CRDTError>(())
/// ```
#[derive(Debug)]
pub struct LWWRegister<T, C: MemoryConfig> {
    /// Current value and its metadata
    #[cfg(not(feature = "hardware-atomic"))]
    current_value: Option<T>,
    #[cfg(not(feature = "hardware-atomic"))]
    current_timestamp: CompactTimestamp,
    #[cfg(not(feature = "hardware-atomic"))]
    current_node_id: NodeId,

    /// Atomic version uses separate storage for coordination
    #[cfg(feature = "hardware-atomic")]
    current_value: UnsafeCell<Option<T>>,
    #[cfg(feature = "hardware-atomic")]
    current_timestamp: AtomicU32,
    #[cfg(feature = "hardware-atomic")]
    current_node_id: AtomicU8,

    /// This node's ID
    node_id: NodeId,

    /// Phantom data to maintain the memory config type
    _phantom: core::marker::PhantomData<C>,
}

// SAFETY: The atomic version is safe to share between threads because:
// 1. All access to current_value is protected by atomic timestamp coordination
// 2. Only one thread can successfully update at a time via compare_exchange
// 3. UnsafeCell is only accessed after winning the atomic coordination
#[cfg(feature = "hardware-atomic")]
unsafe impl<T, C: MemoryConfig> Sync for LWWRegister<T, C>
where
    T: Send,
    C: Send + Sync,
{
}

// Implement Clone manually due to atomic types not implementing Clone
impl<T, C: MemoryConfig> Clone for LWWRegister<T, C>
where
    T: Clone,
{
    fn clone(&self) -> Self {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            Self {
                current_value: self.current_value.clone(),
                current_timestamp: self.current_timestamp,
                current_node_id: self.current_node_id,
                node_id: self.node_id,
                _phantom: core::marker::PhantomData,
            }
        }

        #[cfg(feature = "hardware-atomic")]
        {
            // For atomic version, we need to manually clone the UnsafeCell content
            let cloned_value = unsafe { (*self.current_value.get()).clone() };
            Self {
                current_value: UnsafeCell::new(cloned_value),
                current_timestamp: AtomicU32::new(self.current_timestamp.load(Ordering::Relaxed)),
                current_node_id: AtomicU8::new(self.current_node_id.load(Ordering::Relaxed)),
                node_id: self.node_id,
                _phantom: core::marker::PhantomData,
            }
        }
    }
}

impl<T, C: MemoryConfig> LWWRegister<T, C>
where
    T: Clone + PartialEq,
{
    /// Creates a new LWW register for the given node
    ///
    /// # Arguments
    /// * `node_id` - The ID of this node (must be < MAX_NODES)
    ///
    /// # Returns
    /// A new empty register
    ///
    /// # Example
    /// ```rust
    /// use crdtosphere::prelude::*;
    /// let register = LWWRegister::<i32, DefaultConfig>::new(1);
    /// ```
    pub fn new(node_id: NodeId) -> Self {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            Self {
                current_value: None,
                current_timestamp: CompactTimestamp::zero(),
                current_node_id: 0,
                node_id,
                _phantom: core::marker::PhantomData,
            }
        }

        #[cfg(feature = "hardware-atomic")]
        {
            Self {
                current_value: UnsafeCell::new(None),
                current_timestamp: AtomicU32::new(0),
                current_node_id: AtomicU8::new(0),
                node_id,
                _phantom: core::marker::PhantomData,
            }
        }
    }

    /// Sets a new value with the current timestamp
    ///
    /// # Concurrency Behavior
    /// - **Without `hardware-atomic`**: Requires `&mut self`, single-threaded only
    /// - **With `hardware-atomic`**: Allows `&self`, thread-safe atomic operations
    ///
    /// # Arguments
    /// * `value` - The new value to set
    /// * `timestamp` - The timestamp for this update
    ///
    /// # Returns
    /// Ok(()) if successful, or an error if the timestamp is older than current
    ///
    /// # Examples
    ///
    /// ## Standard Version (default)
    /// ```rust
    /// use crdtosphere::prelude::*;
    /// let mut register = LWWRegister::<i32, DefaultConfig>::new(1);
    /// register.set(42, 1000)?; // Requires &mut self
    /// assert_eq!(register.get(), Some(&42));
    /// # Ok::<(), crdtosphere::error::CRDTError>(())
    /// ```
    ///
    /// ## Atomic Version (with `hardware-atomic` feature)
    /// ```toml
    /// [dependencies]
    /// crdtosphere = { features = ["hardware-atomic"] }
    /// ```
    /// ```rust,ignore
    /// # // This example requires hardware-atomic feature
    /// use crdtosphere::prelude::*;
    /// let register = LWWRegister::<i32, DefaultConfig>::new(1);
    /// register.set(42, 1000)?; // Works with &self (atomic)
    /// assert_eq!(register.get(), Some(&42));
    /// # Ok::<(), crdtosphere::error::CRDTError>(())
    /// ```
    #[cfg(not(feature = "hardware-atomic"))]
    pub fn set(&mut self, value: T, timestamp: u64) -> CRDTResult<()> {
        let new_timestamp = CompactTimestamp::new(timestamp);

        // Only update if this timestamp is newer (or same timestamp but higher node ID)
        if self.should_update(&new_timestamp, self.node_id) {
            self.current_value = Some(value);
            self.current_timestamp = new_timestamp;
            self.current_node_id = self.node_id;
        }

        Ok(())
    }

    /// Sets a new value with the current timestamp (atomic version)
    ///
    /// # Arguments
    /// * `value` - The new value to set
    /// * `timestamp` - The timestamp for this update
    ///
    /// # Returns
    /// Ok(()) if successful, or an error if the timestamp is older than current
    ///
    /// # Note
    /// In the atomic version, we use unsafe code to update the value after
    /// successfully updating the timestamp atomically. This is safe because
    /// we ensure only one thread can update at a time through the atomic timestamp.
    #[cfg(feature = "hardware-atomic")]
    pub fn set(&self, value: T, timestamp: u64) -> CRDTResult<()> {
        let new_timestamp_u32 = timestamp as u32; // Truncate to u32 for ARM compatibility

        // Atomic compare-exchange loop to update timestamp and node_id together
        loop {
            let current_timestamp = self.current_timestamp.load(Ordering::Relaxed);
            let current_node_id = self.current_node_id.load(Ordering::Relaxed);

            // Check if we should update
            let should_update = if current_timestamp == 0 {
                true // No current value
            } else if new_timestamp_u32 > current_timestamp {
                true // Newer timestamp
            } else if new_timestamp_u32 == current_timestamp {
                self.node_id > current_node_id // Same timestamp, higher node ID wins
            } else {
                false // Older timestamp
            };

            if !should_update {
                return Ok(());
            }

            // Try to atomically update timestamp
            match self.current_timestamp.compare_exchange_weak(
                current_timestamp,
                new_timestamp_u32,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => {
                    // Successfully updated timestamp, now update node_id and value
                    self.current_node_id.store(self.node_id, Ordering::Relaxed);

                    // SAFETY: We have exclusive access to update the value because we
                    // successfully updated the timestamp atomically. Only one thread
                    // can succeed in the compare_exchange above.
                    unsafe {
                        *self.current_value.get() = Some(value);
                    }
                    break;
                }
                Err(_) => {
                    // Retry the loop
                    continue;
                }
            }
        }

        Ok(())
    }

    /// Gets the current value
    ///
    /// # Returns
    /// The current value, or None if no value has been set
    ///
    /// # Example
    /// ```rust
    /// use crdtosphere::prelude::*;
    /// let mut register = LWWRegister::<i32, DefaultConfig>::new(1);
    /// assert_eq!(register.get(), None);
    /// register.set(42, 1000)?;
    /// assert_eq!(register.get(), Some(&42));
    /// # Ok::<(), crdtosphere::error::CRDTError>(())
    /// ```
    pub fn get(&self) -> Option<&T> {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            self.current_value.as_ref()
        }

        #[cfg(feature = "hardware-atomic")]
        {
            unsafe { (*self.current_value.get()).as_ref() }
        }
    }

    /// Gets the current timestamp
    ///
    /// # Returns
    /// The timestamp of the current value
    pub fn timestamp(&self) -> CompactTimestamp {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            self.current_timestamp
        }

        #[cfg(feature = "hardware-atomic")]
        {
            CompactTimestamp::new(self.current_timestamp.load(Ordering::Relaxed) as u64)
        }
    }

    /// Gets the node ID that set the current value
    ///
    /// # Returns
    /// The node ID of the current value's author
    pub fn current_node(&self) -> NodeId {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            self.current_node_id
        }

        #[cfg(feature = "hardware-atomic")]
        {
            self.current_node_id.load(Ordering::Relaxed)
        }
    }

    /// Checks if this register has a value
    ///
    /// # Returns
    /// true if a value is set, false otherwise
    pub fn is_empty(&self) -> bool {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            self.current_value.is_none()
        }

        #[cfg(feature = "hardware-atomic")]
        {
            unsafe { (*self.current_value.get()).is_none() }
        }
    }

    /// Determines if we should update with a new timestamp and node ID
    #[cfg(not(feature = "hardware-atomic"))]
    fn should_update(&self, new_timestamp: &CompactTimestamp, new_node_id: NodeId) -> bool {
        if self.current_value.is_none() {
            return true;
        }

        match new_timestamp.cmp(&self.current_timestamp) {
            core::cmp::Ordering::Greater => true,
            core::cmp::Ordering::Less => false,
            core::cmp::Ordering::Equal => {
                // Same timestamp - use node ID as tiebreaker (higher wins)
                new_node_id > self.current_node_id
            }
        }
    }
}

// Serde implementation for LWWRegister
#[cfg(feature = "serde")]
impl<T, C: MemoryConfig> Serialize for LWWRegister<T, C>
where
    T: Serialize + Clone + PartialEq,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeStruct;

        let mut state = serializer.serialize_struct("LWWRegister", 4)?;

        // Serialize the logical state
        #[cfg(not(feature = "hardware-atomic"))]
        {
            state.serialize_field("current_value", &self.current_value)?;
            state.serialize_field("current_timestamp", &self.current_timestamp.as_u64())?;
            state.serialize_field("current_node_id", &self.current_node_id)?;
        }

        #[cfg(feature = "hardware-atomic")]
        {
            // For atomic version, we need to extract values safely
            let current_value = unsafe { &*self.current_value.get() };
            let current_timestamp = self.current_timestamp.load(Ordering::Relaxed) as u64;
            let current_node_id = self.current_node_id.load(Ordering::Relaxed);

            state.serialize_field("current_value", current_value)?;
            state.serialize_field("current_timestamp", &current_timestamp)?;
            state.serialize_field("current_node_id", &current_node_id)?;
        }

        state.serialize_field("node_id", &self.node_id)?;
        state.end()
    }
}

#[cfg(feature = "serde")]
impl<'de, T, C: MemoryConfig> Deserialize<'de> for LWWRegister<T, C>
where
    T: Deserialize<'de> + Clone + PartialEq,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use core::fmt;
        use serde::de::{self, MapAccess, Visitor};

        #[derive(Deserialize)]
        #[serde(field_identifier, rename_all = "snake_case")]
        enum Field {
            CurrentValue,
            CurrentTimestamp,
            CurrentNodeId,
            NodeId,
        }

        struct LWWRegisterVisitor<T, C: MemoryConfig> {
            _phantom: core::marker::PhantomData<(T, C)>,
        }

        impl<'de, T, C: MemoryConfig> Visitor<'de> for LWWRegisterVisitor<T, C>
        where
            T: Deserialize<'de> + Clone + PartialEq,
        {
            type Value = LWWRegister<T, C>;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct LWWRegister")
            }

            fn visit_map<V>(self, mut map: V) -> Result<LWWRegister<T, C>, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut current_value = None;
                let mut current_timestamp = None;
                let mut current_node_id = None;
                let mut node_id = None;

                while let Some(key) = map.next_key()? {
                    match key {
                        Field::CurrentValue => {
                            if current_value.is_some() {
                                return Err(de::Error::duplicate_field("current_value"));
                            }
                            current_value = Some(map.next_value::<Option<T>>()?);
                        }
                        Field::CurrentTimestamp => {
                            if current_timestamp.is_some() {
                                return Err(de::Error::duplicate_field("current_timestamp"));
                            }
                            current_timestamp = Some(map.next_value::<u64>()?);
                        }
                        Field::CurrentNodeId => {
                            if current_node_id.is_some() {
                                return Err(de::Error::duplicate_field("current_node_id"));
                            }
                            current_node_id = Some(map.next_value::<NodeId>()?);
                        }
                        Field::NodeId => {
                            if node_id.is_some() {
                                return Err(de::Error::duplicate_field("node_id"));
                            }
                            node_id = Some(map.next_value::<NodeId>()?);
                        }
                    }
                }

                let current_value =
                    current_value.ok_or_else(|| de::Error::missing_field("current_value"))?;
                let current_timestamp = current_timestamp
                    .ok_or_else(|| de::Error::missing_field("current_timestamp"))?;
                let current_node_id =
                    current_node_id.ok_or_else(|| de::Error::missing_field("current_node_id"))?;
                let node_id = node_id.ok_or_else(|| de::Error::missing_field("node_id"))?;

                // Reconstruct the LWWRegister
                #[cfg(not(feature = "hardware-atomic"))]
                {
                    Ok(LWWRegister {
                        current_value,
                        current_timestamp: CompactTimestamp::new(current_timestamp),
                        current_node_id,
                        node_id,
                        _phantom: core::marker::PhantomData,
                    })
                }

                #[cfg(feature = "hardware-atomic")]
                {
                    Ok(LWWRegister {
                        current_value: UnsafeCell::new(current_value),
                        current_timestamp: AtomicU32::new(current_timestamp as u32),
                        current_node_id: AtomicU8::new(current_node_id),
                        node_id,
                        _phantom: core::marker::PhantomData,
                    })
                }
            }
        }

        const FIELDS: &[&str] = &[
            "current_value",
            "current_timestamp",
            "current_node_id",
            "node_id",
        ];
        deserializer.deserialize_struct(
            "LWWRegister",
            FIELDS,
            LWWRegisterVisitor {
                _phantom: core::marker::PhantomData,
            },
        )
    }
}

impl<T, C: MemoryConfig> CRDT<C> for LWWRegister<T, C>
where
    T: Clone + PartialEq + core::fmt::Debug,
{
    type Error = CRDTError;

    fn merge(&mut self, other: &Self) -> CRDTResult<()> {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            if let Some(ref other_value) = other.current_value {
                if self.should_update(&other.current_timestamp, other.current_node_id) {
                    self.current_value = Some(other_value.clone());
                    self.current_timestamp = other.current_timestamp;
                    self.current_node_id = other.current_node_id;
                }
            }
        }

        #[cfg(feature = "hardware-atomic")]
        {
            let other_value_ref = unsafe { &*other.current_value.get() };
            if let Some(other_value) = other_value_ref {
                let other_timestamp = other.current_timestamp.load(Ordering::Relaxed);
                let other_node_id = other.current_node_id.load(Ordering::Relaxed);

                // Atomic compare-exchange loop for merge
                loop {
                    let current_timestamp = self.current_timestamp.load(Ordering::Relaxed);
                    let current_node_id = self.current_node_id.load(Ordering::Relaxed);

                    // Check if we should update
                    let should_update = if current_timestamp == 0 {
                        true // No current value
                    } else if other_timestamp > current_timestamp {
                        true // Newer timestamp
                    } else if other_timestamp == current_timestamp {
                        other_node_id > current_node_id // Same timestamp, higher node ID wins
                    } else {
                        false // Older timestamp
                    };

                    if !should_update {
                        break;
                    }

                    // Try to atomically update timestamp
                    match self.current_timestamp.compare_exchange_weak(
                        current_timestamp,
                        other_timestamp,
                        Ordering::Relaxed,
                        Ordering::Relaxed,
                    ) {
                        Ok(_) => {
                            // Successfully updated timestamp, now update node_id and value
                            self.current_node_id.store(other_node_id, Ordering::Relaxed);

                            // SAFETY: We have exclusive access to update the value because we
                            // successfully updated the timestamp atomically. Only one thread
                            // can succeed in the compare_exchange above.
                            unsafe {
                                *self.current_value.get() = Some(other_value.clone());
                            }
                            break;
                        }
                        Err(_) => {
                            // Retry the loop
                            continue;
                        }
                    }
                }
            }
        }
        Ok(())
    }

    fn eq(&self, other: &Self) -> bool {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            self.current_value == other.current_value
                && self.current_timestamp == other.current_timestamp
                && self.current_node_id == other.current_node_id
        }

        #[cfg(feature = "hardware-atomic")]
        {
            unsafe {
                (*self.current_value.get()) == (*other.current_value.get())
                    && self.current_timestamp.load(Ordering::Relaxed)
                        == other.current_timestamp.load(Ordering::Relaxed)
                    && self.current_node_id.load(Ordering::Relaxed)
                        == other.current_node_id.load(Ordering::Relaxed)
            }
        }
    }

    fn size_bytes(&self) -> usize {
        core::mem::size_of::<Self>()
    }

    fn validate(&self) -> CRDTResult<()> {
        // Validate node ID is within bounds
        if self.node_id as usize >= C::MAX_NODES {
            return Err(CRDTError::InvalidNodeId);
        }

        // Validate current node ID is within bounds
        #[cfg(not(feature = "hardware-atomic"))]
        {
            if self.current_node_id as usize >= C::MAX_NODES {
                return Err(CRDTError::InvalidNodeId);
            }
        }

        #[cfg(feature = "hardware-atomic")]
        {
            if self.current_node_id.load(Ordering::Relaxed) as usize >= C::MAX_NODES {
                return Err(CRDTError::InvalidNodeId);
            }
        }

        Ok(())
    }

    fn state_hash(&self) -> u32 {
        // Simple hash based on current state
        let mut hash = 0u32;

        #[cfg(not(feature = "hardware-atomic"))]
        {
            if let Some(ref _value) = self.current_value {
                hash ^= self.current_timestamp.as_u64() as u32;
                hash ^= (self.current_node_id as u32) << 16;
            }
        }

        #[cfg(feature = "hardware-atomic")]
        {
            unsafe {
                if let Some(_value) = &*self.current_value.get() {
                    hash ^= self.current_timestamp.load(Ordering::Relaxed) as u32;
                    hash ^= (self.current_node_id.load(Ordering::Relaxed) as u32) << 16;
                }
            }
        }

        hash
    }

    fn can_merge(&self, _other: &Self) -> bool {
        // LWW registers can always merge
        true
    }
}

impl<T, C: MemoryConfig> BoundedCRDT<C> for LWWRegister<T, C>
where
    T: Clone + PartialEq + core::fmt::Debug,
{
    const MAX_SIZE_BYTES: usize = core::mem::size_of::<Self>();
    const MAX_ELEMENTS: usize = 1; // A register holds one value

    fn memory_usage(&self) -> usize {
        core::mem::size_of::<Self>()
    }

    fn element_count(&self) -> usize {
        #[cfg(not(feature = "hardware-atomic"))]
        {
            if self.current_value.is_some() { 1 } else { 0 }
        }

        #[cfg(feature = "hardware-atomic")]
        {
            unsafe {
                if (*self.current_value.get()).is_some() {
                    1
                } else {
                    0
                }
            }
        }
    }

    fn compact(&mut self) -> CRDTResult<usize> {
        // LWW registers don't have anything to compact
        Ok(0)
    }

    fn can_add_element(&self) -> bool {
        // For registers, we can always "add" (update) if not at max capacity
        self.element_count() < Self::MAX_ELEMENTS
    }
}

impl<T, C: MemoryConfig> RealTimeCRDT<C> for LWWRegister<T, C>
where
    T: Clone + PartialEq + core::fmt::Debug,
{
    const MAX_MERGE_CYCLES: u32 = 100;
    const MAX_VALIDATE_CYCLES: u32 = 50;
    const MAX_SERIALIZE_CYCLES: u32 = 75;

    fn merge_bounded(&mut self, other: &Self) -> CRDTResult<()> {
        // LWW merge is always bounded - just a few comparisons and assignments
        self.merge(other)
    }

    fn validate_bounded(&self) -> CRDTResult<()> {
        // Validation is always bounded - just a few checks
        self.validate()
    }

    fn remaining_budget(&self) -> Option<u32> {
        // For this simple implementation, we don't track budget
        None
    }

    fn set_budget(&mut self, _cycles: u32) {
        // For this simple implementation, we don't track budget
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::DefaultConfig;

    #[test]
    fn test_new_register() {
        let register = LWWRegister::<i32, DefaultConfig>::new(1);
        assert!(register.is_empty());
        assert_eq!(register.get(), None);
        assert_eq!(register.node_id, 1);
    }

    #[test]
    fn test_set_and_get() {
        let mut register = LWWRegister::<i32, DefaultConfig>::new(1);

        assert!(register.set(42, 1000).is_ok());
        assert_eq!(register.get(), Some(&42));
        assert!(!register.is_empty());
        assert_eq!(register.current_node(), 1);
    }

    #[test]
    fn test_lww_semantics() {
        let mut register = LWWRegister::<i32, DefaultConfig>::new(1);

        // Set initial value
        register.set(10, 1000).unwrap();
        assert_eq!(register.get(), Some(&10));

        // Newer timestamp should win
        register.set(20, 2000).unwrap();
        assert_eq!(register.get(), Some(&20));

        // Older timestamp should be ignored
        register.set(30, 500).unwrap();
        assert_eq!(register.get(), Some(&20)); // Still 20
    }

    #[test]
    fn test_merge() {
        let mut register1 = LWWRegister::<i32, DefaultConfig>::new(1);
        let mut register2 = LWWRegister::<i32, DefaultConfig>::new(2);

        register1.set(10, 1000).unwrap();
        register2.set(20, 2000).unwrap();

        // Merge register2 into register1
        register1.merge(&register2).unwrap();
        assert_eq!(register1.get(), Some(&20)); // register2's value wins (newer)

        // Test reverse merge
        let mut register3 = LWWRegister::<i32, DefaultConfig>::new(3);
        register3.set(30, 500).unwrap(); // Older timestamp

        register1.merge(&register3).unwrap();
        assert_eq!(register1.get(), Some(&20)); // Still register2's value
    }

    #[test]
    fn test_tiebreaker() {
        let mut register1 = LWWRegister::<i32, DefaultConfig>::new(1);
        let mut register2 = LWWRegister::<i32, DefaultConfig>::new(2);

        register1.set(10, 1000).unwrap();
        register2.set(20, 1000).unwrap(); // Same timestamp

        register1.merge(&register2).unwrap();
        assert_eq!(register1.get(), Some(&20)); // Higher node ID wins
    }

    #[test]
    fn test_bounded_crdt() {
        let register = LWWRegister::<i32, DefaultConfig>::new(1);

        assert_eq!(register.element_count(), 0);
        assert!(register.memory_usage() > 0);
        assert!(register.can_add_element());
    }

    #[test]
    fn test_validation() {
        let register = LWWRegister::<i32, DefaultConfig>::new(1);
        assert!(register.validate().is_ok());

        // Test with invalid node ID would require creating an invalid state
        // which is hard to do with the current API (good!)
    }

    #[test]
    fn test_real_time_crdt() {
        let mut register1 = LWWRegister::<i32, DefaultConfig>::new(1);
        let register2 = LWWRegister::<i32, DefaultConfig>::new(2);

        assert!(register1.merge_bounded(&register2).is_ok());
        assert!(register1.validate_bounded().is_ok());
    }

    #[cfg(all(test, feature = "serde"))]
    mod serde_tests {
        use super::*;

        #[test]
        fn test_serialize_deserialize() {
            let mut register = LWWRegister::<i32, DefaultConfig>::new(1);
            register.set(42, 1000).unwrap();

            let mut other = LWWRegister::<i32, DefaultConfig>::new(2);
            other.set(100, 2000).unwrap();
            register.merge(&other).unwrap();

            // Test that the serde traits are implemented
            // This ensures the code compiles with serde feature
            assert_eq!(register.get(), Some(&100)); // Latest value wins
            assert_eq!(register.current_node(), 2);
            assert_eq!(register.timestamp().as_u64(), 2000);
        }

        #[test]
        fn test_atomic_vs_standard_compatibility() {
            // This test ensures that atomic and standard versions would serialize to the same format
            // The logical state should be identical regardless of internal representation
            let mut register = LWWRegister::<i32, DefaultConfig>::new(1);
            register.set(42, 1000).unwrap();

            // Both versions should have the same logical state
            assert_eq!(register.get(), Some(&42));
            assert_eq!(register.current_node(), 1);
            assert_eq!(register.timestamp().as_u64(), 1000);
        }

        #[test]
        fn test_empty_register_serialization() {
            let register = LWWRegister::<i32, DefaultConfig>::new(1);

            // Empty register should serialize correctly
            assert!(register.is_empty());
            assert_eq!(register.get(), None);
            assert_eq!(register.current_node(), 0);
        }
    }
}