Skip to main content

vihaco_runtime/
inspection.rs

1// SPDX-FileCopyrightText: 2026 The vihaco Authors
2// SPDX-License-Identifier: MIT
3
4use std::{any::Any, fmt::Debug};
5
6/// Inspect a resolved instruction without depending on its generated enum variant.
7pub trait InspectInstruction {
8    fn inspect<R>(&self, inspect: impl FnOnce(InstructionView<'_>) -> R) -> R;
9}
10
11/// A resolved instruction's canonical route and operands in declaration order.
12pub struct InstructionView<'a> {
13    pub device_path: &'static [&'static str],
14    pub dialect: &'static str,
15    pub mnemonic: &'static str,
16    /// Operand borrows are unavailable when the instruction is packed.
17    pub operands: Option<&'a [InstructionOperand<'a>]>,
18    pub leaf: &'a dyn Debug,
19}
20
21/// A borrow of a resolved operand with its original Rust type.
22pub struct InstructionOperand<'a> {
23    pub debug: &'a dyn Debug,
24    pub value: &'a dyn Any,
25}
26
27/// Generated by the dialect macro for use by composite instruction inspection.
28#[doc(hidden)]
29pub trait InspectOperands {
30    fn inspect_operands<R>(
31        &self,
32        inspect: impl FnOnce(Option<&[InstructionOperand<'_>]>) -> R,
33    ) -> R;
34}