Skip to main content

harmos_runtime/operations/
catalog.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16// Standard Library Imports
17use std::marker::PhantomData;
18use std::sync::Arc;
19// Third Party Imports
20
21// Local Imports
22use super::{Bindings, Context, Operation};
23use crate::catalog::{Member, Visitor};
24use crate::schema::Described;
25use crate::{Message, Result};
26
27/* === Constants === */
28
29/* === Macros === */
30
31/* === Type Aliases === */
32
33type Register<H> = fn(&mut Bindings<'_>, &'static str, Arc<H>) -> Result<()>;
34
35/* === Structs === */
36
37pub 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
45/* === Enums === */
46
47/* === Traits === */
48
49/* === Implementations === */
50
51impl<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
83/* === Public Functions === */
84
85/* === Private Functions === */
86
87fn 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/* === Tests === */