asupersync 0.3.1

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Migration path and backward compatibility layer.
//!
//! Allows gradual adoption of RaptorQ symbol-native operations while
//! maintaining compatibility with existing traditional code paths.
//! Features can be enabled individually via [`MigrationBuilder`].

use std::collections::{HashMap, HashSet};
use std::marker::PhantomData;

use serde::Serialize;
use serde::de::DeserializeOwned;

use crate::config::EncodingConfig;
use crate::types::symbol::ObjectId;

// ============================================================================
// MigrationMode
// ============================================================================

/// Controls how operations handle dual-mode values.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum MigrationMode {
    /// Only use traditional mode (no RaptorQ).
    TraditionalOnly,
    /// Default to traditional, RaptorQ opt-in.
    #[default]
    PreferTraditional,
    /// Use RaptorQ when beneficial, fall back to traditional.
    Adaptive,
    /// Default to RaptorQ, traditional opt-in.
    PreferSymbolNative,
    /// Only use RaptorQ (errors on traditional-only operations).
    SymbolNativeOnly,
}

impl MigrationMode {
    /// Whether to use RaptorQ for a given operation.
    ///
    /// Explicit hints always override the mode. In `Adaptive` mode,
    /// payloads larger than 1024 bytes default to RaptorQ.
    #[must_use]
    pub fn should_use_raptorq(&self, hint: Option<bool>, data_size: usize) -> bool {
        match (self, hint) {
            // Explicit hints always win; otherwise prefer symbol-native modes.
            (_, Some(true)) | (Self::SymbolNativeOnly | Self::PreferSymbolNative, None) => true,
            (_, Some(false)) | (Self::TraditionalOnly | Self::PreferTraditional, None) => false,
            // Adaptive mode uses size heuristic
            (Self::Adaptive, None) => data_size > 1024,
        }
    }
}

// ============================================================================
// MigrationFeature
// ============================================================================

/// Individual features that can be toggled during migration.
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq)]
pub enum MigrationFeature {
    /// Enable RaptorQ for join operations.
    JoinEncoding,
    /// Enable RaptorQ for race operations.
    RaceEncoding,
    /// Enable distributed region encoding.
    DistributedRegions,
    /// Enable symbol-based cancellation.
    SymbolCancellation,
    /// Enable symbol-based tracing.
    SymbolTracing,
    /// Enable epoch barriers.
    EpochBarriers,
}

impl MigrationFeature {
    /// Returns an iterator over all features.
    pub fn all() -> impl Iterator<Item = Self> {
        [
            Self::JoinEncoding,
            Self::RaceEncoding,
            Self::DistributedRegions,
            Self::SymbolCancellation,
            Self::SymbolTracing,
            Self::EpochBarriers,
        ]
        .into_iter()
    }
}

// ============================================================================
// MigrationConfig
// ============================================================================

/// Active migration configuration.
#[derive(Debug, Clone, Default)]
pub struct MigrationConfig {
    /// Enabled features.
    features: HashSet<MigrationFeature>,
    /// Global migration mode.
    mode: MigrationMode,
    /// Per-operation overrides.
    overrides: HashMap<String, MigrationMode>,
}

impl MigrationConfig {
    /// Returns true if a feature is enabled.
    #[must_use]
    pub fn is_enabled(&self, feature: MigrationFeature) -> bool {
        self.features.contains(&feature)
    }

    /// Returns the global migration mode.
    #[must_use]
    pub fn mode(&self) -> MigrationMode {
        self.mode
    }

    /// Returns the set of enabled features.
    #[must_use]
    pub fn enabled_features(&self) -> &HashSet<MigrationFeature> {
        &self.features
    }

    /// Returns the mode override for a specific operation, if set.
    #[must_use]
    pub fn mode_for(&self, operation: &str) -> MigrationMode {
        self.overrides.get(operation).copied().unwrap_or(self.mode)
    }
}

// ============================================================================
// MigrationBuilder
// ============================================================================

