datex_core/shared_values/shared_container/
apply.rs1use crate::{
2 core_compiler::instruction_input::InstructionInput,
3 instruction::{
4 instruction_data::ApplyData, regular_instruction::RegularInstruction,
5 },
6 prelude::*,
7 runtime::Runtime,
8 shared_values::{SharedContainer, traits::SharedContainerCommon},
9 traits::apply::{Apply, ApplyArgument, ApplyError},
10 values::{
11 core_values::callable::{Callable, error::CallableError},
12 value_container::ValueContainer,
13 },
14};
15
16impl Apply for SharedContainer {
17 fn try_apply_sync(
18 &self,
19 runtime: &Runtime,
20 args: Vec<ApplyArgument>,
21 ) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ApplyError> {
22 if !self.is_self_owned() {
23 return Err(ApplyError::AsyncCallableRequiresAsyncExecution);
24 }
25 let base = self.base_shared_container();
26 let callable = base
27 .value_container()
28 .try_as::<Callable>()
29 .ok_or(ApplyError::UnsupportedApply)?;
30 callable.try_apply_sync(runtime, args)
31 }
32
33 async fn try_apply_async(
34 &self,
35 runtime: &Runtime,
36 args: Vec<ApplyArgument>,
37 ) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ApplyError> {
38 if !self.is_self_owned() {
39 return self.apply_remote(runtime, args).await;
40 }
41
42 let callable = {
43 let base = self.base_shared_container();
44 let value = base
45 .value_container()
46 .try_as::<Callable>()
47 .ok_or(ApplyError::UnsupportedApply)?;
48 value.clone()
50 };
51 callable.try_apply_async(runtime, args).await
52 }
53}
54
55impl SharedContainer {
56 async fn apply_remote(
58 &self,
59 runtime: &Runtime,
60 args: Vec<ApplyArgument>,
61 ) -> Result<(Option<ValueContainer>, Vec<ValueContainer>), ApplyError> {
62 let mut instructions: Vec<InstructionInput> = vec![
63 RegularInstruction::Apply(ApplyData {
64 arg_count: args.len() as u8,
65 })
66 .into(),
67 ];
68 instructions.extend(
70 args.into_iter()
71 .map(|v| InstructionInput::ValueContainer(v.value)),
72 );
73 instructions.push(InstructionInput::ValueContainer(
75 ValueContainer::Shared(self.clone()),
76 ));
77
78 let is_debug = self.is_debug();
82
83 let res = runtime
84 .execute_instructions_remote_single_receiver(
85 self.pointer_address().endpoint(),
86 instructions,
87 is_debug,
88 )
89 .await
90 .map_err(|e| {
91 ApplyError::CallableError(Box::new(
92 CallableError::ExecutionError(e),
93 ))
94 })?;
95
96 Ok((res, vec![]))
97 }
98}