rust-qt-binding-generator/tests/rust_object/src/interface.rs

109 lines
2.5 KiB
Rust
Raw Normal View History

2017-08-12 05:03:11 -07:00
/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
2018-05-16 00:12:03 -07:00
use libc::{c_char, c_ushort, c_int};
use std::slice;
2018-05-01 14:46:17 -07:00
use std::char::decode_utf16;
2017-08-12 05:03:11 -07:00
use std::sync::{Arc, Mutex};
use std::ptr::null;
use implementation::*;
2018-05-01 14:46:17 -07:00
pub enum QString {}
2018-05-01 14:46:17 -07:00
fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) };
2018-05-01 14:46:17 -07:00
let characters = decode_utf16(utf16.iter().cloned())
.into_iter()
.map(|r| r.unwrap());
s.clear();
s.extend(characters);
}
fn to_usize(n: c_int) -> usize {
if n < 0 {
panic!("Cannot cast {} to usize", n);
}
n as usize
}
fn to_c_int(n: usize) -> c_int {
if n > c_int::max_value() as usize {
panic!("Cannot cast {} to c_int", n);
}
n as c_int
}
2017-08-12 05:03:11 -07:00
pub struct PersonQObject {}
2017-08-29 11:37:51 -07:00
#[derive(Clone)]
2017-08-12 05:03:11 -07:00
pub struct PersonEmitter {
qobject: Arc<Mutex<*const PersonQObject>>,
user_name_changed: fn(*const PersonQObject),
}
unsafe impl Send for PersonEmitter {}
impl PersonEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn user_name_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.user_name_changed)(ptr);
}
}
}
pub trait PersonTrait {
fn new(emit: PersonEmitter) -> Self;
2017-08-12 05:03:11 -07:00
fn emit(&self) -> &PersonEmitter;
2017-08-31 00:01:14 -07:00
fn user_name(&self) -> &str;
2017-08-12 05:03:11 -07:00
fn set_user_name(&mut self, value: String);
}
#[no_mangle]
2017-08-29 11:37:51 -07:00
pub extern "C" fn person_new(
person: *mut PersonQObject,
2018-06-30 11:25:39 -07:00
person_user_name_changed: fn(*const PersonQObject),
2017-08-29 11:37:51 -07:00
) -> *mut Person {
2017-08-26 10:10:18 -07:00
let person_emit = PersonEmitter {
qobject: Arc::new(Mutex::new(person)),
2018-06-30 11:25:39 -07:00
user_name_changed: person_user_name_changed,
2017-08-12 05:03:11 -07:00
};
let d_person = Person::new(person_emit);
2017-08-26 10:10:18 -07:00
Box::into_raw(Box::new(d_person))
2017-08-12 05:03:11 -07:00
}
#[no_mangle]
pub unsafe extern "C" fn person_free(ptr: *mut Person) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
2018-05-01 14:46:17 -07:00
pub extern "C" fn person_user_name_get(
2017-08-29 11:37:51 -07:00
ptr: *const Person,
2018-05-16 00:12:03 -07:00
p: *mut QString,
set: fn(*mut QString, *const c_char, c_int),
2017-08-29 11:37:51 -07:00
) {
2018-05-01 14:46:17 -07:00
let o = unsafe { &*ptr };
let v = o.user_name();
let s: *const c_char = v.as_ptr() as (*const c_char);
set(p, s, to_c_int(v.len()));
2017-08-12 05:03:11 -07:00
}
#[no_mangle]
2018-05-01 14:46:17 -07:00
pub extern "C" fn person_user_name_set(ptr: *mut Person, v: *const c_ushort, len: c_int) {
let o = unsafe { &mut *ptr };
let mut s = String::new();
set_string_from_utf16(&mut s, v, len);
o.set_user_name(s);
2017-08-12 05:03:11 -07:00
}