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

ambee/giterated

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

MOre pre vtable changes

Amber - ⁨1⁩ year ago

parent: tbd commit: ⁨9cfa135

⁨giterated-plugin/src/vtable/object.rs⁩ - ⁨4111⁩ bytes
Raw
1 use std::{mem::transmute, str::FromStr};
2
3 use giterated_abi::vtable::IntoObjectVTable;
4 use giterated_models::object::GiteratedObject;
5
6 use crate::FFIBox;
7
8 // #[derive(Clone, Copy)]
9 // #[repr(C)]
10 // pub struct ObjectVtable {
11 // object_kind: *const u8,
12 // object_kind_len: usize,
13 // pub to_str: unsafe extern "C" fn(&AnyObject) -> FFIBox<[u8]>,
14 // pub from_str: unsafe extern "C" fn(&str) -> Result<AnyObject, FFIBox<str>>,
15 // pub home_uri: unsafe extern "C" fn(&AnyObject) -> FFIBox<str>,
16 // pub is_same: unsafe extern "C" fn(&AnyObject) -> bool,
17 // }
18
19 // impl ObjectVtable {
20 // pub fn new<T: IntoObjectVTable>() -> Self {
21 // let object_kind = T::object_kind().as_ptr();
22 // let object_kind_len = T::object_kind().len();
23
24 // Self {
25 // to_str: T::to_str,
26 // from_str: T::from_str,
27 // home_uri: T::home_uri,
28 // is_same: T::is_same,
29 // object_kind,
30 // object_kind_len,
31 // }
32 // }
33
34 // pub fn kind(&self) -> &'static str {
35 // let slice = unsafe { std::slice::from_raw_parts(self.object_kind, self.object_kind_len) };
36
37 // std::str::from_utf8(slice).unwrap()
38 // }
39 // }
40
41 // pub trait IntoObjectVTable {
42 // fn object_kind() -> &'static str;
43 // unsafe extern "C" fn to_str(this: &AnyObject) -> FFIBox<[u8]>;
44 // unsafe extern "C" fn from_str(src: &str) -> Result<AnyObject, FFIBox<str>>;
45 // unsafe extern "C" fn home_uri(this: &AnyObject) -> FFIBox<str>;
46 // unsafe extern "C" fn is_same(other: &AnyObject) -> bool;
47 // }
48
49 impl<T: GiteratedObject + 'static> IntoObjectVTable for T {
50 unsafe extern "C" fn to_str(this: &AnyObject) -> FFIBox<[u8]> {
51 let this: &Box<T> = this.transmute_ref();
52
53 let result = this.to_string();
54
55 FFIBox::from_box(result.into_bytes().into_boxed_slice())
56 }
57
58 unsafe extern "C" fn from_str(src: &str) -> Result<AnyObject, FFIBox<str>> {
59 let result = T::from_object_str(src)
60 .map_err(|err| FFIBox::from_box(err.to_string().into_boxed_str()))?;
61
62 let any_object = AnyObject::new(result);
63
64 Ok(any_object)
65 }
66
67 unsafe extern "C" fn home_uri(_this: &AnyObject) -> FFIBox<str> {
68 todo!()
69 }
70
71 fn object_kind() -> &'static std::ffi::CStr {
72 <T as GiteratedObject>::object_name()
73 }
74
75 // unsafe extern "C" fn is_same(_other: &AnyObject) -> bool {
76 // todo!()
77 // }
78 }
79
80
81 // impl AnyObject {
82 // pub fn new<T: IntoObjectVTable>(inner: T) -> Self {
83 // Self {
84 // inner: FFIBox::from_box(Box::new(inner)).untyped(),
85 // vtable: ObjectVtable::new::<T>(),
86 // }
87 // }
88
89 // pub fn vtable(&self) -> ObjectVtable {
90 // self.vtable
91 // }
92
93 // pub fn object_kind(&self) -> &str {
94 // unsafe {
95 // std::str::from_utf8_unchecked(std::slice::from_raw_parts(
96 // self.vtable.object_kind,
97 // self.vtable.object_kind_len,
98 // ))
99 // }
100 // }
101
102 // pub fn home_uri(&self) -> String {
103 // unsafe { (self.vtable.home_uri)(self).to_string() }
104 // }
105
106 // pub fn is_same(&self, other: &AnyObject) -> bool {
107 // unsafe { (self.vtable.is_same)(other) }
108 // }
109
110 // pub unsafe fn cast<T: IntoObjectVTable + GiteratedObject>(self) -> T {
111 // assert_eq!(self.object_kind(), T::object_kind());
112
113 // info!("{}", self.to_string());
114
115 // T::from_object_str(&self.to_string()).unwrap()
116 // }
117 // }
118
119 // impl ToString for AnyObject {
120 // fn to_string(&self) -> String {
121 // let slice: Box<[u8]> = unsafe { Box::from_raw((self.vtable.to_str)(&self).0 as *mut _) };
122
123 // let string = unsafe { std::str::from_boxed_utf8_unchecked(slice) };
124
125 // String::from(string)
126 // }
127 // }
128
129 impl AnyObject {
130 pub unsafe fn transmute_owned<T>(&mut self) -> Box<T> {
131 Box::from_raw(self.inner.0 as *mut T)
132 }
133
134 pub unsafe fn transmute_ref<T>(&self) -> &T {
135 let ptr: *const T = transmute(self.inner.0);
136
137 ptr.as_ref().unwrap()
138 }
139 }
140