Warning, /sdk/rust-qt-binding-generator/tests/rust_functions/src/interface.rs is written in an unsupported language. File is not indexed.

0001 /* generated by rust_qt_binding_generator */
0002 use libc::{c_char, c_ushort, c_int};
0003 use std::slice;
0004 use std::char::decode_utf16;
0005 
0006 use std::sync::Arc;
0007 use std::sync::atomic::{AtomicPtr, Ordering};
0008 use std::ptr::null;
0009 
0010 use implementation::*;
0011 
0012 
0013 pub enum QString {}
0014 
0015 fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
0016     let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) };
0017     let characters = decode_utf16(utf16.iter().cloned())
0018         .map(|r| r.unwrap());
0019     s.clear();
0020     s.extend(characters);
0021 }
0022 
0023 
0024 
0025 pub enum QByteArray {}
0026 
0027 
0028 fn to_usize(n: c_int) -> usize {
0029     if n < 0 {
0030         panic!("Cannot cast {} to usize", n);
0031     }
0032     n as usize
0033 }
0034 
0035 
0036 fn to_c_int(n: usize) -> c_int {
0037     if n > c_int::max_value() as usize {
0038         panic!("Cannot cast {} to c_int", n);
0039     }
0040     n as c_int
0041 }
0042 
0043 
0044 pub struct PersonQObject {}
0045 
0046 pub struct PersonEmitter {
0047     qobject: Arc<AtomicPtr<PersonQObject>>,
0048     user_name_changed: extern fn(*mut PersonQObject),
0049 }
0050 
0051 unsafe impl Send for PersonEmitter {}
0052 
0053 impl PersonEmitter {
0054     /// Clone the emitter
0055     ///
0056     /// The emitter can only be cloned when it is mutable. The emitter calls
0057     /// into C++ code which may call into Rust again. If emmitting is possible
0058     /// from immutable structures, that might lead to access to a mutable
0059     /// reference. That is undefined behaviour and forbidden.
0060     pub fn clone(&mut self) -> PersonEmitter {
0061         PersonEmitter {
0062             qobject: self.qobject.clone(),
0063             user_name_changed: self.user_name_changed,
0064         }
0065     }
0066     fn clear(&self) {
0067         let n: *const PersonQObject = null();
0068         self.qobject.store(n as *mut PersonQObject, Ordering::SeqCst);
0069     }
0070     pub fn user_name_changed(&mut self) {
0071         let ptr = self.qobject.load(Ordering::SeqCst);
0072         if !ptr.is_null() {
0073             (self.user_name_changed)(ptr);
0074         }
0075     }
0076 }
0077 
0078 pub trait PersonTrait {
0079     fn new(emit: PersonEmitter) -> Self;
0080     fn emit(&mut self) -> &mut PersonEmitter;
0081     fn user_name(&self) -> &str;
0082     fn set_user_name(&mut self, value: String);
0083     fn append(&mut self, suffix: String, amount: u32) -> ();
0084     fn double_name(&mut self) -> ();
0085     fn greet(&self, name: String) -> String;
0086     fn quote(&self, prefix: String, suffix: String) -> String;
0087     fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> Vec<u8>;
0088     fn vowels_in_name(&self) -> u8;
0089 }
0090 
0091 #[no_mangle]
0092 pub extern "C" fn person_new(
0093     person: *mut PersonQObject,
0094     person_user_name_changed: extern fn(*mut PersonQObject),
0095 ) -> *mut Person {
0096     let person_emit = PersonEmitter {
0097         qobject: Arc::new(AtomicPtr::new(person)),
0098         user_name_changed: person_user_name_changed,
0099     };
0100     let d_person = Person::new(person_emit);
0101     Box::into_raw(Box::new(d_person))
0102 }
0103 
0104 #[no_mangle]
0105 pub unsafe extern "C" fn person_free(ptr: *mut Person) {
0106     Box::from_raw(ptr).emit().clear();
0107 }
0108 
0109 #[no_mangle]
0110 pub unsafe extern "C" fn person_user_name_get(
0111     ptr: *const Person,
0112     p: *mut QString,
0113     set: extern fn(*mut QString, *const c_char, c_int),
0114 ) {
0115     let o = &*ptr;
0116     let v = o.user_name();
0117     let s: *const c_char = v.as_ptr() as *const c_char;
0118     set(p, s, to_c_int(v.len()));
0119 }
0120 
0121 #[no_mangle]
0122 pub unsafe extern "C" fn person_user_name_set(ptr: *mut Person, v: *const c_ushort, len: c_int) {
0123     let o = &mut *ptr;
0124     let mut s = String::new();
0125     set_string_from_utf16(&mut s, v, len);
0126     o.set_user_name(s);
0127 }
0128 
0129 #[no_mangle]
0130 pub unsafe extern "C" fn person_append(ptr: *mut Person, suffix_str: *const c_ushort, suffix_len: c_int, amount: u32) {
0131     let mut suffix = String::new();
0132     set_string_from_utf16(&mut suffix, suffix_str, suffix_len);
0133     let o = &mut *ptr;
0134     o.append(suffix, amount)
0135 }
0136 
0137 #[no_mangle]
0138 pub unsafe extern "C" fn person_double_name(ptr: *mut Person) {
0139     let o = &mut *ptr;
0140     o.double_name()
0141 }
0142 
0143 #[no_mangle]
0144 pub unsafe extern "C" fn person_greet(ptr: *const Person, name_str: *const c_ushort, name_len: c_int, d: *mut QString, set: extern fn(*mut QString, str: *const c_char, len: c_int)) {
0145     let mut name = String::new();
0146     set_string_from_utf16(&mut name, name_str, name_len);
0147     let o = &*ptr;
0148     let r = o.greet(name);
0149     let s: *const c_char = r.as_ptr() as *const c_char;
0150     set(d, s, r.len() as i32);
0151 }
0152 
0153 #[no_mangle]
0154 pub unsafe extern "C" fn person_quote(ptr: *const Person, prefix_str: *const c_ushort, prefix_len: c_int, suffix_str: *const c_ushort, suffix_len: c_int, d: *mut QString, set: extern fn(*mut QString, str: *const c_char, len: c_int)) {
0155     let mut prefix = String::new();
0156     set_string_from_utf16(&mut prefix, prefix_str, prefix_len);
0157     let mut suffix = String::new();
0158     set_string_from_utf16(&mut suffix, suffix_str, suffix_len);
0159     let o = &*ptr;
0160     let r = o.quote(prefix, suffix);
0161     let s: *const c_char = r.as_ptr() as *const c_char;
0162     set(d, s, r.len() as i32);
0163 }
0164 
0165 #[no_mangle]
0166 pub unsafe extern "C" fn person_quote_bytes(ptr: *const Person, prefix_str: *const c_char, prefix_len: c_int, suffix_str: *const c_char, suffix_len: c_int, d: *mut QByteArray, set: extern fn(*mut QByteArray, str: *const c_char, len: c_int)) {
0167     let prefix = { slice::from_raw_parts(prefix_str as *const u8, to_usize(prefix_len)) };
0168     let suffix = { slice::from_raw_parts(suffix_str as *const u8, to_usize(suffix_len)) };
0169     let o = &*ptr;
0170     let r = o.quote_bytes(prefix, suffix);
0171     let s: *const c_char = r.as_ptr() as *const c_char;
0172     set(d, s, r.len() as i32);
0173 }
0174 
0175 #[no_mangle]
0176 pub unsafe extern "C" fn person_vowels_in_name(ptr: *const Person) -> u8 {
0177     let o = &*ptr;
0178     o.vowels_in_name()
0179 }