rust-qt-binding-generator/templates/qt_widgets/rust/src/interface.rs

111 lines
2.4 KiB
Rust

/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
use libc::{c_int, c_void, uint8_t, uint16_t};
use std::slice;
use std::sync::{Arc, Mutex};
use std::ptr::null;
use implementation::*;
#[repr(C)]
pub struct QString {
data: *const uint8_t,
len: c_int,
}
#[repr(C)]
pub struct QStringIn {
data: *const uint16_t,
len: c_int,
}
impl QStringIn {
fn convert(&self) -> String {
let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) };
String::from_utf16_lossy(data)
}
}
impl<'a> From<&'a str> for QString {
fn from(string: &'a str) -> QString {
QString {
len: string.len() as c_int,
data: string.as_ptr(),
}
}
}
impl<'a> From<&'a String> for QString {
fn from(string: &'a String) -> QString {
QString {
len: string.len() as c_int,
data: string.as_ptr(),
}
}
}
pub struct SimpleQObject {}
#[derive(Clone)]
pub struct SimpleEmitter {
qobject: Arc<Mutex<*const SimpleQObject>>,
message_changed: fn(*const SimpleQObject),
}
unsafe impl Send for SimpleEmitter {}
impl SimpleEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn message_changed(&self) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.message_changed)(ptr);
}
}
}
pub trait SimpleTrait {
fn new(emit: SimpleEmitter) -> Self;
fn emit(&self) -> &SimpleEmitter;
fn message(&self) -> &str;
fn set_message(&mut self, value: String);
}
#[no_mangle]
pub extern "C" fn simple_new(
simple: *mut SimpleQObject,
message_changed: fn(*const SimpleQObject),
) -> *mut Simple {
let simple_emit = SimpleEmitter {
qobject: Arc::new(Mutex::new(simple)),
message_changed: message_changed,
};
let d_simple = Simple::new(simple_emit);
Box::into_raw(Box::new(d_simple))
}
#[no_mangle]
pub unsafe extern "C" fn simple_free(ptr: *mut Simple) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn simple_message_get(
ptr: *const Simple,
p: *mut c_void,
set: fn(*mut c_void, QString),
) {
let data = (&*ptr).message();
set(p, data.into());
}
#[no_mangle]
pub unsafe extern "C" fn simple_message_set(ptr: *mut Simple, v: QStringIn) {
(&mut *ptr).set_message(v.convert());
}