1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::ptr::null_mut;
use std::{mem, slice};

pub mod closure;
pub mod contact;
pub mod error;
pub mod ffi;
pub mod future;
pub mod message;
pub mod plugin;

#[repr(C)]
pub struct Managed {
    pub pointer: *mut (),
    pub drop: extern "C" fn(*mut ()),
}

unsafe impl Send for Managed {}
unsafe impl Sync for Managed {}

impl Managed {
    pub fn from_value<T>(value: T) -> Self {
        let b = Box::new(value);
        let ptr = Box::into_raw(b);

        extern "C" fn _drop<B>(pointer: *mut ()) {
            drop(unsafe { Box::from_raw(pointer.cast::<B>()) });
        }

        Self {
            pointer: ptr.cast(),
            drop: _drop::<T>,
        }
    }

    pub fn from_static<T>(static_ref: &'static T) -> Self {
        extern "C" fn _drop(_: *mut ()) {
            // nothing to do
        }

        Self {
            pointer: static_ref as *const _ as _,
            drop: _drop,
        }
    }

    pub fn as_mut_ptr(&self) -> *mut () {
        self.pointer
    }

    pub fn as_ptr(&self) -> *const () {
        self.pointer
    }

    pub fn into_value<T>(self) -> T {
        let ma = ManuallyDrop::new(self);
        *unsafe { Box::from_raw(ma.pointer as _) }
    }

    /// for option
    pub unsafe fn null() -> Self {
        extern "C" fn _drop_null(_: *mut ()) {}

        Self {
            pointer: null_mut(),
            drop: _drop_null,
        }
    }
}

impl Drop for Managed {
    fn drop(&mut self) {
        (self.drop)(self.pointer);
    }
}

#[repr(C)]
pub struct ManagedCloneable {
    pub value: Managed,
    clone: extern "C" fn(this: *const ()) -> ManagedCloneable,
}

impl ManagedCloneable {
    pub fn from_value<T: Clone>(value: T) -> Self {
        extern "C" fn _clone<T: Clone>(this: *const ()) -> ManagedCloneable {
            let this = unsafe { &*(this as *const T) };
            ManagedCloneable::from_value(this.clone())
        }

        let value = Managed::from_value(value);
        Self {
            value,
            clone: _clone::<T>,
        }
    }

    pub fn into_value<T>(self) -> T {
        self.value.into_value()
    }

    /// for option
    pub unsafe fn null() -> Self {
        extern "C" fn _clone_null(_: *const ()) -> ManagedCloneable {
            panic!("Shouldn't call this because this is null");
        }

        Self {
            value: Managed::null(),
            clone: _clone_null,
        }
    }
}

impl Clone for ManagedCloneable {
    fn clone(&self) -> Self {
        (self.clone)(self.value.pointer)
    }
}

impl Deref for ManagedCloneable {
    type Target = Managed;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

pub struct ManagedRef {
    pub pointer: *mut (),
}

impl ManagedRef {
    pub fn from_ref<T>(val: &T) -> Self {
        Self {
            pointer: val as *const T as usize as *mut (),
        }
    }
}

#[repr(C)]
pub struct RustString {
    pub ptr: *mut u8,
    pub len: usize,
    pub capacity: usize,
}

impl From<String> for RustString {
    fn from(s: String) -> Self {
        let mut ma = ManuallyDrop::new(s);
        let ptr = ma.as_mut_ptr();
        let len = ma.len();
        let cap = ma.capacity();

        Self {
            ptr,
            len,
            capacity: cap,
        }
    }
}

impl From<RustString> for String {
    fn from(s: RustString) -> Self {
        let str = unsafe { String::from_raw_parts(s.ptr, s.len, s.capacity) };
        str
    }
}

impl AsRef<str> for RustString {
    fn as_ref(&self) -> &str {
        unsafe {
            let slice = slice::from_raw_parts(self.ptr, self.len);
            std::str::from_utf8_unchecked(slice)
        }
    }
}

impl ToString for RustString {
    fn to_string(&self) -> String {
        self.as_ref().to_string()
    }
}

#[repr(C)]
pub struct RustStr {
    pub slice: *const u8,
    pub len: usize,
}

impl RustStr {
    pub fn as_str<'a>(&self) -> &'a str {
        unsafe {
            let slice = slice::from_raw_parts(self.slice, self.len);
            std::str::from_utf8_unchecked(slice)
        }
    }
}

impl From<&str> for RustStr {
    fn from(s: &str) -> Self {
        Self {
            slice: s.as_ptr(),
            len: s.len(),
        }
    }
}

impl AsRef<str> for RustStr {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl ToString for RustStr {
    fn to_string(&self) -> String {
        self.as_ref().to_string()
    }
}

#[repr(C)]
pub struct RustVec<T> {
    ptr: *mut T,
    len: usize,
    capacity: usize,
}

unsafe impl<T: Send> Send for RustVec<T> {}
unsafe impl<T: Sync> Sync for RustVec<T> {}

impl<T> RustVec<T> {
    pub fn into_vec(self) -> Vec<T> {
        unsafe { Vec::from_raw_parts(self.ptr, self.len, self.capacity) }
    }
}

impl<T> From<Vec<T>> for RustVec<T> {
    fn from(mut v: Vec<T>) -> Self {
        let (ptr, len, cap) = (v.as_mut_ptr(), v.len(), v.capacity());
        mem::forget(v);
        Self {
            ptr,
            len,
            capacity: cap,
        }
    }
}

pub struct RustSlice<T> {
    ptr: *const T,
    len: usize,
}

impl<T> RustSlice<T> {
    pub fn as_ptr(&self) -> *const T {
        self.ptr
    }

    pub fn as_slice(&self) -> &[T] {
        unsafe { slice::from_raw_parts(self.ptr, self.len) }
    }
}

impl<T> From<&[T]> for RustSlice<T> {
    fn from(slice: &[T]) -> Self {
        Self {
            ptr: slice.as_ptr(),
            len: slice.len(),
        }
    }
}

unsafe impl<T: Send> Send for RustSlice<T> {}
unsafe impl<T: Sync> Sync for RustSlice<T> {}

#[cfg(test)]
mod tests {
    use crate::{Managed, RustStr, RustString, RustVec};

    #[test]
    fn vec() {
        let v = vec![1, 1, 4, 5, 1, 4];
        let raw = RustVec::from(v);
        let v = raw.into_vec();

        assert_eq!(v, [1, 1, 4, 5, 1, 4]);
    }

    #[test]
    fn string() {
        let s = String::from("114514");
        let raw = RustString::from(s);
        let s = String::from(raw);

        assert_eq!(s, "114514");

        let slice = &s[1..];
        let raw = RustStr::from(slice);
        let slice = raw.as_ref();

        assert_eq!(slice, "14514");
    }

    #[test]
    fn managed_value() {
        #[derive(Debug, Clone)]
        struct Test {
            a: i32,
            b: usize,
            c: Option<Box<(Test, Test)>>,
        }

        impl PartialEq for Test {
            fn eq(&self, other: &Self) -> bool {
                if self.a != other.a {
                    return false;
                }
                if self.b != other.b {
                    return false;
                }

                self.c == other.c
            }
        }

        let test = Test {
            a: 233,
            b: 114514,
            c: Some(Box::new((
                Test {
                    a: 23114,
                    b: 114514,
                    c: None,
                },
                Test {
                    a: 114514,
                    b: 2333,
                    c: None,
                },
            ))),
        };
        let test0 = test.clone();
        let managed = Managed::from_value(test);
        let test: Test = managed.into_value();

        assert_eq!(test, test0);
    }
}