Warning, /sdk/rust-qt-binding-generator/tests/rust_object/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 fn to_usize(n: c_int) -> usize {
0026     if n < 0 {
0027         panic!("Cannot cast {} to usize", n);
0028     }
0029     n as usize
0030 }
0031 
0032 
0033 fn to_c_int(n: usize) -> c_int {
0034     if n > c_int::max_value() as usize {
0035         panic!("Cannot cast {} to c_int", n);
0036     }
0037     n as c_int
0038 }
0039 
0040 
0041 pub struct PersonQObject {}
0042 
0043 pub struct PersonEmitter {
0044     qobject: Arc<AtomicPtr<PersonQObject>>,
0045     user_name_changed: extern fn(*mut PersonQObject),
0046 }
0047 
0048 unsafe impl Send for PersonEmitter {}
0049 
0050 impl PersonEmitter {
0051     /// Clone the emitter
0052     ///
0053     /// The emitter can only be cloned when it is mutable. The emitter calls
0054     /// into C++ code which may call into Rust again. If emmitting is possible
0055     /// from immutable structures, that might lead to access to a mutable
0056     /// reference. That is undefined behaviour and forbidden.
0057     pub fn clone(&mut self) -> PersonEmitter {
0058         PersonEmitter {
0059             qobject: self.qobject.clone(),
0060             user_name_changed: self.user_name_changed,
0061         }
0062     }
0063     fn clear(&self) {
0064         let n: *const PersonQObject = null();
0065         self.qobject.store(n as *mut PersonQObject, Ordering::SeqCst);
0066     }
0067     pub fn user_name_changed(&mut self) {
0068         let ptr = self.qobject.load(Ordering::SeqCst);
0069         if !ptr.is_null() {
0070             (self.user_name_changed)(ptr);
0071         }
0072     }
0073 }
0074 
0075 pub trait PersonTrait {
0076     fn new(emit: PersonEmitter) -> Self;
0077     fn emit(&mut self) -> &mut PersonEmitter;
0078     fn user_name(&self) -> &str;
0079     fn set_user_name(&mut self, value: String);
0080 }
0081 
0082 #[no_mangle]
0083 pub extern "C" fn person_new(
0084     person: *mut PersonQObject,
0085     person_user_name_changed: extern fn(*mut PersonQObject),
0086 ) -> *mut Person {
0087     let person_emit = PersonEmitter {
0088         qobject: Arc::new(AtomicPtr::new(person)),
0089         user_name_changed: person_user_name_changed,
0090     };
0091     let d_person = Person::new(person_emit);
0092     Box::into_raw(Box::new(d_person))
0093 }
0094 
0095 #[no_mangle]
0096 pub unsafe extern "C" fn person_free(ptr: *mut Person) {
0097     Box::from_raw(ptr).emit().clear();
0098 }
0099 
0100 #[no_mangle]
0101 pub unsafe extern "C" fn person_user_name_get(
0102     ptr: *const Person,
0103     p: *mut QString,
0104     set: extern fn(*mut QString, *const c_char, c_int),
0105 ) {
0106     let o = &*ptr;
0107     let v = o.user_name();
0108     let s: *const c_char = v.as_ptr() as *const c_char;
0109     set(p, s, to_c_int(v.len()));
0110 }
0111 
0112 #[no_mangle]
0113 pub unsafe extern "C" fn person_user_name_set(ptr: *mut Person, v: *const c_ushort, len: c_int) {
0114     let o = &mut *ptr;
0115     let mut s = String::new();
0116     set_string_from_utf16(&mut s, v, len);
0117     o.set_user_name(s);
0118 }