Skip to main content

composite

Attribute Macro composite 

#[composite]
Expand description

Declare device fields and complete child capabilities.

With typed-declarations, named device declarations contribute every leaf from their declared dialects to a hidden flat runtime enum exposed through HasInstructionSet. Its syntax parser uses field::nested_field::dialect::instruction prefixes and accepts device aliases. Implement Step for every destination/leaf pair. With program-image-execution, a #[program] device selects program access and operand-storage wiring; Steps and effect handling remain authored. The generated Dispatch implementation requires complete Step coverage for the selected context when a Dispatch<Context> bound is required or dispatch is called. Declaring a composite alone does not check Step coverage. Generic declarations retain their original bounds. Every device needs an explicit u8 code, unique within its parent. Ordinary state is excluded from parent instruction sets. Without program-image-execution, #[loadable] devices form section boundaries and are excluded from parent instruction sets. The macro generates legacy section-loading implementations in that configuration. With the feature enabled, construct images explicitly and pair them with stdlib Loadable; #[loadable] helpers and generated section loading are unavailable. Field::get and Field::get_mut borrow eligible devices through direct or nested field routes, including devices that contain borrowed data. Named generic children are supported; bare type parameters and associated child types cannot supply an inferred inventory companion. Renamed child imports/type aliases require matching __vihaco_inventory_Name and field companion reexports. Inventory slots and variant names are not bytecode IDs.

use vihaco_runtime::{Execute, NoMessage, Step, field, component, composite, dialect};
#[dialect]
mod control { #[instruction] pub struct Halt; }
#[component]
#[instructions { control }]
struct Cpu;
impl Execute<control::Halt> for Cpu {
    type Message = NoMessage;
    type Effect = ();
    type Error = std::convert::Infallible;
    fn execute(&mut self, _: &control::Halt, _: NoMessage) -> Result<(), Self::Error> {
        Ok(())
    }
}
#[composite]
struct Machine { #[device(7, alias = "host")] cpu: Cpu }
impl Step<field!(Machine::cpu), control::Halt> for Machine {
    fn step(&mut self, instruction: &control::Halt, _: &()) -> vihaco_runtime::eyre::Result<()> {
        self.cpu.execute(instruction, NoMessage::new())?;
        Ok(())
    }
}

Without typed-declarations, this retains the legacy composite expansion. Declare typed device fields and complete capabilities. Legacy section loading is generated without program-image-execution.