Skip to main content

hydr_structures/structures/controlled_gate/snapshot/
restore.rs

1//! Atomic restoration preserves both destination child contexts.
2
3use super::super::ControlledGateBatch;
4use super::{
5    CONTROLLED_GATE_CONTRACT_VERSION, CONTROLLED_GATE_SNAPSHOT_SCHEMA, ControlledGateSnapshot,
6    ControlledGateSnapshotData,
7};
8use crate::structures::StructureError;
9use hydr_core::contract::package_identity;
10use numr::dtype::DType;
11use numr::runtime::Runtime;
12
13impl<R: Runtime<DType = DType>> ControlledGateBatch<R> {
14    /// Restore trusted child tensors atomically while retaining this composite's immutable contexts.
15    pub fn restore(&mut self, snapshot: &ControlledGateSnapshot<R>) -> Result<(), StructureError> {
16        let mut controls = self.controls.clone();
17        let mut actuators = self.actuators.clone();
18        controls.restore(&snapshot.batch.controls.snapshot())?;
19        actuators.restore(&snapshot.batch.actuators.snapshot())?;
20        super::check::metadata(&controls, &actuators)?;
21        self.controls = controls;
22        self.actuators = actuators;
23        Ok(())
24    }
25    /// Check both complete child packages and their correspondence before atomically replacing either child.
26    ///
27    /// Bounded checks establish continuation consistency. They establish no full historical authenticity.
28    pub fn restore_data(&mut self, data: &ControlledGateSnapshotData) -> Result<(), StructureError>
29    where
30        R::Client: numr::ops::BinaryOps<R>
31            + numr::ops::CompareOps<R>
32            + numr::ops::ConditionalOps<R>
33            + numr::ops::ReduceOps<R>
34            + numr::ops::ScalarOps<R>
35            + numr::ops::TwoSumOps<R>
36            + numr::ops::TypeConversionOps<R>
37            + numr::ops::UnaryOps<R>,
38    {
39        package_identity(
40            "controlled gate",
41            &data.schema,
42            CONTROLLED_GATE_SNAPSHOT_SCHEMA,
43            &data.contract,
44            CONTROLLED_GATE_CONTRACT_VERSION,
45        )
46        .map_err(StructureError::definition)?;
47        let mut controls = self.controls.clone();
48        let mut actuators = self.actuators.clone();
49        controls.restore_data(&data.controls)?;
50        actuators.restore_data(&data.actuators)?;
51        super::check::data(self, data)?;
52        self.controls = controls;
53        self.actuators = actuators;
54        Ok(())
55    }
56}