JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

no clue what this is

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨7889bf6

⁨giterated-runtime/giterated-abi/src/vtable/runtime.rs⁩ - ⁨3829⁩ bytes
Raw
1 use giterated_models::{
2 error::OperationError,
3 object::{self, GiteratedObject, ObjectRequestError},
4 object_backend::ObjectBackend,
5 operation::{GiteratedOperation, OperationState},
6 };
7
8 use crate::{
9 result::{FfiError, FfiResult},
10 state::State,
11 value_ex::FfiValueUntyped,
12 FfiFuture, FfiSliceRef, FfiValueMut, StackPinned,
13 };
14
15 use core::fmt::Debug;
16
17 use super::{Object, ObjectABI, VTable};
18
19 #[derive(Clone)]
20 pub struct RuntimeHandle {
21 pub vtable: &'static VTable<RuntimeHandle>,
22 }
23
24 impl ObjectABI for RuntimeHandle {
25 type VTable = RuntimeVTable;
26 }
27
28 impl RuntimeHandle {
29 pub async fn handle_serialized(
30 &self,
31 object: &str,
32 operation_type: &str,
33 operation_payload: &[u8],
34 ) -> Result<Vec<u8>, OperationError<Vec<u8>>> {
35 todo!()
36 }
37
38 pub async fn handle_typed<O, D>(
39 &self,
40 object: O,
41 operation: D,
42 ) -> Result<D::Success, OperationError<D::Failure>>
43 where
44 O: GiteratedObject,
45 D: GiteratedOperation<O>,
46 {
47 todo!()
48 }
49
50 pub fn inner_get_object(
51 &self,
52 object_str: &str,
53 state: &mut State,
54 ) -> Result<Object, OperationError<ObjectRequestError>> {
55 let pinned_object_str = (*object_str.as_bytes()).pin();
56 let mut pinned_state = (*state).pin();
57
58 let result = unsafe {
59 (self.vtable.get_object)(unsafe { pinned_object_str.grant_ref() }, unsafe {
60 pinned_state.grant_mut()
61 })
62 };
63
64 todo!()
65 }
66 }
67
68 #[derive(Clone, Copy)]
69 pub struct RuntimeVTable {
70 pub(crate) handle_fn: unsafe extern "C" fn(
71 FfiSliceRef<str>,
72 FfiSliceRef<str>,
73 FfiSliceRef<str>,
74 FfiSliceRef<[u8]>,
75 FfiSliceRef<[u8]>,
76 ) -> FfiFuture<
77 FfiResult<FfiValueUntyped, OperationError<FfiError>>,
78 >,
79 pub(crate) get_object:
80 unsafe extern "C" fn(
81 object_str: FfiSliceRef<u8>,
82 state: FfiValueMut<State>,
83 ) -> FfiResult<Object, OperationError<ObjectRequestError>>,
84 }
85
86 impl RuntimeVTable {
87 pub const fn new<R: IntoRuntimeVtable>() -> RuntimeVTable {
88 RuntimeVTable {
89 handle_fn: R::handle,
90 get_object: R::get_object,
91 }
92 }
93 }
94
95 unsafe impl Send for RuntimeVTable {}
96 unsafe impl Sync for RuntimeVTable {}
97
98 pub trait IntoRuntimeVtable: Sized {
99 const VTABLE: RuntimeVTable = RuntimeVTable::new::<Self>();
100
101 unsafe extern "C" fn handle(
102 object_kind: FfiSliceRef<str>,
103 operation_name: FfiSliceRef<str>,
104 object: FfiSliceRef<str>,
105 operation_payload: FfiSliceRef<[u8]>,
106 operation_state: FfiSliceRef<[u8]>,
107 ) -> FfiFuture<FfiResult<FfiValueUntyped, OperationError<FfiError>>>;
108
109 unsafe extern "C" fn get_object(
110 object_str: FfiSliceRef<u8>,
111 operation_state: FfiValueMut<State>,
112 ) -> FfiResult<Object, OperationError<ObjectRequestError>>;
113 }
114
115 #[async_trait::async_trait(?Send)]
116 impl ObjectBackend<State> for RuntimeHandle {
117 async fn object_operation<O, D>(
118 &self,
119 object: O,
120 operation: &str,
121 payload: D,
122 operation_state: &mut State,
123 ) -> Result<D::Success, OperationError<D::Failure>>
124 where
125 O: GiteratedObject + Debug + 'static,
126 D: GiteratedOperation<O> + Debug + 'static,
127 D::Success: Clone,
128 D::Failure: Clone,
129 {
130 todo!()
131 }
132
133 async fn get_object<O: GiteratedObject + Debug + 'static>(
134 &self,
135 object_str: &str,
136 operation_state: &mut State,
137 ) -> Result<object::Object<O, Self, State>, OperationError<ObjectRequestError>> {
138 let result = self.inner_get_object(object_str, operation_state)?;
139
140 Ok(unsafe { object::Object::new_unchecked(result.cast(), self.clone()) })
141 }
142 }
143