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/state.rs⁩ - ⁨4896⁩ bytes
Raw
1 use std::{
2 marker::PhantomData,
3 mem::{transmute, MaybeUninit},
4 ptr::{null, null_mut, read},
5 };
6
7 use anyhow::Error;
8
9 pub trait FromOperationState<O, D>: Sized {
10 fn from_operation_state(
11 state: &mut State,
12 object: &O,
13 operation: &D,
14 ) -> Result<Self, OperationError<Error>>;
15 }
16
17 pub struct StateExtractor<T>(pub T);
18
19 impl<T: StateUUID> FromState for StateExtractor<T> {
20 fn from_state(state: &mut State) -> Result<Self, Error> {
21 todo!()
22 }
23 }
24
25 use c_linked_list::CLinkedListMut;
26 use giterated_models::{error::OperationError, value};
27
28 use crate::{
29 value_ex::FfiValueUntyped,
30 vtable::{runtime::RuntimeHandle, VTable},
31 FfiValue,
32 };
33
34 #[derive(Debug)]
35 #[repr(transparent)]
36 pub struct State {
37 list: CLinkedListMut<StateItem<()>, fn(&StateItem<()>) -> *mut StateItem<()>>,
38 }
39
40 unsafe impl Send for State {}
41 unsafe impl Sync for State {}
42
43 impl Default for State {
44 fn default() -> Self {
45 let value = FfiValue::new(StateItem {
46 next_item: core::ptr::null(),
47 state_uuid: 0,
48 state: (),
49 });
50
51 State {
52 list: unsafe {
53 CLinkedListMut::from_ptr(value.ptr() as *const StateItem<()> as *mut _, |n| {
54 n.next_item as *mut StateItem<()>
55 })
56 },
57 }
58 }
59 }
60
61 #[repr(transparent)]
62 struct StateHandle {
63 state: FfiValueUntyped,
64 }
65
66 impl StateHandle {
67 pub unsafe fn clone_unsafe(&self) -> Self {
68 Self {
69 state: FfiValueUntyped {
70 inner: self.state.inner,
71 _type_marker: PhantomData,
72 _abi_marker: PhantomData,
73 },
74 }
75 }
76 }
77
78 #[repr(C)]
79 pub struct StateItem<T: ?Sized> {
80 /// The pointer to the next item.
81 ///
82 /// `next_item` is most likely always an `FfiValue<StateItem<()>>` and that's how we free them.
83 next_item: *const StateItem<()>,
84 pub state_uuid: u128,
85 pub state: T,
86 }
87
88 impl<T> std::fmt::Debug for StateItem<T> {
89 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
90 f.debug_struct("StateItem")
91 .field("state_uuid", &self.state_uuid)
92 .finish()
93 }
94 }
95
96 impl Drop for State {
97 fn drop(&mut self) {
98 let state_manager = unsafe { StateManager::new(self) };
99
100 // for state in state_manager {}
101 }
102 }
103
104 pub struct StateManager<'s, S> {
105 state: S,
106 _marker: PhantomData<&'s ()>,
107 }
108
109 impl<'s> StateManager<'s, &'s mut State> {
110 pub unsafe fn new_mut(handle: &'s mut State) -> Self {
111 Self {
112 state: handle,
113 _marker: PhantomData,
114 }
115 }
116
117 pub unsafe fn write_state<S: StateUUID>(&mut self, state: S) -> &mut Self {
118 let list = &mut self.state.list;
119 let last_item = list.iter_mut().last().unwrap();
120
121 last_item.next_item = unsafe {
122 FfiValue::new(StateItem {
123 next_item: null(),
124 state_uuid: S::uuid(),
125 state,
126 })
127 .ptr()
128 } as *const StateItem<()>;
129
130 self
131 }
132 }
133
134 impl<'s> StateManager<'s, &'s State> {
135 pub unsafe fn new<'o: 's>(handle: &'o State) -> Self {
136 Self {
137 state: handle,
138 _marker: PhantomData,
139 }
140 }
141
142 pub unsafe fn get_state<S: StateUUID>(&self) -> Option<&S> {
143 println!("state info: {:#?}", self.state);
144
145 for state in self.state.list.iter() {
146 println!("iter");
147 if state.state_uuid == S::uuid() {
148 let state: *const StateItem<S> = unsafe { transmute(&state) };
149
150 let value_ref = state.as_ref().unwrap();
151 let value_ref = &value_ref.state as *const S;
152
153 return value_ref.as_ref();
154 }
155 }
156
157 None
158 }
159 }
160
161 pub trait StateUUID {
162 fn uuid() -> u128;
163
164 fn unsafe_hint_copy() -> Option<bool> {
165 None
166 }
167 }
168
169 /// State values for the current execution domain. 99.99% of the time this means "plugin-specific"
170 ///
171 /// The remainder 0.01% of the time it refers to the daemon's runtime domain.
172 #[repr(transparent)]
173 pub struct DomainState(pub &'static State);
174
175 impl StateUUID for DomainState {
176 fn uuid() -> u128 {
177 32894238940832489328402398490328423
178 }
179 }
180
181 pub struct RuntimeState(StateItem<&'static VTable<RuntimeHandle>>);
182
183 impl StateUUID for RuntimeState {
184 fn uuid() -> u128 {
185 todo!()
186 }
187 }
188
189 impl RuntimeState {
190 pub fn queue_insert_state<S: StateUUID>(&mut self, state: S) {
191 todo!()
192 }
193 }
194
195 pub trait FromState: Sized {
196 fn from_state(state: &mut State) -> Result<Self, Error>;
197 }
198
199 impl<T: StateUUID> FromState for T {
200 fn from_state(state: &mut State) -> Result<Self, Error> {
201 todo!()
202 }
203 }
204
205 impl<T: FromState> FromState for Option<T> {
206 fn from_state(state: &mut State) -> Result<Self, Error> {
207 todo!()
208 }
209 }
210