use smol_bytes::Utf8Bytes;
use crate::{
Async, AsyncAutonomousOperator, AsyncOperator, AsyncStreamOperator, AutonomousOperator, Block,
Driven, Drives, Operator, StreamOperator,
arrange::NodeId,
drive::{
Attaches, Attributes, Inbox, ItemId, Outbox, Resident, Runs, Sink, StreamOptions, Streams,
Thread, Upstreams,
},
};
use super::{
Blueprint, Declared, Ignition, LaunchError, Staging, Wiring,
wiring::{AutonomousBody, DeclaredResident, MountBody, NodeAttribution, NodeBody, StreamWires},
};
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Buffered;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Streaming;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Autonomous;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Worn<P, M>(core::marker::PhantomData<fn() -> (P, M)>);
pub type Cell<N> = Worn<<N as Declared>::Pair, <N as Declared>::Mode>;
#[diagnostic::on_unimplemented(
message = "`{N}` cannot be mounted at this cell of the pair axis",
label = "no mount for this declaration",
note = "a node in a `#[ingraph::graph]` root is mounted through the cell its OWN declaration \
selected: `Declared::Pair` picks the pair (`stream` seats `Streaming`) and \
`Declared::Mode` picks the half, and this bound is the two of them cashed",
note = "a BUFFERED cell asks for `Operator` (mode `Block`) or `AsyncOperator` (mode `Async`); a \
STREAMING cell asks for `StreamOperator` or `AsyncStreamOperator`; an AUTONOMOUS cell \
asks for `AutonomousOperator` or `AsyncAutonomousOperator` — so a body whose face differs \
from the words its declaration wrote is refused here, naming the trait it does not \
implement",
note = "a message a graph root carries is `Send + 'static`, and a per-item error is an `Error + \
Send + Sync + 'static`",
note = "at the BUFFERED and AUTONOMOUS cells an output is `Clone` besides — both publish through \
`Downstream`, whose N edges of one product are the fan-out itself and whose `Sink` impl \
takes the copy. The STREAMING cell asks for none: it publishes through `<N as \
Declared>::Wires<Out>`, which is that same set for a node that emits one thing and the \
node's OWN set, which asks for a copy only where a product really has more than one \
subscriber — for a node that emits several",
note = "a streaming cell also asks that the node's `Wires` seat really be a downstream set \
carrying its output: `Sink<Out>` on the projection, so a hand-written seat naming \
somebody else's set is refused here",
note = "a BUFFERED cell asks that the node's `Attribution` seat really project a root out of its \
output: `Attributes<Out>` on the projection, so a declaration that wrote `attributed` \
over a payload no record declared — a `String`, a `u64` — is refused here. A node that \
attributes nothing seats `()`, which satisfies this for every payload there is"
)]
pub trait Mount<N>
where
N: Declared,
{
const SYNC: bool;
type In: Send + 'static;
type Out: Send + 'static;
type Error: core::error::Error + Send + Sync + 'static;
type Body;
type Intake;
fn declare(blueprint: &mut Blueprint, mount: impl Into<Utf8Bytes>) -> NodeId;
fn intake(wiring: &mut Wiring<'_>, node: NodeId) -> Result<Option<Self::Intake>, LaunchError>;
fn wire(
wiring: &mut Wiring<'_>,
node: NodeId,
operator: N,
intake: Self::Intake,
seats: Option<StreamOptions>,
) -> Result<Option<Self::Body>, LaunchError>;
fn feeder<R>(ignition: &mut Ignition<R>, feeder: Outbox<ItemId, Self::In>);
fn stage(staged: &mut Staging, mount: &str, body: Self::Body);
fn ignite<R>(ignition: &mut Ignition<R>, body: Self::Body) -> Result<(), LaunchError>;
}
macro_rules! cells {
($(($mode:ident, $face:ident, $streaming:ident, $autonomous:ident)),* $(,)?) => {
$(
impl<N> Mount<N> for Worn<Buffered, $mode>
where
// THE FACE ITSELF is the bound, and `Driven<M>` is not written beside it: implementing the
// face IS implementing the row, by the blanket, so one is the other said twice — and it is
// the face that has to be named, because `Drives` (the ignition's own bound) is written over
N: Declared<Mode = $mode> + $face + Send + 'static,
<N as $face>::In: Send + 'static,
<N as $face>::Out: Clone + Send + 'static,
<N as $face>::Error: core::error::Error + Send + Sync + 'static,
NodeAttribution<N, $mode>: Attributes<<N as $face>::Out>,
DeclaredResident<N, Inbox<ItemId, <N as $face>::In>, $mode>: Drives + Send + 'static,
{
const SYNC: bool = <N as Driven<$mode>>::SYNC;
type In = <N as $face>::In;
type Out = <N as $face>::Out;
type Error = <N as $face>::Error;
type Body = NodeBody<N, Inbox<ItemId, <N as $face>::In>, $mode>;
type Intake = Inbox<ItemId, <N as $face>::In>;
#[inline]
fn declare(blueprint: &mut Blueprint, mount: impl Into<Utf8Bytes>) -> NodeId {
blueprint.node::<N>(mount)
}
#[inline]
fn intake(
wiring: &mut Wiring<'_>,
node: NodeId,
) -> Result<Option<Self::Intake>, LaunchError> {
wiring.inbox::<<N as $face>::In>(node)
}
#[inline]
fn wire(
wiring: &mut Wiring<'_>,
node: NodeId,
operator: N,
intake: Self::Intake,
_seats: Option<StreamOptions>,
) -> Result<Option<Self::Body>, LaunchError> {
wiring.resident::<N, _>(node, operator, intake)
}
#[inline]
fn feeder<R>(ignition: &mut Ignition<R>, feeder: Outbox<ItemId, Self::In>) {
ignition.intake::<N, _>(feeder);
}
#[inline]
fn stage(staged: &mut Staging, mount: &str, body: Self::Body) {
let (resident, _party) = body;
staged.declared::<N>(mount, Resident::into_operator(resident));
}
#[inline]
fn ignite<R>(ignition: &mut Ignition<R>, body: Self::Body) -> Result<(), LaunchError> {
let (resident, party) = body;
ignition.resident_declared(resident, party)
}
}
impl<N> Mount<N> for Worn<Streaming, $mode>
where
N: Declared<Mode = $mode> + $streaming + Send + 'static,
<N as $streaming>::In: Send + 'static,
<N as $streaming>::Out: Send + 'static,
<N as $streaming>::Error: core::error::Error + Send + Sync + 'static,
StreamWires<N, $mode>: Attaches + Sink<<N as $streaming>::Out> + Send + 'static,
{
const SYNC: bool = <N as Streams<$mode>>::SYNC;
type In = <N as $streaming>::In;
type Out = <N as $streaming>::Out;
type Error = <N as $streaming>::Error;
type Body = MountBody<N, Thread, $mode>;
type Intake = Inbox<ItemId, <N as $streaming>::In>;
#[inline]
fn declare(blueprint: &mut Blueprint, mount: impl Into<Utf8Bytes>) -> NodeId {
blueprint.stream::<N>(mount)
}
#[inline]
fn intake(
wiring: &mut Wiring<'_>,
node: NodeId,
) -> Result<Option<Self::Intake>, LaunchError> {
wiring.inbox::<<N as $streaming>::In>(node)
}
#[inline]
fn wire(
wiring: &mut Wiring<'_>,
node: NodeId,
operator: N,
intake: Self::Intake,
seats: Option<StreamOptions>,
) -> Result<Option<Self::Body>, LaunchError> {
let seats = seats.unwrap_or_default();
wiring.lane_mount::<N, _, Thread>(node, [operator], seats, Thread::new(), intake)
}
#[inline]
fn feeder<R>(ignition: &mut Ignition<R>, feeder: Outbox<ItemId, Self::In>) {
ignition.intake::<N, _>(feeder);
}
#[inline]
fn stage(staged: &mut Staging, mount: &str, body: Self::Body) {
let (held, _party) = body;
for instance in held.into_instances() {
staged.declared::<N>(mount, instance);
}
}
#[inline]
fn ignite<R>(ignition: &mut Ignition<R>, body: Self::Body) -> Result<(), LaunchError> {
let (held, party) = body;
ignition.lane_declared::<N, Thread>(held, party);
Ok(())
}
}
impl<N> Mount<N> for Worn<Autonomous, $mode>
where
N: Declared<Mode = $mode> + $autonomous + Send + 'static,
<N as $autonomous>::In: Send + 'static,
<N as $autonomous>::Out: Clone + Send + 'static,
<N as $autonomous>::Error: core::error::Error + Send + Sync + 'static,
{
const SYNC: bool = <N as Runs<$mode>>::SYNC;
type In = <N as $autonomous>::In;
type Out = <N as $autonomous>::Out;
type Error = <N as $autonomous>::Error;
type Body = AutonomousBody<N, $mode>;
type Intake = Upstreams<<N as $autonomous>::In>;
#[inline]
fn declare(blueprint: &mut Blueprint, mount: impl Into<Utf8Bytes>) -> NodeId {
blueprint.autonomous::<N>(mount)
}
#[inline]
fn intake(
wiring: &mut Wiring<'_>,
node: NodeId,
) -> Result<Option<Self::Intake>, LaunchError> {
wiring.upstreams::<<N as $autonomous>::In>(node)
}
#[inline]
fn wire(
wiring: &mut Wiring<'_>,
node: NodeId,
operator: N,
intake: Self::Intake,
_seats: Option<StreamOptions>,
) -> Result<Option<Self::Body>, LaunchError> {
wiring.autonomous::<N>(node, operator, intake)
}
#[inline]
fn feeder<R>(ignition: &mut Ignition<R>, feeder: Outbox<ItemId, Self::In>) {
ignition.intake::<N, _>(feeder);
}
#[inline]
fn stage(staged: &mut Staging, mount: &str, body: Self::Body) {
let (helm, _stopper, _party) = body;
staged.declared::<N>(mount, helm.into_operator());
}
#[inline]
fn ignite<R>(ignition: &mut Ignition<R>, body: Self::Body) -> Result<(), LaunchError> {
let (helm, stopper, party) = body;
ignition.autonomous_declared::<N>(helm, stopper, party)
}
}
)*
};
}
cells!(
(Block, Operator, StreamOperator, AutonomousOperator),
(
Async,
AsyncOperator,
AsyncStreamOperator,
AsyncAutonomousOperator
),
);