Skip to main content

harmos_runtime/operations/
surface.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// In-process binding of the portable host: a thin layer over the
11// manager's own submission, so in-process and remote bodies share one shape.
12
13/* === Modules === */
14
15/* === Re-Exports === */
16
17/* === Imports === */
18
19// Standard Library Imports
20use std::future::Future;
21use std::sync::Arc;
22
23// Third Party Imports
24
25// Local Imports
26use super::caller::Identity;
27use super::{CallContext, Context, ContextError, ContextValue, OperationDeclaration, OperationInstance, OperationManager, Response};
28use crate::{Error, Result};
29
30/* === Constants === */
31
32/* === Macros === */
33
34/* === Type Aliases === */
35
36/* === Structs === */
37
38/* === Enums === */
39
40/* === Traits === */
41
42/* === Implementations === */
43
44impl harmos_sdk::operations::Host for OperationManager {
45    type Error = Error;
46    type Instance<O: OperationDeclaration> = OperationInstance<O>;
47
48    fn context(&self) -> &CallContext {
49        self.call_context()
50    }
51
52    fn with_value<T: ContextValue>(&self, value: T) -> std::result::Result<Self, ContextError> {
53        let context = self.call_context().with_value(value)?;
54        Ok(self.with_context(context))
55    }
56
57    fn submit<O: OperationDeclaration>(&self, request: O) -> impl Future<Output = Result<OperationInstance<O>>> + Send {
58        std::future::ready(OperationManager::submit(self, request))
59    }
60
61    fn call<O: OperationDeclaration>(&self, request: O) -> impl Future<Output = Result<Response<O::Response>>> + Send {
62        let submitted = OperationManager::submit(self, request);
63        async move { submitted?.await }
64    }
65
66    fn submit_with_resources<O: OperationDeclaration<Response = ()>, R: Send + Sync + 'static>(
67        &self, _: O, resources: Arc<R>,
68    ) -> impl Future<Output = Result<OperationInstance<O>>> + Send {
69        self.activate_resident::<O, R>(O::ID, resources, Identity::Host)
70    }
71}
72
73impl harmos_sdk::operations::Host for Context<'_> {
74    type Error = Error;
75    type Instance<O: OperationDeclaration> = OperationInstance<O>;
76
77    fn context(&self) -> &CallContext {
78        Context::context(self)
79    }
80
81    fn with_value<T: ContextValue>(&self, value: T) -> std::result::Result<Self, ContextError> {
82        let context = Context::context(self).with_value(value)?;
83        let mut child = self.clone();
84        child.operations = child.operations.with_context(context);
85        Ok(child)
86    }
87
88    fn submit<O: OperationDeclaration>(&self, request: O) -> impl Future<Output = Result<OperationInstance<O>>> + Send {
89        std::future::ready(Context::submit(self, request))
90    }
91
92    fn call<O: OperationDeclaration>(&self, request: O) -> impl Future<Output = Result<Response<O::Response>>> + Send {
93        let submitted = Context::submit(self, request);
94        async move { submitted?.await }
95    }
96
97    // A resident outlives the attempt that activated it; its lease names the
98    // attempt's caller.
99    fn submit_with_resources<O: OperationDeclaration<Response = ()>, R: Send + Sync + 'static>(
100        &self, _: O, resources: Arc<R>,
101    ) -> impl Future<Output = Result<OperationInstance<O>>> + Send {
102        self.operations.activate_resident::<O, R>(O::ID, resources, self.origin.identity.clone())
103    }
104}
105
106/* === Public Functions === */
107
108/* === Private Functions === */
109
110/* === Tests === */