geam_core/host/external/
stored.rs1use super::store::ExternalPayloadView;
2use crate::host::type_::HostTypeAt;
3use crate::host::{
4 HostCall, HostCallRuntime, HostProfile, HostProvider, HostType, HostTypeSequence,
5};
6use std::marker::PhantomData;
7use std::ops::Deref;
8
9pub struct HostStoredValue<Type> {
43 pub(crate) value: crate::runtime::StoredRuntimeValue,
44 marker: PhantomData<fn() -> Type>,
45}
46
47pub struct HostStoredType<Index>(PhantomData<Index>);
49
50pub struct HostExternalPayloadBuilder<'runtime, Profile, Arguments>
52where
53 Profile: HostProfile,
54 Arguments: HostTypeSequence,
55{
56 pub(super) runtime: &'runtime mut dyn HostCallRuntime<Profile>,
57 arguments: PhantomData<Arguments>,
58}
59
60pub struct HostExternalPayloadView<'call, Payload, Arguments>
72where
73 Arguments: HostTypeSequence,
74{
75 pub(super) value: ExternalPayloadView<Payload>,
76 call: PhantomData<&'call Arguments>,
77}
78
79impl<Type> HostStoredValue<Type> {
80 pub(crate) fn new(value: crate::runtime::StoredRuntimeValue) -> Self {
81 Self {
82 value,
83 marker: PhantomData,
84 }
85 }
86}
87
88impl<'runtime, Profile, Arguments> HostExternalPayloadBuilder<'runtime, Profile, Arguments>
89where
90 Profile: HostProfile,
91 Arguments: HostTypeSequence,
92{
93 pub(crate) fn new(runtime: &'runtime mut dyn HostCallRuntime<Profile>) -> Self {
94 Self {
95 runtime,
96 arguments: PhantomData,
97 }
98 }
99
100 pub fn store<Type>(&mut self, value: Type::Value<'_>) -> HostStoredValue<Type>
102 where
103 Type: HostType,
104 {
105 HostStoredValue::new(
106 self.runtime
107 .retain_stored(crate::host::type_::into_scoped::<Type>(value)),
108 )
109 }
110
111 pub fn store_argument<Index>(
113 &mut self,
114 value: <<Arguments as HostTypeAt<Index>>::Type as HostType>::Value<'_>,
115 ) -> HostStoredValue<HostStoredType<Index>>
116 where
117 Arguments: HostTypeAt<Index>,
118 {
119 HostStoredValue::new(
120 self.runtime
121 .retain_stored(crate::host::type_::into_scoped::<
122 <Arguments as HostTypeAt<Index>>::Type,
123 >(value)),
124 )
125 }
126}
127
128impl<'call, Payload, Arguments> HostExternalPayloadView<'call, Payload, Arguments>
129where
130 Arguments: HostTypeSequence,
131{
132 pub(crate) fn new(value: ExternalPayloadView<Payload>) -> Self {
133 Self {
134 value,
135 call: PhantomData,
136 }
137 }
138
139 pub fn restore<Profile, Provider, Return, Type>(
141 &self,
142 call: &mut HostCall<'call, Profile, Provider, Return>,
143 select: impl FnOnce(&Payload) -> &HostStoredValue<Type>,
144 ) -> Type::Value<'call>
145 where
146 Profile: HostProfile,
147 Provider: HostProvider<Profile>,
148 Return: HostType,
149 Type: HostType,
150 {
151 call.restore_stored::<Type, Type>(select(&self.value))
152 }
153
154 pub fn restore_argument<Profile, Provider, Return, Index>(
156 &self,
157 call: &mut HostCall<'call, Profile, Provider, Return>,
158 select: impl FnOnce(&Payload) -> &HostStoredValue<HostStoredType<Index>>,
159 ) -> <<Arguments as HostTypeAt<Index>>::Type as HostType>::Value<'call>
160 where
161 Profile: HostProfile,
162 Provider: HostProvider<Profile>,
163 Return: HostType,
164 Arguments: HostTypeAt<Index>,
165 {
166 call.restore_stored::<<Arguments as HostTypeAt<Index>>::Type, _>(select(&self.value))
167 }
168}
169
170impl<Payload, Arguments> Deref for HostExternalPayloadView<'_, Payload, Arguments>
171where
172 Arguments: HostTypeSequence,
173{
174 type Target = Payload;
175
176 fn deref(&self) -> &Self::Target {
177 &self.value
178 }
179}