Skip to main content

tenferro_runtime/program/
import.rs

1use std::fmt;
2
3use super::{ProgramBindings, ProgramValue, SemanticProgram};
4
5/// Read-only request to import the dependency closure of ordered roots.
6pub struct ProgramImport<'a> {
7    /// Immutable source semantic program.
8    pub program: &'a SemanticProgram,
9    /// Tensor bindings frozen with the source program, when structure and
10    /// bindings should travel together.
11    ///
12    /// A structural import passes `None` so symbolic extents stay symbolic; a
13    /// caller that wants the frozen defaults re-attaches them afterwards.
14    pub bindings: Option<&'a ProgramBindings>,
15    /// Ordered source roots to remap; empty and duplicate roots are valid.
16    pub roots: &'a [ProgramValue],
17    /// Source inputs that already exist in the destination.
18    ///
19    /// A mapped source input reuses the destination value (and its metadata)
20    /// instead of creating a fresh input, which is how a derivative program
21    /// lands back on the leaves it was differentiated from.
22    pub input_map: &'a [(ProgramValue, ProgramValue)],
23}
24
25impl fmt::Debug for ProgramImport<'_> {
26    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
27        formatter
28            .debug_struct("ProgramImport")
29            .field("program_inputs", &self.program.inputs().len())
30            .field("program_outputs", &self.program.outputs().len())
31            .field("bindings", &self.bindings.map(ProgramBindings::len))
32            .field("roots", &self.roots.len())
33            .field("input_map", &self.input_map.len())
34            .finish()
35    }
36}
37
38/// Destination-builder values corresponding to requested import roots.
39pub struct ImportedProgramValues {
40    roots: Box<[ProgramValue]>,
41}
42
43impl ImportedProgramValues {
44    pub(crate) fn new(roots: Box<[ProgramValue]>) -> Self {
45        Self { roots }
46    }
47
48    /// Borrow imported roots in request order, including duplicates.
49    pub fn roots(&self) -> &[ProgramValue] {
50        &self.roots
51    }
52}
53
54impl fmt::Debug for ImportedProgramValues {
55    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
56        formatter
57            .debug_struct("ImportedProgramValues")
58            .field("roots", &self.roots.len())
59            .finish()
60    }
61}