Changeset View
Changeset View
Standalone View
Standalone View
tests/rust_functions/src/interface.rs
- This file was added.
1 | /* generated by rust_qt_binding_generator */ | ||||
---|---|---|---|---|---|
2 | #![allow(unknown_lints)] | ||||
3 | #![allow(mutex_atomic, needless_pass_by_value)] | ||||
4 | use libc::{c_int, c_void, uint8_t, uint16_t}; | ||||
5 | use std::slice; | ||||
6 | | ||||
7 | use std::sync::{Arc, Mutex}; | ||||
8 | use std::ptr::null; | ||||
9 | | ||||
10 | use implementation::*; | ||||
11 | | ||||
12 | | ||||
13 | #[repr(C)] | ||||
14 | pub struct QString { | ||||
15 | data: *const uint8_t, | ||||
16 | len: c_int, | ||||
17 | } | ||||
18 | | ||||
19 | #[repr(C)] | ||||
20 | pub struct QStringIn { | ||||
21 | data: *const uint16_t, | ||||
22 | len: c_int, | ||||
23 | } | ||||
24 | | ||||
25 | impl QStringIn { | ||||
26 | fn convert(&self) -> String { | ||||
27 | let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) }; | ||||
28 | String::from_utf16_lossy(data) | ||||
29 | } | ||||
30 | } | ||||
31 | | ||||
32 | impl<'a> From<&'a str> for QString { | ||||
33 | fn from(string: &'a str) -> QString { | ||||
34 | QString { | ||||
35 | len: string.len() as c_int, | ||||
36 | data: string.as_ptr(), | ||||
37 | } | ||||
38 | } | ||||
39 | } | ||||
40 | | ||||
41 | impl<'a> From<&'a String> for QString { | ||||
42 | fn from(string: &'a String) -> QString { | ||||
43 | QString { | ||||
44 | len: string.len() as c_int, | ||||
45 | data: string.as_ptr(), | ||||
46 | } | ||||
47 | } | ||||
48 | } | ||||
49 | | ||||
50 | pub struct PersonQObject {} | ||||
51 | | ||||
52 | #[derive(Clone)] | ||||
53 | pub struct PersonEmitter { | ||||
54 | qobject: Arc<Mutex<*const PersonQObject>>, | ||||
55 | user_name_changed: fn(*const PersonQObject), | ||||
56 | } | ||||
57 | | ||||
58 | unsafe impl Send for PersonEmitter {} | ||||
59 | | ||||
60 | impl PersonEmitter { | ||||
61 | fn clear(&self) { | ||||
62 | *self.qobject.lock().unwrap() = null(); | ||||
63 | } | ||||
64 | pub fn user_name_changed(&self) { | ||||
65 | let ptr = *self.qobject.lock().unwrap(); | ||||
66 | if !ptr.is_null() { | ||||
67 | (self.user_name_changed)(ptr); | ||||
68 | } | ||||
69 | } | ||||
70 | } | ||||
71 | | ||||
72 | pub trait PersonTrait { | ||||
73 | fn new(emit: PersonEmitter) -> Self; | ||||
74 | fn emit(&self) -> &PersonEmitter; | ||||
75 | fn user_name(&self) -> &str; | ||||
76 | fn set_user_name(&mut self, value: String); | ||||
77 | fn double_name(&mut self) -> (); | ||||
78 | fn greet(&self, Name: String) -> String; | ||||
79 | fn vowels_in_name(&self) -> u8; | ||||
80 | } | ||||
81 | | ||||
82 | #[no_mangle] | ||||
83 | pub extern "C" fn person_new( | ||||
84 | person: *mut PersonQObject, | ||||
85 | user_name_changed: fn(*const PersonQObject), | ||||
86 | ) -> *mut Person { | ||||
87 | let person_emit = PersonEmitter { | ||||
88 | qobject: Arc::new(Mutex::new(person)), | ||||
89 | user_name_changed: user_name_changed, | ||||
90 | }; | ||||
91 | let d_person = Person::new(person_emit); | ||||
92 | Box::into_raw(Box::new(d_person)) | ||||
93 | } | ||||
94 | | ||||
95 | #[no_mangle] | ||||
96 | pub unsafe extern "C" fn person_free(ptr: *mut Person) { | ||||
97 | Box::from_raw(ptr).emit().clear(); | ||||
98 | } | ||||
99 | | ||||
100 | #[no_mangle] | ||||
101 | pub unsafe extern "C" fn person_user_name_get( | ||||
102 | ptr: *const Person, | ||||
103 | p: *mut c_void, | ||||
104 | set: fn(*mut c_void, QString), | ||||
105 | ) { | ||||
106 | let data = (&*ptr).user_name(); | ||||
107 | set(p, data.into()); | ||||
108 | } | ||||
109 | | ||||
110 | #[no_mangle] | ||||
111 | pub unsafe extern "C" fn person_user_name_set(ptr: *mut Person, v: QStringIn) { | ||||
112 | (&mut *ptr).set_user_name(v.convert()); | ||||
113 | } | ||||
114 | | ||||
115 | #[no_mangle] | ||||
116 | pub unsafe extern "C" fn person_double_name(ptr: *mut Person) -> () { | ||||
117 | (&mut *ptr).double_name() | ||||
118 | } | ||||
119 | | ||||
120 | #[no_mangle] | ||||
121 | pub unsafe extern "C" fn person_greet(ptr: *const Person, Name: QStringIn, d: *mut c_void, set: fn(*mut c_void, QString)) { | ||||
122 | let data = (&*ptr).greet(Name.convert()); | ||||
123 | set(d, (&data).into()); | ||||
124 | } | ||||
125 | | ||||
126 | #[no_mangle] | ||||
127 | pub unsafe extern "C" fn person_vowels_in_name(ptr: *const Person) -> u8 { | ||||
128 | (&*ptr).vowels_in_name() | ||||
129 | } |