/// Builder for [`MigrationConfig`].
#[derive(Debug, Default)]
pub struct MigrationBuilder {
    /// Features to enable.
    features: HashSet<MigrationFeature>,
    /// Global mode.
    mode: MigrationMode,
    /// Per-operation overrides.
    overrides: HashMap<String, MigrationMode>,
}

impl MigrationBuilder {
    /// Creates a new builder with defaults.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable a specific migration feature.
    #[must_use]
    pub fn enable(mut self, feature: MigrationFeature) -> Self {
        self.features.insert(feature);
        self
    }

    /// Disable a specific feature.
    #[must_use]
    pub fn disable(mut self, feature: MigrationFeature) -> Self {
        self.features.remove(&feature);
        self
    }

    /// Set the global migration mode.
    #[must_use]
    pub fn with_mode(mut self, mode: MigrationMode) -> Self {
        self.mode = mode;
        self
    }

    /// Override the mode for a specific operation.
    #[must_use]
    pub fn override_operation(mut self, operation: impl Into<String>, mode: MigrationMode) -> Self {
        self.overrides.insert(operation.into(), mode);
        self
    }

    /// Enable all features (full RaptorQ mode).
    #[must_use]
    pub fn full_raptorq(mut self) -> Self {
        self.features = MigrationFeature::all().collect();
        self.mode = MigrationMode::SymbolNativeOnly;
        self
    }

    /// Build the migration configuration.
    #[must_use]
    pub fn build(self) -> MigrationConfig {
        MigrationConfig {
            features: self.features,
            mode: self.mode,
            overrides: self.overrides,
        }
    }
}

/// Entry point for configuring migration.
#[must_use]
pub fn configure_migration() -> MigrationBuilder {
    MigrationBuilder::new()
}

// ============================================================================
// DualValueError
// ============================================================================

/// Errors from [`DualValue`] operations.
#[derive(Debug)]
pub enum DualValueError {
    /// Serialization to symbol encoding failed.
    SerializationFailed(String),
    /// Deserialization from symbol encoding failed.
    DeserializationFailed(String),
}

impl std::fmt::Display for DualValueError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SerializationFailed(msg) => write!(f, "serialization failed: {msg}"),
            Self::DeserializationFailed(msg) => write!(f, "deserialization failed: {msg}"),
        }
    }
}

impl std::error::Error for DualValueError {}

// ============================================================================
// DualValue
// ============================================================================

/// A value that can be held in either traditional or symbol-encoded form.
///
/// In traditional mode, the value is stored directly. In symbol-encoded mode,
/// the value is serialized and can be transmitted as symbols. Both forms
/// support retrieving the underlying value via [`get`][DualValue::get].
pub enum DualValue<T> {
    /// Traditional direct value.
    Traditional(T),
    /// Symbol-encoded value with serialized bytes and metadata.
    SymbolNative {
        /// Serialized representation.
        serialized: Vec<u8>,
        /// Object identifier.
        object_id: ObjectId,
        /// Type marker.
        _phantom: PhantomData<T>,
    },
}

impl<T> DualValue<T> {
    /// Returns true if this value is in symbol-encoded form.
    #[must_use]
    pub fn uses_raptorq(&self) -> bool {
        matches!(self, Self::SymbolNative { .. })
    }

    /// Returns true if this value is in traditional form.
    #[must_use]
    pub fn is_traditional(&self) -> bool {
        matches!(self, Self::Traditional(_))
    }
}

impl<T: Clone + Serialize + DeserializeOwned> DualValue<T> {
    /// Retrieves the underlying value, deserializing if necessary.
    pub fn get(&self) -> Result<T, DualValueError> {
        match self {
            Self::Traditional(v) => Ok(v.clone()),
            Self::SymbolNative { serialized, .. } => serde_json::from_slice(serialized)
                .map_err(|e| DualValueError::DeserializationFailed(e.to_string())),
        }
    }

