pub use crate::{TransportFrom, TransportTo};
use crate::{CircuitDraft, Participant, StateMachine, StepInputOf};
mod private {
use super::{Participant, StateMachine, StepInputOf};
pub(super) trait StepLaw<'message> {}
impl<'message, Machine> StepLaw<'message> for Machine
where
Machine: StateMachine + Participant,
Machine::In<'message>: StepInputOf<Machine, Outcome = Machine::Out<'message>>,
{
}
}
#[doc(hidden)]
#[allow(private_bounds)]
pub trait StepsParticipantInputToOutput<'message>:
StateMachine + Participant + private::StepLaw<'message>
{
#[must_use = "participant step outcomes must be routed or settled"]
#[doc(hidden)]
fn step_boundary(&mut self, input: Self::In<'message>) -> Self::Out<'message>;
}
impl<'message, Machine> StepsParticipantInputToOutput<'message> for Machine
where
Machine: StateMachine + Participant,
Machine::In<'message>: StepInputOf<Machine, Outcome = Machine::Out<'message>>,
{
#[inline]
fn step_boundary(&mut self, input: Self::In<'message>) -> Self::Out<'message> {
input.step(self)
}
}
pub struct Inline<Machine> {
destination: Machine,
}
impl<Machine> Inline<Machine> {
#[inline]
pub const fn new(destination: Machine) -> Self {
Self { destination }
}
#[inline]
pub const fn destination(&self) -> &Machine {
&self.destination
}
#[inline]
pub fn into_destination(self) -> Machine {
self.destination
}
}
impl<Machine> TransportTo<Machine> for Inline<Machine>
where
Machine: for<'message> StepsParticipantInputToOutput<'message>,
{
type Outcome<'transport, 'message>
= Machine::Out<'message>
where
Self: 'transport;
#[inline]
fn send<'transport, 'message>(
&'transport mut self,
input: Machine::In<'message>,
) -> Self::Outcome<'transport, 'message> {
self.destination.step_boundary(input)
}
}
pub struct InlineCircuitDraft<InstalledCircuit> {
destination: InstalledCircuit,
}
impl<InstalledCircuit> InlineCircuitDraft<InstalledCircuit> {
#[inline(always)]
pub const fn new(destination: InstalledCircuit) -> Self {
Self { destination }
}
#[inline(always)]
pub const fn destination(&self) -> &InstalledCircuit {
&self.destination
}
#[inline(always)]
pub fn into_destination(self) -> InstalledCircuit {
self.destination
}
}
impl<InstalledCircuit: CircuitDraft> TransportTo<InstalledCircuit>
for InlineCircuitDraft<InstalledCircuit>
{
type Outcome<'transport, 'message>
= InstalledCircuit::Out<'message>
where
Self: 'transport;
#[inline(always)]
fn send<'transport, 'message>(
&'transport mut self,
input: InstalledCircuit::In<'message>,
) -> Self::Outcome<'transport, 'message> {
self.destination.turn(input)
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Counter(u8);
impl StateMachine for Counter {}
struct Add(u8);
impl Participant for Counter {
type In<'message> = Add;
type Out<'message> = u8;
}
impl StepInputOf<Counter> for Add {
type Outcome = u8;
fn step(self, counter: &mut Counter) -> Self::Outcome {
counter.0 = counter.0.saturating_add(self.0);
counter.0
}
}
#[test]
fn inline_transport_completes_the_precise_step() {
let mut transport = Inline::new(Counter(3));
assert_eq!(TransportTo::<Counter>::send(&mut transport, Add(4)), 7);
assert_eq!(transport.into_destination().0, 7);
}
}