harmos_runtime/operations/
catalog.rs1use std::marker::PhantomData;
18use std::sync::Arc;
19use super::{Bindings, Context, Operation};
23use crate::catalog::{Member, Visitor};
24use crate::schema::Described;
25use crate::{Message, Result};
26
27type Register<H> = fn(&mut Bindings<'_>, &'static str, Arc<H>) -> Result<()>;
34
35pub struct Implementations<R>(PhantomData<fn() -> R>);
38
39pub struct WireImplementations<R>(PhantomData<fn() -> R>);
40
41pub struct Binding<H> {
42 pub(super) register: Register<H>,
43}
44
45impl<R: Send + Sync + 'static> Visitor for Implementations<R> {
52 type Entry = Binding<R>;
53}
54
55impl<R: Send + Sync + 'static> Visitor for WireImplementations<R> {
56 type Entry = Binding<R>;
57}
58
59impl<T, R> Member<T> for Implementations<R>
60where
61 R: Send + Sync + 'static,
62 T: for<'ctx> Operation<Resources = R, Context<'ctx> = Context<'ctx>>,
63{
64 const ENTRY: Binding<R> = Binding { register: register_owned::<T> };
65}
66
67impl<T, R> Member<T> for WireImplementations<R>
68where
69 R: Send + Sync + 'static,
70 T: for<'ctx> Operation<Resources = R, Context<'ctx> = Context<'ctx>>,
71 T::Declaration: crate::operations::OperationDeclaration,
72 T::Declaration: Message + Described,
73{
74 const ENTRY: Binding<R> = Binding { register: register_owned_wire::<T> };
75}
76
77impl<H> Binding<H> {
78 pub(super) fn apply(&self, bindings: &mut Bindings<'_>, lane: &'static str, handler: Arc<H>) -> Result<()> {
79 (self.register)(bindings, lane, handler)
80 }
81}
82
83fn register_owned<T>(bindings: &mut Bindings<'_>, lane: &'static str, resources: Arc<T::Resources>) -> Result<()>
88where
89 T: for<'ctx> Operation<Context<'ctx> = Context<'ctx>>,
90{
91 bindings.register_owned_at::<T>(lane, resources)
92}
93
94fn register_owned_wire<T>(bindings: &mut Bindings<'_>, lane: &'static str, resources: Arc<T::Resources>) -> Result<()>
95where
96 T: for<'ctx> Operation<Context<'ctx> = Context<'ctx>>,
97 T::Declaration: crate::operations::OperationDeclaration,
98 T::Declaration: Message + Described,
99{
100 bindings.register_owned_wire_at::<T>(lane, resources)
101}
102
103