    /// Converts to symbol-encoded form if not already.
    ///
    /// The `_config` parameter is reserved for future use with actual
    /// RaptorQ encoding configuration.
    pub fn ensure_symbols(&mut self, _config: &EncodingConfig) -> Result<(), DualValueError> {
        if let Self::Traditional(v) = self {
            let serialized = serde_json::to_vec(v)
                .map_err(|e| DualValueError::SerializationFailed(e.to_string()))?;
            let object_id = ObjectId::new_for_test(0);
            *self = Self::SymbolNative {
                serialized,
                object_id,
                _phantom: PhantomData,
            };
        }
        Ok(())
    }
}

impl<T: std::fmt::Debug> std::fmt::Debug for DualValue<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Traditional(v) => f.debug_tuple("Traditional").field(v).finish(),
            Self::SymbolNative {
                serialized,
                object_id,
                ..
            } => f
                .debug_struct("SymbolNative")
                .field("bytes", &serialized.len())
                .field("object_id", object_id)
                .finish(),
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_dual_value_traditional() {
        let value = DualValue::Traditional(42i32);
        assert_eq!(value.get().unwrap(), 42);
        assert!(!value.uses_raptorq());
    }

    #[test]
    fn test_dual_value_conversion() {
        let mut value = DualValue::Traditional("hello".to_string());
        let config = EncodingConfig::default();

        // Convert to symbol-native
        value.ensure_symbols(&config).unwrap();
        assert!(matches!(value, DualValue::SymbolNative { .. }));

        // Still get same value
        assert_eq!(value.get().unwrap(), "hello".to_string());
    }

    #[test]
    fn test_migration_mode_decisions() {
        // Traditional only never uses RaptorQ
        assert!(!MigrationMode::TraditionalOnly.should_use_raptorq(None, 10000));

        // Symbol-native only always uses RaptorQ
        assert!(MigrationMode::SymbolNativeOnly.should_use_raptorq(None, 10));

        // Hints override mode
        assert!(MigrationMode::TraditionalOnly.should_use_raptorq(Some(true), 10));
        assert!(!MigrationMode::SymbolNativeOnly.should_use_raptorq(Some(false), 10));

        // Adaptive uses heuristics
        assert!(!MigrationMode::Adaptive.should_use_raptorq(None, 100));
        assert!(MigrationMode::Adaptive.should_use_raptorq(None, 10000));
    }

    #[test]
    fn test_migration_builder() {
        let config = configure_migration()
            .enable(MigrationFeature::JoinEncoding)
            .enable(MigrationFeature::RaceEncoding)
            .build();

        assert!(config.is_enabled(MigrationFeature::JoinEncoding));
        assert!(config.is_enabled(MigrationFeature::RaceEncoding));
        assert!(!config.is_enabled(MigrationFeature::DistributedRegions));
    }

    #[test]
    fn test_full_raptorq_mode() {
        let config = configure_migration().full_raptorq().build();

        for feature in MigrationFeature::all() {
            assert!(config.is_enabled(feature));
        }
    }

    #[test]
    fn test_migration_mode_default() {
        let mode = MigrationMode::default();
        assert_eq!(mode, MigrationMode::PreferTraditional);
    }

    #[test]
    fn test_migration_builder_disable() {
        let config = configure_migration()
            .full_raptorq()
            .disable(MigrationFeature::SymbolTracing)
            .build();

        assert!(config.is_enabled(MigrationFeature::JoinEncoding));
        assert!(!config.is_enabled(MigrationFeature::SymbolTracing));
    }

    #[test]
    fn test_per_operation_override() {
        let config = configure_migration()
            .with_mode(MigrationMode::PreferTraditional)
            .override_operation("heavy_join", MigrationMode::PreferSymbolNative)
            .build();

        assert_eq!(config.mode(), MigrationMode::PreferTraditional);
        assert_eq!(
            config.mode_for("heavy_join"),
            MigrationMode::PreferSymbolNative
        );
        assert_eq!(
            config.mode_for("other_op"),
            MigrationMode::PreferTraditional
        );
    }

    // ---- DualValueError ----

    #[test]
    fn dual_value_error_display_serialization() {
        let err = DualValueError::SerializationFailed("bad input".into());
        assert_eq!(err.to_string(), "serialization failed: bad input");
    }

    #[test]
    fn dual_value_error_display_deserialization() {
        let err = DualValueError::DeserializationFailed("unexpected EOF".into());
        assert_eq!(err.to_string(), "deserialization failed: unexpected EOF");
    }

    #[test]
    fn dual_value_error_source_is_none() {
        use std::error::Error;
        let err = DualValueError::SerializationFailed("x".into());
        assert!(err.source().is_none());
    }

    // ---- DualValue predicates ----

    #[test]
    fn dual_value_is_traditional() {
        let val = DualValue::Traditional(100u32);
        assert!(val.is_traditional());
        assert!(!val.uses_raptorq());
    }

    #[test]
    fn dual_value_uses_raptorq_after_ensure_symbols() {
        let mut val = DualValue::Traditional(42u32);
        let config = EncodingConfig::default();
        val.ensure_symbols(&config).unwrap();
        assert!(val.uses_raptorq());
        assert!(!val.is_traditional());
    }

    #[test]
    fn dual_value_ensure_symbols_idempotent() {
        let mut val = DualValue::Traditional(42u32);
        let config = EncodingConfig::default();
        val.ensure_symbols(&config).unwrap();
        assert!(val.uses_raptorq());
        // Second call should be a no-op (already SymbolNative)
        val.ensure_symbols(&config).unwrap();
        assert!(val.uses_raptorq());
        assert_eq!(val.get().unwrap(), 42u32);
    }

    #[test]
    fn dual_value_get_deserialization_failure() {
        // Construct a SymbolNative with garbage bytes that won't parse as u32
        let bad = DualValue::<u32>::SymbolNative {
            serialized: b"not valid json".to_vec(),
            object_id: ObjectId::new_for_test(0),
            _phantom: PhantomData,
        };
        let err = bad.get().unwrap_err();
        assert!(matches!(err, DualValueError::DeserializationFailed(_)));
    }

    #[test]
    fn dual_value_debug_traditional() {
        let val = DualValue::Traditional(99i32);
        let dbg = format!("{val:?}");
        assert!(dbg.contains("Traditional"), "{dbg}");
        assert!(dbg.contains("99"), "{dbg}");
    }

    #[test]
    fn dual_value_debug_symbol_native() {
        let mut val = DualValue::Traditional("hello".to_string());
        let config = EncodingConfig::default();
        val.ensure_symbols(&config).unwrap();
        let dbg = format!("{val:?}");
        assert!(dbg.contains("SymbolNative"), "{dbg}");
        assert!(dbg.contains("bytes"), "{dbg}");
    }

    #[test]
    fn dual_value_ensure_symbols_serialization_failure() {
        #[derive(Clone)]
        struct FailingSerialize;

        impl Serialize for FailingSerialize {
            fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                Err(serde::ser::Error::custom("forced serialization failure"))
            }
        }

        impl<'de> serde::Deserialize<'de> for FailingSerialize {
            fn deserialize<D>(_deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                Ok(Self)
            }
        }

        let mut val = DualValue::Traditional(FailingSerialize);
        let config = EncodingConfig::default();
        let err = val.ensure_symbols(&config).unwrap_err();
        assert!(matches!(err, DualValueError::SerializationFailed(_)));
        assert!(
            val.is_traditional(),
            "failed conversion should preserve original"
        );
    }

    // ---- MigrationConfig ----

    #[test]
    fn migration_config_enabled_features_returns_set() {
        let config = configure_migration()
            .enable(MigrationFeature::EpochBarriers)
            .enable(MigrationFeature::SymbolCancellation)
            .build();

        let features = config.enabled_features();
        assert_eq!(features.len(), 2);
        assert!(features.contains(&MigrationFeature::EpochBarriers));
        assert!(features.contains(&MigrationFeature::SymbolCancellation));
    }

    #[test]
    fn migration_config_default_has_no_features() {
        let config = MigrationConfig::default();
        assert!(config.enabled_features().is_empty());
        assert_eq!(config.mode(), MigrationMode::PreferTraditional);
    }

    // ---- MigrationMode::Adaptive boundary ----

    #[test]
    fn adaptive_mode_boundary_at_1024() {
        // Exactly 1024 should NOT trigger RaptorQ (condition is > 1024)
        assert!(!MigrationMode::Adaptive.should_use_raptorq(None, 1024));
        // 1025 should trigger it
        assert!(MigrationMode::Adaptive.should_use_raptorq(None, 1025));
    }

    #[test]
    fn prefer_symbol_native_without_hint() {
        assert!(MigrationMode::PreferSymbolNative.should_use_raptorq(None, 0));
        assert!(MigrationMode::PreferSymbolNative.should_use_raptorq(None, 9999));
    }

    // ---- MigrationFeature ----

    #[test]
    fn migration_feature_all_has_six_items() {
        assert_eq!(MigrationFeature::all().count(), 6);
    }

    #[test]
    fn migration_feature_all_roundtrip_via_full_raptorq() {
        let config = configure_migration().full_raptorq().build();
        assert_eq!(config.mode(), MigrationMode::SymbolNativeOnly);
        for feature in MigrationFeature::all() {
            assert!(
                config.is_enabled(feature),
                "full_raptorq should enable {feature:?}"
            );
        }
    }

    // ---- MigrationBuilder ----

    #[test]
    fn migration_builder_with_mode() {
        let config = MigrationBuilder::new()
            .with_mode(MigrationMode::Adaptive)
            .build();
        assert_eq!(config.mode(), MigrationMode::Adaptive);
    }

    #[test]
    fn migration_builder_multiple_overrides() {
        let config = configure_migration()
            .override_operation("op_a", MigrationMode::SymbolNativeOnly)
            .override_operation("op_b", MigrationMode::TraditionalOnly)
            .build();
        assert_eq!(config.mode_for("op_a"), MigrationMode::SymbolNativeOnly);
        assert_eq!(config.mode_for("op_b"), MigrationMode::TraditionalOnly);
        // Fallback to global default
        assert_eq!(config.mode_for("op_c"), MigrationMode::PreferTraditional);
    }

    #[test]
    fn migration_mode_debug_clone_copy_default_eq() {
        let m = MigrationMode::Adaptive;
        let dbg = format!("{m:?}");
        assert!(dbg.contains("Adaptive"), "{dbg}");
        let copied: MigrationMode = m;
        let cloned = m;
        assert_eq!(copied, cloned);
        assert_eq!(MigrationMode::default(), MigrationMode::PreferTraditional);
        assert_ne!(m, MigrationMode::TraditionalOnly);
    }

    #[test]
    fn migration_feature_debug_clone_copy_hash_eq() {
        use std::collections::HashSet;
        let f = MigrationFeature::JoinEncoding;
        let dbg = format!("{f:?}");
        assert!(dbg.contains("JoinEncoding"), "{dbg}");
        let copied: MigrationFeature = f;
        let cloned = f;
        assert_eq!(copied, cloned);

        let mut set = HashSet::new();
        set.insert(MigrationFeature::JoinEncoding);
        set.insert(MigrationFeature::RaceEncoding);
        set.insert(MigrationFeature::DistributedRegions);
        assert_eq!(set.len(), 3);
    }

    #[test]
    fn migration_config_debug_clone_default() {
        let c = MigrationConfig::default();
        let dbg = format!("{c:?}");
        assert!(dbg.contains("MigrationConfig"), "{dbg}");
        let cloned = c;
        assert_eq!(format!("{cloned:?}"), dbg);
    }
}