Skip to main content

propaga_core/
id.rs

1use slotmap::new_key_type;
2use std::fmt;
3
4new_key_type! {
5    /// Opaque key for a decision variable stored in the engine arena.
6    pub struct VariableKey;
7    /// Opaque key for a propagator stored in the engine arena.
8    pub struct PropagatorKey;
9}
10
11/// Stable handle to a decision variable.
12#[derive(Clone, Copy, PartialEq, Eq, Hash)]
13pub struct VariableId(pub(crate) VariableKey);
14
15impl VariableId {
16    /// Returns the underlying arena key.
17    #[must_use]
18    pub const fn key(self) -> VariableKey {
19        self.0
20    }
21
22    /// Creates an id from an arena key.
23    #[must_use]
24    pub const fn from_key(key: VariableKey) -> Self {
25        Self(key)
26    }
27}
28
29impl fmt::Debug for VariableId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        write!(f, "VariableId({:?})", self.0)
32    }
33}
34
35/// Stable handle to a propagator.
36#[derive(Clone, Copy, PartialEq, Eq, Hash)]
37pub struct PropagatorId(pub(crate) PropagatorKey);
38
39impl PropagatorId {
40    /// Returns the underlying arena key.
41    #[must_use]
42    pub const fn key(self) -> PropagatorKey {
43        self.0
44    }
45
46    /// Creates an id from an arena key.
47    #[must_use]
48    pub const fn from_key(key: PropagatorKey) -> Self {
49        Self(key)
50    }
51}
52
53impl fmt::Debug for PropagatorId {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        write!(f, "PropagatorId({:?})", self.0)
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62    use slotmap::SlotMap;
63
64    #[test]
65    fn variable_id_roundtrips_key() {
66        let mut sm: SlotMap<VariableKey, ()> = SlotMap::with_key();
67        let key = sm.insert(());
68        let id = VariableId::from_key(key);
69        assert_eq!(id.key(), key);
70    }
71
72    #[test]
73    fn propagator_id_roundtrips_key() {
74        let mut sm: SlotMap<PropagatorKey, ()> = SlotMap::with_key();
75        let key = sm.insert(());
76        let id = PropagatorId::from_key(key);
77        assert_eq!(id.key(), key);
78    }
79}