vihaco_runtime/execute.rs
1// SPDX-FileCopyrightText: 2026 The vihaco Authors
2// SPDX-License-Identifier: MIT
3
4use crate::Component;
5
6/// Execute one instruction type with runtime-supplied input.
7///
8/// Available with the experimental `typed-declarations` feature. Each impl
9/// chooses its message, result, and error types independently.
10/// Implementing this trait alone does not declare dialect availability.
11///
12/// # Examples
13///
14/// ```
15/// use vihaco_runtime::{Component, Effects, Execute, NoMessage};
16///
17/// struct Add(i64);
18/// struct Counter(i64);
19/// impl Component for Counter {}
20/// impl Execute<Add> for Counter {
21/// type Message = NoMessage;
22/// type Effect = Effects<i64>;
23/// type Error = eyre::Report;
24///
25/// fn execute(&mut self, instruction: &Add, _: NoMessage) -> eyre::Result<Self::Effect> {
26/// self.0 += instruction.0;
27/// Ok(Effects::one(self.0))
28/// }
29/// }
30///
31/// let mut counter = Counter(1);
32/// assert_eq!(counter.execute(&Add(2), NoMessage::new()).unwrap(), Effects::one(3));
33/// ```
34pub trait Execute<I>: Component {
35 /// Input supplied by the runtime, rather than encoded in the instruction.
36 type Message;
37 /// The successful result.
38 type Effect;
39 /// The component's execution error, usually `eyre::Report`.
40 type Error;
41
42 /// Execute a borrowed instruction without consuming its payload.
43 ///
44 /// # Errors
45 ///
46 /// Returns the error chosen by this implementation if execution fails.
47 fn execute(
48 &mut self,
49 instruction: &I,
50 message: Self::Message,
51 ) -> Result<Self::Effect, Self::Error>;
52}