Skip to main content

sectorsync_core/
policy.rs

1//! Runtime-configurable, hot-path-compiled sync policies.
2
3use crate::ids::PolicyId;
4
5/// Compiled sync policy used by hot-path replication planning.
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub struct CompiledSyncPolicy {
8    /// Small policy id used by entities.
9    pub id: PolicyId,
10    /// Minimum update rate in hertz.
11    pub min_hz: u16,
12    /// Maximum update rate in hertz.
13    pub max_hz: u16,
14    /// Primary interest radius in world units.
15    pub interest_radius: f32,
16    /// Weight used when client budget is tight.
17    pub priority_weight: u16,
18    /// Whether this entity can be represented by ghosts.
19    pub allow_ghost: bool,
20    /// Whether this entity can be aggregated at low detail.
21    pub allow_aggregate: bool,
22}
23
24impl CompiledSyncPolicy {
25    /// Creates a compiled policy.
26    pub const fn new(id: PolicyId, min_hz: u16, max_hz: u16, interest_radius: f32) -> Self {
27        Self {
28            id,
29            min_hz,
30            max_hz,
31            interest_radius,
32            priority_weight: 1,
33            allow_ghost: true,
34            allow_aggregate: false,
35        }
36    }
37}
38
39/// Dense policy table indexed by `PolicyId`.
40#[derive(Clone, Debug, Default)]
41pub struct PolicyTable {
42    policies: Vec<Option<CompiledSyncPolicy>>,
43}
44
45impl PolicyTable {
46    /// Inserts or replaces a compiled policy.
47    pub fn set(&mut self, policy: CompiledSyncPolicy) {
48        let index = usize::from(policy.id.get());
49        if self.policies.len() <= index {
50            self.policies.resize(index + 1, None);
51        }
52        self.policies[index] = Some(policy);
53    }
54
55    /// Gets a policy by id.
56    pub fn get(&self, id: PolicyId) -> Option<&CompiledSyncPolicy> {
57        self.policies
58            .get(usize::from(id.get()))
59            .and_then(Option::as_ref)
60    }
61
62    /// Number of slots in the dense policy table.
63    pub fn slot_count(&self) -> usize {
64        self.policies.len()
65    }
66}