2025-11-15 23:42:12 +00:00
|
|
|
use proc_macro::TokenStream;
|
2025-12-04 19:49:48 +00:00
|
|
|
use quote::quote;
|
2025-11-16 11:50:49 +00:00
|
|
|
use syn::{
|
2025-12-11 18:39:57 +00:00
|
|
|
DeriveInput,
|
|
|
|
|
parse_macro_input,
|
2025-11-16 11:50:49 +00:00
|
|
|
};
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Sync strategy types
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
enum SyncStrategy {
|
|
|
|
|
LastWriteWins,
|
|
|
|
|
Set,
|
|
|
|
|
Sequence,
|
|
|
|
|
Custom,
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
impl SyncStrategy {
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, String> {
|
|
|
|
|
match s {
|
2025-12-11 18:39:57 +00:00
|
|
|
| "LastWriteWins" => Ok(SyncStrategy::LastWriteWins),
|
|
|
|
|
| "Set" => Ok(SyncStrategy::Set),
|
|
|
|
|
| "Sequence" => Ok(SyncStrategy::Sequence),
|
|
|
|
|
| "Custom" => Ok(SyncStrategy::Custom),
|
|
|
|
|
| _ => Err(format!(
|
2025-12-04 19:49:48 +00:00
|
|
|
"Unknown strategy '{}'. Choose one of: \"LastWriteWins\", \"Set\", \"Sequence\", \"Custom\"",
|
|
|
|
|
s
|
|
|
|
|
)),
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
fn to_tokens(&self) -> proc_macro2::TokenStream {
|
|
|
|
|
match self {
|
2025-12-11 18:39:57 +00:00
|
|
|
| SyncStrategy::LastWriteWins => {
|
2025-12-13 22:22:05 +00:00
|
|
|
quote! { libmarathon::networking::SyncStrategy::LastWriteWins }
|
2025-12-11 18:39:57 +00:00
|
|
|
},
|
2025-12-13 22:22:05 +00:00
|
|
|
| SyncStrategy::Set => quote! { libmarathon::networking::SyncStrategy::Set },
|
|
|
|
|
| SyncStrategy::Sequence => quote! { libmarathon::networking::SyncStrategy::Sequence },
|
|
|
|
|
| SyncStrategy::Custom => quote! { libmarathon::networking::SyncStrategy::Custom },
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Parsed sync attributes
|
|
|
|
|
struct SyncAttributes {
|
|
|
|
|
version: u32,
|
|
|
|
|
strategy: SyncStrategy,
|
|
|
|
|
persist: bool,
|
|
|
|
|
lazy: bool,
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
impl SyncAttributes {
|
|
|
|
|
fn parse(input: &DeriveInput) -> Result<Self, syn::Error> {
|
|
|
|
|
let mut version: Option<u32> = None;
|
|
|
|
|
let mut strategy: Option<SyncStrategy> = None;
|
|
|
|
|
let mut persist = true; // default
|
|
|
|
|
let mut lazy = false; // default
|
|
|
|
|
|
|
|
|
|
// Find the #[sync(...)] attribute
|
|
|
|
|
for attr in &input.attrs {
|
|
|
|
|
if !attr.path().is_ident("sync") {
|
|
|
|
|
continue;
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
attr.parse_nested_meta(|meta| {
|
|
|
|
|
if meta.path.is_ident("version") {
|
|
|
|
|
let value: syn::LitInt = meta.value()?.parse()?;
|
|
|
|
|
version = Some(value.base10_parse()?);
|
|
|
|
|
Ok(())
|
|
|
|
|
} else if meta.path.is_ident("strategy") {
|
|
|
|
|
let value: syn::LitStr = meta.value()?.parse()?;
|
|
|
|
|
let strategy_str = value.value();
|
|
|
|
|
strategy = Some(
|
|
|
|
|
SyncStrategy::from_str(&strategy_str)
|
2025-12-11 18:39:57 +00:00
|
|
|
.map_err(|e| syn::Error::new_spanned(&value, e))?,
|
2025-12-04 19:49:48 +00:00
|
|
|
);
|
|
|
|
|
Ok(())
|
|
|
|
|
} else if meta.path.is_ident("persist") {
|
|
|
|
|
let value: syn::LitBool = meta.value()?.parse()?;
|
|
|
|
|
persist = value.value;
|
|
|
|
|
Ok(())
|
|
|
|
|
} else if meta.path.is_ident("lazy") {
|
|
|
|
|
let value: syn::LitBool = meta.value()?.parse()?;
|
|
|
|
|
lazy = value.value;
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(meta.error("unrecognized sync attribute"))
|
|
|
|
|
}
|
|
|
|
|
})?;
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
// Require version and strategy
|
|
|
|
|
let version = version.ok_or_else(|| {
|
|
|
|
|
syn::Error::new(
|
|
|
|
|
proc_macro2::Span::call_site(),
|
|
|
|
|
"Missing required attribute `version`\n\
|
|
|
|
|
\n\
|
|
|
|
|
= help: Add #[sync(version = 1, strategy = \"...\")] to your struct\n\
|
2025-12-11 18:39:57 +00:00
|
|
|
= note: See documentation: https://docs.rs/lonni/sync/strategies.html",
|
2025-12-04 19:49:48 +00:00
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let strategy = strategy.ok_or_else(|| {
|
|
|
|
|
syn::Error::new(
|
|
|
|
|
proc_macro2::Span::call_site(),
|
|
|
|
|
"Missing required attribute `strategy`\n\
|
|
|
|
|
\n\
|
|
|
|
|
= help: Choose one of: \"LastWriteWins\", \"Set\", \"Sequence\", \"Custom\"\n\
|
|
|
|
|
= help: Add #[sync(version = 1, strategy = \"LastWriteWins\")] to your struct\n\
|
2025-12-11 18:39:57 +00:00
|
|
|
= note: See documentation: https://docs.rs/lonni/sync/strategies.html",
|
2025-12-04 19:49:48 +00:00
|
|
|
)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
Ok(SyncAttributes {
|
|
|
|
|
version,
|
|
|
|
|
strategy,
|
|
|
|
|
persist,
|
|
|
|
|
lazy,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// RFC 0003 macro: Generate SyncComponent trait implementation
|
|
|
|
|
///
|
|
|
|
|
/// # Example
|
|
|
|
|
/// ```ignore
|
|
|
|
|
/// use bevy::prelude::*;
|
2025-12-13 22:22:05 +00:00
|
|
|
/// use libmarathon::networking::Synced;
|
2025-12-04 19:49:48 +00:00
|
|
|
/// use sync_macros::Synced as SyncedDerive;
|
|
|
|
|
///
|
2025-12-17 19:20:34 +00:00
|
|
|
/// #[derive(Component, Clone)]
|
|
|
|
|
/// #[derive(Synced)]
|
2025-12-04 19:49:48 +00:00
|
|
|
/// #[sync(version = 1, strategy = "LastWriteWins")]
|
|
|
|
|
/// struct Health(f32);
|
|
|
|
|
///
|
|
|
|
|
/// // In a Bevy system:
|
|
|
|
|
/// fn spawn_health(mut commands: Commands) {
|
|
|
|
|
/// commands.spawn((Health(100.0), Synced));
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
|
|
|
|
#[proc_macro_derive(Synced, attributes(sync))]
|
|
|
|
|
pub fn derive_synced(input: TokenStream) -> TokenStream {
|
|
|
|
|
let input = parse_macro_input!(input as DeriveInput);
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
// Parse attributes
|
|
|
|
|
let attrs = match SyncAttributes::parse(&input) {
|
2025-12-11 18:39:57 +00:00
|
|
|
| Ok(attrs) => attrs,
|
|
|
|
|
| Err(e) => return TokenStream::from(e.to_compile_error()),
|
2025-12-04 19:49:48 +00:00
|
|
|
};
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
let name = &input.ident;
|
2025-12-17 19:20:34 +00:00
|
|
|
let name_str = name.to_string();
|
2025-12-04 19:49:48 +00:00
|
|
|
let version = attrs.version;
|
|
|
|
|
let strategy_tokens = attrs.strategy.to_tokens();
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
// Generate serialization method based on type
|
|
|
|
|
let serialize_impl = generate_serialize(&input);
|
|
|
|
|
let deserialize_impl = generate_deserialize(&input, name);
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
// Generate merge method based on strategy
|
|
|
|
|
let merge_impl = generate_merge(&input, &attrs.strategy);
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-17 19:20:34 +00:00
|
|
|
// Note: Users must add #[derive(rkyv::Archive, rkyv::Serialize,
|
|
|
|
|
// rkyv::Deserialize)] to their struct
|
2025-12-04 19:49:48 +00:00
|
|
|
let expanded = quote! {
|
2025-12-17 19:20:34 +00:00
|
|
|
// Register component with inventory for type registry
|
|
|
|
|
// Build type path at compile time using concat! and module_path!
|
|
|
|
|
// since std::any::type_name() is not yet const
|
|
|
|
|
const _: () = {
|
|
|
|
|
const TYPE_PATH: &str = concat!(module_path!(), "::", stringify!(#name));
|
|
|
|
|
|
|
|
|
|
inventory::submit! {
|
|
|
|
|
libmarathon::persistence::ComponentMeta {
|
|
|
|
|
type_name: #name_str,
|
|
|
|
|
type_path: TYPE_PATH,
|
|
|
|
|
type_id: std::any::TypeId::of::<#name>(),
|
|
|
|
|
deserialize_fn: |bytes: &[u8]| -> anyhow::Result<Box<dyn std::any::Any>> {
|
|
|
|
|
let component: #name = rkyv::from_bytes::<#name, rkyv::rancor::Failure>(bytes)?;
|
|
|
|
|
Ok(Box::new(component))
|
|
|
|
|
},
|
|
|
|
|
serialize_fn: |world: &bevy::ecs::world::World, entity: bevy::ecs::entity::Entity| -> Option<Vec<u8>> {
|
|
|
|
|
world.get::<#name>(entity).and_then(|component| {
|
|
|
|
|
rkyv::to_bytes::<rkyv::rancor::Failure>(component)
|
|
|
|
|
.map(|bytes| bytes.to_vec())
|
|
|
|
|
.ok()
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
insert_fn: |entity_mut: &mut bevy::ecs::world::EntityWorldMut, boxed: Box<dyn std::any::Any>| {
|
|
|
|
|
if let Ok(component) = boxed.downcast::<#name>() {
|
|
|
|
|
entity_mut.insert(*component);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-13 22:22:05 +00:00
|
|
|
impl libmarathon::networking::SyncComponent for #name {
|
2025-12-04 19:49:48 +00:00
|
|
|
const VERSION: u32 = #version;
|
2025-12-13 22:22:05 +00:00
|
|
|
const STRATEGY: libmarathon::networking::SyncStrategy = #strategy_tokens;
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
#[inline]
|
|
|
|
|
fn serialize_sync(&self) -> anyhow::Result<Vec<u8>> {
|
|
|
|
|
#serialize_impl
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
#[inline]
|
|
|
|
|
fn deserialize_sync(data: &[u8]) -> anyhow::Result<Self> {
|
|
|
|
|
#deserialize_impl
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
#[inline]
|
2025-12-13 22:22:05 +00:00
|
|
|
fn merge(&mut self, remote: Self, clock_cmp: libmarathon::networking::ClockComparison) -> libmarathon::networking::ComponentMergeDecision {
|
2025-12-04 19:49:48 +00:00
|
|
|
#merge_impl
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TokenStream::from(expanded)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Generate specialized serialization code
|
|
|
|
|
fn generate_serialize(_input: &DeriveInput) -> proc_macro2::TokenStream {
|
2025-12-17 19:20:34 +00:00
|
|
|
// Use rkyv for zero-copy serialization
|
2025-12-04 19:49:48 +00:00
|
|
|
// Later we can optimize for specific types (e.g., f32 -> to_le_bytes)
|
|
|
|
|
quote! {
|
2025-12-17 19:20:34 +00:00
|
|
|
rkyv::to_bytes::<rkyv::rancor::Failure>(self).map(|bytes| bytes.to_vec()).map_err(|e| anyhow::anyhow!("Serialization failed: {}", e))
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Generate specialized deserialization code
|
|
|
|
|
fn generate_deserialize(_input: &DeriveInput, _name: &syn::Ident) -> proc_macro2::TokenStream {
|
|
|
|
|
quote! {
|
2025-12-17 19:20:34 +00:00
|
|
|
rkyv::from_bytes::<Self, rkyv::rancor::Failure>(data).map_err(|e| anyhow::anyhow!("Deserialization failed: {}", e))
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Generate merge logic based on strategy
|
|
|
|
|
fn generate_merge(input: &DeriveInput, strategy: &SyncStrategy) -> proc_macro2::TokenStream {
|
|
|
|
|
match strategy {
|
2025-12-11 18:39:57 +00:00
|
|
|
| SyncStrategy::LastWriteWins => generate_lww_merge(input),
|
|
|
|
|
| SyncStrategy::Set => generate_set_merge(input),
|
|
|
|
|
| SyncStrategy::Sequence => generate_sequence_merge(input),
|
|
|
|
|
| SyncStrategy::Custom => generate_custom_merge(input),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generate hash calculation code for tiebreaking in concurrent merges
|
|
|
|
|
///
|
|
|
|
|
/// Returns a TokenStream that computes hashes for both local and remote values
|
|
|
|
|
/// and compares them for deterministic conflict resolution.
|
|
|
|
|
fn generate_hash_tiebreaker() -> proc_macro2::TokenStream {
|
|
|
|
|
quote! {
|
|
|
|
|
let local_hash = {
|
2025-12-17 19:20:34 +00:00
|
|
|
let bytes = rkyv::to_bytes::<rkyv::rancor::Failure>(self).map(|b| b.to_vec()).unwrap_or_default();
|
2025-12-11 18:39:57 +00:00
|
|
|
bytes.iter().fold(0u64, |acc, &b| acc.wrapping_mul(31).wrapping_add(b as u64))
|
|
|
|
|
};
|
|
|
|
|
let remote_hash = {
|
2025-12-17 19:20:34 +00:00
|
|
|
let bytes = rkyv::to_bytes::<rkyv::rancor::Failure>(&remote).map(|b| b.to_vec()).unwrap_or_default();
|
2025-12-11 18:39:57 +00:00
|
|
|
bytes.iter().fold(0u64, |acc, &b| acc.wrapping_mul(31).wrapping_add(b as u64))
|
|
|
|
|
};
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generate Last-Write-Wins merge logic
|
|
|
|
|
fn generate_lww_merge(_input: &DeriveInput) -> proc_macro2::TokenStream {
|
2025-12-11 18:39:57 +00:00
|
|
|
let hash_tiebreaker = generate_hash_tiebreaker();
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
quote! {
|
|
|
|
|
use tracing::info;
|
|
|
|
|
|
|
|
|
|
match clock_cmp {
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::RemoteNewer => {
|
2025-12-04 19:49:48 +00:00
|
|
|
info!(
|
|
|
|
|
component = std::any::type_name::<Self>(),
|
|
|
|
|
?clock_cmp,
|
|
|
|
|
"Taking remote (newer)"
|
|
|
|
|
);
|
|
|
|
|
*self = remote;
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::TookRemote
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::LocalNewer => {
|
|
|
|
|
libmarathon::networking::ComponentMergeDecision::KeptLocal
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::Concurrent => {
|
2025-12-04 19:49:48 +00:00
|
|
|
// Tiebreaker: Compare serialized representations for deterministic choice
|
|
|
|
|
// In a real implementation, we'd use node_id, but for now use a simple hash
|
2025-12-11 18:39:57 +00:00
|
|
|
#hash_tiebreaker
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
if remote_hash > local_hash {
|
|
|
|
|
info!(
|
|
|
|
|
component = std::any::type_name::<Self>(),
|
|
|
|
|
?clock_cmp,
|
|
|
|
|
"Taking remote (concurrent, tiebreaker)"
|
|
|
|
|
);
|
|
|
|
|
*self = remote;
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::TookRemote
|
2025-12-04 19:49:48 +00:00
|
|
|
} else {
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::KeptLocal
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Generate OR-Set merge logic
|
|
|
|
|
///
|
|
|
|
|
/// For OR-Set strategy, the component must contain an OrSet<T> field.
|
2025-12-11 18:39:57 +00:00
|
|
|
/// We merge by calling the OrSet's merge method which implements add-wins
|
|
|
|
|
/// semantics.
|
2025-12-04 19:49:48 +00:00
|
|
|
fn generate_set_merge(_input: &DeriveInput) -> proc_macro2::TokenStream {
|
2025-12-11 18:39:57 +00:00
|
|
|
let hash_tiebreaker = generate_hash_tiebreaker();
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
quote! {
|
|
|
|
|
use tracing::info;
|
|
|
|
|
|
|
|
|
|
// For Set strategy, we always merge the sets
|
|
|
|
|
// The OrSet CRDT handles the conflict resolution with add-wins semantics
|
|
|
|
|
info!(
|
|
|
|
|
component = std::any::type_name::<Self>(),
|
|
|
|
|
"Merging OR-Set (add-wins semantics)"
|
|
|
|
|
);
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
// Assuming the component wraps an OrSet or has a field with merge()
|
|
|
|
|
// For now, we'll do a structural merge by replacing the whole value
|
|
|
|
|
// This is a simplified implementation - full implementation would require
|
|
|
|
|
// the component to expose merge() method or implement it directly
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
match clock_cmp {
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::RemoteNewer => {
|
2025-12-04 19:49:48 +00:00
|
|
|
*self = remote;
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::TookRemote
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::LocalNewer => {
|
|
|
|
|
libmarathon::networking::ComponentMergeDecision::KeptLocal
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::Concurrent => {
|
2025-12-04 19:49:48 +00:00
|
|
|
// In a full implementation, we would merge the OrSet here
|
|
|
|
|
// For now, use LWW with tiebreaker as fallback
|
2025-12-11 18:39:57 +00:00
|
|
|
#hash_tiebreaker
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
if remote_hash > local_hash {
|
|
|
|
|
*self = remote;
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::TookRemote
|
2025-12-04 19:49:48 +00:00
|
|
|
} else {
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::KeptLocal
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Generate RGA/Sequence merge logic
|
|
|
|
|
///
|
|
|
|
|
/// For Sequence strategy, the component must contain an Rga<T> field.
|
|
|
|
|
/// We merge by calling the Rga's merge method which maintains causal ordering.
|
|
|
|
|
fn generate_sequence_merge(_input: &DeriveInput) -> proc_macro2::TokenStream {
|
2025-12-11 18:39:57 +00:00
|
|
|
let hash_tiebreaker = generate_hash_tiebreaker();
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
quote! {
|
|
|
|
|
use tracing::info;
|
|
|
|
|
|
|
|
|
|
// For Sequence strategy, we always merge the sequences
|
|
|
|
|
// The RGA CRDT handles the conflict resolution with causal ordering
|
|
|
|
|
info!(
|
|
|
|
|
component = std::any::type_name::<Self>(),
|
|
|
|
|
"Merging RGA sequence (causal ordering)"
|
|
|
|
|
);
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
// Assuming the component wraps an Rga or has a field with merge()
|
|
|
|
|
// For now, we'll do a structural merge by replacing the whole value
|
|
|
|
|
// This is a simplified implementation - full implementation would require
|
|
|
|
|
// the component to expose merge() method or implement it directly
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
match clock_cmp {
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::RemoteNewer => {
|
2025-12-04 19:49:48 +00:00
|
|
|
*self = remote;
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::TookRemote
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::LocalNewer => {
|
|
|
|
|
libmarathon::networking::ComponentMergeDecision::KeptLocal
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ClockComparison::Concurrent => {
|
2025-12-04 19:49:48 +00:00
|
|
|
// In a full implementation, we would merge the Rga here
|
|
|
|
|
// For now, use LWW with tiebreaker as fallback
|
2025-12-11 18:39:57 +00:00
|
|
|
#hash_tiebreaker
|
2025-11-15 23:42:12 +00:00
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
if remote_hash > local_hash {
|
|
|
|
|
*self = remote;
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::TookRemote
|
2025-12-04 19:49:48 +00:00
|
|
|
} else {
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::KeptLocal
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-04 19:49:48 +00:00
|
|
|
/// Generate custom merge logic placeholder
|
|
|
|
|
fn generate_custom_merge(input: &DeriveInput) -> proc_macro2::TokenStream {
|
|
|
|
|
let name = &input.ident;
|
|
|
|
|
quote! {
|
|
|
|
|
compile_error!(
|
|
|
|
|
concat!(
|
|
|
|
|
"Custom strategy requires implementing ConflictResolver trait for ",
|
|
|
|
|
stringify!(#name)
|
|
|
|
|
)
|
|
|
|
|
);
|
2025-12-13 22:22:05 +00:00
|
|
|
libmarathon::networking::ComponentMergeDecision::KeptLocal
|
2025-12-04 19:49:48 +00:00
|
|
|
}
|
2025-11-15 23:42:12 +00:00
|
|
|
}
|