use super::{
ItemId, Rings,
channel::Outbox,
message::{AckSender, Delivery, Item, Payload},
};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Wanted(u64);
impl Wanted {
pub const CEILING: usize = u64::BITS as usize;
#[inline]
#[must_use]
pub const fn none() -> Self {
Self(0)
}
#[inline]
#[must_use]
pub const fn sole(wanted: bool) -> Self {
Self(wanted as u64)
}
#[inline]
#[must_use]
pub const fn with(self, index: usize) -> Self {
if index >= Self::CEILING {
return self;
}
Self(self.0 | (1u64 << index))
}
#[inline]
#[must_use]
pub const fn at(self, index: usize) -> bool {
if index >= Self::CEILING {
return false;
}
self.0 & (1u64 << index) != 0
}
#[inline]
#[must_use]
pub const fn any(self) -> bool {
self.0 != 0
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a downstream set carrying `{O}`",
label = "no `Sink<{O}>`",
note = "a node's out-edges are held by ONE type implementing this trait once per product it \
carries: `Downstream<{O}>` where the node has a single product, the node's own emitted \
`<Node>Downstream` where it declared several, or a set written by hand",
note = "if the type IS a downstream set, the PRODUCT is the mismatch — an edge is a subscription \
to one product, so a set carrying another one implements this trait at that one and not \
at `{O}`"
)]
pub trait Sink<O> {
fn arity(&self) -> usize;
#[inline]
fn wanted(&self) -> bool {
self.arity() > 0
}
#[inline]
fn connected(&self) -> Wanted {
Wanted::sole(self.wanted())
}
#[inline]
fn carries(&self, edge: usize, output: &O) -> bool {
let _ = output;
edge < self.arity()
}
#[inline]
fn duplicate(&self, output: &O) -> Option<O> {
let _ = output;
None
}
fn nudges(&self) -> Vec<Box<dyn Rings>>;
fn deliver(
&mut self,
edge: usize,
batch: Vec<Item<O>>,
ack: AckSender<ItemId>,
) -> impl Future<Output = bool> + Send;
}
pub trait Socket {
type Refusal;
fn open<T>(&mut self) -> Result<Outbox<ItemId, T>, Self::Refusal>
where
T: Send + 'static;
fn opened<T>(&mut self) -> Option<Outbox<ItemId, T>>
where
T: Send + 'static;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, derive_more::IsVariant)]
pub enum Attached {
Seated(usize),
Occupied(usize),
Foreign,
}
#[diagnostic::on_unimplemented(
message = "a launch cannot BUILD `{Self}` as a downstream set",
label = "no `Attaches`",
note = "this is the seat `Declared::Wires<Out>` names, and a mounted node's set has to be \
fillable from the erased edges the wiring walk holds: `Downstream<T>` implements it, a \
node's own emitted `<Node>Downstream` implements it, and a set written by hand states it \
alongside its `Sink` rows",
note = "`Sink` says where a built set's outputs GO and this says how the set gets built — a type \
with the first and not the second can be driven by a hand-wired arrangement and not \
mounted by a graph root"
)]
pub trait Attaches: Default {
fn attach<S>(&mut self, socket: &mut S) -> Result<Attached, S::Refusal>
where
S: Socket;
}
#[derive(Debug)]
pub struct Downstream<T> {
edges: Vec<Outbox<ItemId, T>>,
}
impl<T> Downstream<T> {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self { edges: Vec::new() }
}
#[inline]
pub fn attach(&mut self, outbox: Outbox<ItemId, T>) -> &mut Self {
self.edges.push(outbox);
self
}
#[inline]
#[must_use]
pub fn edges(&self) -> &[Outbox<ItemId, T>] {
&self.edges
}
}
impl<T> Default for Downstream<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<T> FromIterator<Outbox<ItemId, T>> for Downstream<T> {
#[inline]
fn from_iter<I: IntoIterator<Item = Outbox<ItemId, T>>>(edges: I) -> Self {
Self {
edges: edges.into_iter().collect(),
}
}
}
impl<T> Attaches for Downstream<T>
where
T: Send + 'static,
{
#[inline]
fn attach<S>(&mut self, socket: &mut S) -> Result<Attached, S::Refusal>
where
S: Socket,
{
let edge = socket.open::<T>()?;
Self::attach(self, edge);
Ok(Attached::Seated(0))
}
}
impl<T> Sink<T> for Downstream<T>
where
T: Clone + Send + 'static,
{
#[inline]
fn arity(&self) -> usize {
self.edges.len()
}
#[inline]
fn duplicate(&self, output: &T) -> Option<T> {
Some(output.clone())
}
#[inline]
fn nudges(&self) -> Vec<Box<dyn Rings>> {
self
.edges
.iter()
.map(|edge| Box::new(edge.nudge()) as Box<dyn Rings>)
.collect()
}
#[inline]
async fn deliver(&mut self, edge: usize, batch: Vec<Item<T>>, ack: AckSender<ItemId>) -> bool {
self.edges[edge]
.deliver_async(Delivery::new(Payload::Batch(batch), ack))
.await
.is_ok()
}
}
#[cfg(test)]
mod tests;