Download rust/crates/hyperkitty-sla/src/compose.rs from Snapkitty/hyperkitty-constraint-dsl: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/Snapkitty/hyperkitty-constraint-dsl/resolve/main/rust/crates/hyperkitty-sla/src/compose.rs
- Command line
-
hf download hf://Snapkitty/hyperkitty-constraint-dsl/rust/crates/hyperkitty-sla/src/compose.rs
-
curl -L -o compose.rs https://huggingface.co/Snapkitty/hyperkitty-constraint-dsl/resolve/main/rust/crates/hyperkitty-sla/src/compose.rs
1.45 kB
| //! Partial composition for SLA ledgers | |
| use super::ledger::Ledger; | |
| use crate::Result; | |
| use hyperkitty_core::Error; | |
| /// Partial composition: λ_A ⊕ λ_B defined iff ω_A = ω_B | |
| pub fn partial_compose(a: &Ledger, b: &Ledger) -> Result<Ledger> { | |
| // Check conserved values match | |
| if a.omega != b.omega { | |
| return Err(Error::SLABalance(format!( | |
| "Cannot compose: omega mismatch {} != {}", | |
| a.omega, b.omega | |
| ))); | |
| } | |
| // Compose by adding deltas (which maintains invariant) | |
| Ledger::new(a.s + b.s, a.delta + b.delta, a.omega) | |
| } | |
| mod tests { | |
| use super::*; | |
| fn valid_composition() { | |
| let a = Ledger::new(100, 50, 42).unwrap(); | |
| let b = Ledger::new(50, 25, 42).unwrap(); | |
| let result = partial_compose(&a, &b).unwrap(); | |
| assert_eq!(result.s, 150); | |
| assert_eq!(result.delta, 75); | |
| assert_eq!(result.omega, 42); | |
| } | |
| fn omega_mismatch() { | |
| let a = Ledger::new(100, 50, 42).unwrap(); | |
| let b = Ledger::new(50, 25, 99).unwrap(); | |
| assert!(partial_compose(&a, &b).is_err()); | |
| } | |
| fn composition_preserves_invariant() { | |
| let a = Ledger::new(100, 50, 42).unwrap(); | |
| let b = Ledger::new(50, -30, 42).unwrap(); | |
| let result = partial_compose(&a, &b).unwrap(); | |
| assert!(result.verify_invariant()); | |
| } | |
| } | |