diff --git a/demo/rust/src/interface.rs b/demo/rust/src/interface.rs index 7a6a3b3..42e360c 100644 --- a/demo/rust/src/interface.rs +++ b/demo/rust/src/interface.rs @@ -1,1214 +1,1215 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; #[repr(C)] pub struct COption { data: T, some: bool, } impl From> for COption where T: Default, { fn from(t: Option) -> COption { if let Some(v) = t { COption { data: v, some: true, } } else { COption { data: T::default(), some: false, } } } } pub enum QString {} 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)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); s.clear(); s.extend(characters); } pub enum QByteArray {} #[repr(C)] +#[derive(PartialEq, Eq, Debug)] pub enum SortOrder { Ascending = 0, Descending = 1, } #[repr(C)] pub struct QModelIndex { row: c_int, internal_id: usize, } 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 } pub struct DemoQObject {} #[derive(Clone)] pub struct DemoEmitter { qobject: Arc>, } unsafe impl Send for DemoEmitter {} impl DemoEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } } pub trait DemoTrait { fn new(emit: DemoEmitter, fibonacci: Fibonacci, fibonacci_list: FibonacciList, file_system_tree: FileSystemTree, processes: Processes, time_series: TimeSeries) -> Self; fn emit(&self) -> &DemoEmitter; fn fibonacci(&self) -> &Fibonacci; fn fibonacci_mut(&mut self) -> &mut Fibonacci; fn fibonacci_list(&self) -> &FibonacciList; fn fibonacci_list_mut(&mut self) -> &mut FibonacciList; fn file_system_tree(&self) -> &FileSystemTree; fn file_system_tree_mut(&mut self) -> &mut FileSystemTree; fn processes(&self) -> &Processes; fn processes_mut(&mut self) -> &mut Processes; fn time_series(&self) -> &TimeSeries; fn time_series_mut(&mut self) -> &mut TimeSeries; } #[no_mangle] pub extern "C" fn demo_new( demo: *mut DemoQObject, fibonacci: *mut FibonacciQObject, - input_changed: fn(*const FibonacciQObject), - result_changed: fn(*const FibonacciQObject), + fibonacci_input_changed: fn(*const FibonacciQObject), + fibonacci_result_changed: fn(*const FibonacciQObject), fibonacci_list: *mut FibonacciListQObject, fibonacci_list_new_data_ready: fn(*const FibonacciListQObject), fibonacci_list_data_changed: fn(*const FibonacciListQObject, usize, usize), fibonacci_list_begin_reset_model: fn(*const FibonacciListQObject), fibonacci_list_end_reset_model: fn(*const FibonacciListQObject), fibonacci_list_begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), fibonacci_list_end_insert_rows: fn(*const FibonacciListQObject), fibonacci_list_begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), fibonacci_list_end_remove_rows: fn(*const FibonacciListQObject), file_system_tree: *mut FileSystemTreeQObject, - path_changed: fn(*const FileSystemTreeQObject), + file_system_tree_path_changed: fn(*const FileSystemTreeQObject), file_system_tree_new_data_ready: fn(*const FileSystemTreeQObject, index: usize, valid: bool), file_system_tree_data_changed: fn(*const FileSystemTreeQObject, usize, usize), file_system_tree_begin_reset_model: fn(*const FileSystemTreeQObject), file_system_tree_end_reset_model: fn(*const FileSystemTreeQObject), file_system_tree_begin_insert_rows: fn(*const FileSystemTreeQObject, index: usize, valid: bool, usize, usize), file_system_tree_end_insert_rows: fn(*const FileSystemTreeQObject), file_system_tree_begin_remove_rows: fn(*const FileSystemTreeQObject, index: usize, valid: bool, usize, usize), file_system_tree_end_remove_rows: fn(*const FileSystemTreeQObject), processes: *mut ProcessesQObject, - active_changed: fn(*const ProcessesQObject), + processes_active_changed: fn(*const ProcessesQObject), processes_new_data_ready: fn(*const ProcessesQObject, index: usize, valid: bool), processes_data_changed: fn(*const ProcessesQObject, usize, usize), processes_begin_reset_model: fn(*const ProcessesQObject), processes_end_reset_model: fn(*const ProcessesQObject), processes_begin_insert_rows: fn(*const ProcessesQObject, index: usize, valid: bool, usize, usize), processes_end_insert_rows: fn(*const ProcessesQObject), processes_begin_remove_rows: fn(*const ProcessesQObject, index: usize, valid: bool, usize, usize), processes_end_remove_rows: fn(*const ProcessesQObject), time_series: *mut TimeSeriesQObject, time_series_new_data_ready: fn(*const TimeSeriesQObject), time_series_data_changed: fn(*const TimeSeriesQObject, usize, usize), time_series_begin_reset_model: fn(*const TimeSeriesQObject), time_series_end_reset_model: fn(*const TimeSeriesQObject), time_series_begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), time_series_end_insert_rows: fn(*const TimeSeriesQObject), time_series_begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), time_series_end_remove_rows: fn(*const TimeSeriesQObject), ) -> *mut Demo { let fibonacci_emit = FibonacciEmitter { qobject: Arc::new(Mutex::new(fibonacci)), - input_changed: input_changed, - result_changed: result_changed, + input_changed: fibonacci_input_changed, + result_changed: fibonacci_result_changed, }; let d_fibonacci = Fibonacci::new(fibonacci_emit); let fibonacci_list_emit = FibonacciListEmitter { qobject: Arc::new(Mutex::new(fibonacci_list)), new_data_ready: fibonacci_list_new_data_ready, }; let model = FibonacciListList { qobject: fibonacci_list, data_changed: fibonacci_list_data_changed, begin_reset_model: fibonacci_list_begin_reset_model, end_reset_model: fibonacci_list_end_reset_model, begin_insert_rows: fibonacci_list_begin_insert_rows, end_insert_rows: fibonacci_list_end_insert_rows, begin_remove_rows: fibonacci_list_begin_remove_rows, end_remove_rows: fibonacci_list_end_remove_rows, }; let d_fibonacci_list = FibonacciList::new(fibonacci_list_emit, model); let file_system_tree_emit = FileSystemTreeEmitter { qobject: Arc::new(Mutex::new(file_system_tree)), - path_changed: path_changed, + path_changed: file_system_tree_path_changed, new_data_ready: file_system_tree_new_data_ready, }; let model = FileSystemTreeTree { qobject: file_system_tree, data_changed: file_system_tree_data_changed, begin_reset_model: file_system_tree_begin_reset_model, end_reset_model: file_system_tree_end_reset_model, begin_insert_rows: file_system_tree_begin_insert_rows, end_insert_rows: file_system_tree_end_insert_rows, begin_remove_rows: file_system_tree_begin_remove_rows, end_remove_rows: file_system_tree_end_remove_rows, }; let d_file_system_tree = FileSystemTree::new(file_system_tree_emit, model); let processes_emit = ProcessesEmitter { qobject: Arc::new(Mutex::new(processes)), - active_changed: active_changed, + active_changed: processes_active_changed, new_data_ready: processes_new_data_ready, }; let model = ProcessesTree { qobject: processes, data_changed: processes_data_changed, begin_reset_model: processes_begin_reset_model, end_reset_model: processes_end_reset_model, begin_insert_rows: processes_begin_insert_rows, end_insert_rows: processes_end_insert_rows, begin_remove_rows: processes_begin_remove_rows, end_remove_rows: processes_end_remove_rows, }; let d_processes = Processes::new(processes_emit, model); let time_series_emit = TimeSeriesEmitter { qobject: Arc::new(Mutex::new(time_series)), new_data_ready: time_series_new_data_ready, }; let model = TimeSeriesList { qobject: time_series, data_changed: time_series_data_changed, begin_reset_model: time_series_begin_reset_model, end_reset_model: time_series_end_reset_model, begin_insert_rows: time_series_begin_insert_rows, end_insert_rows: time_series_end_insert_rows, begin_remove_rows: time_series_begin_remove_rows, end_remove_rows: time_series_end_remove_rows, }; let d_time_series = TimeSeries::new(time_series_emit, model); let demo_emit = DemoEmitter { qobject: Arc::new(Mutex::new(demo)), }; let d_demo = Demo::new(demo_emit, d_fibonacci, d_fibonacci_list, d_file_system_tree, d_processes, d_time_series); Box::into_raw(Box::new(d_demo)) } #[no_mangle] pub unsafe extern "C" fn demo_free(ptr: *mut Demo) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn demo_fibonacci_get(ptr: *mut Demo) -> *mut Fibonacci { (&mut *ptr).fibonacci_mut() } #[no_mangle] pub unsafe extern "C" fn demo_fibonacci_list_get(ptr: *mut Demo) -> *mut FibonacciList { (&mut *ptr).fibonacci_list_mut() } #[no_mangle] pub unsafe extern "C" fn demo_file_system_tree_get(ptr: *mut Demo) -> *mut FileSystemTree { (&mut *ptr).file_system_tree_mut() } #[no_mangle] pub unsafe extern "C" fn demo_processes_get(ptr: *mut Demo) -> *mut Processes { (&mut *ptr).processes_mut() } #[no_mangle] pub unsafe extern "C" fn demo_time_series_get(ptr: *mut Demo) -> *mut TimeSeries { (&mut *ptr).time_series_mut() } pub struct FibonacciQObject {} #[derive(Clone)] pub struct FibonacciEmitter { qobject: Arc>, input_changed: fn(*const FibonacciQObject), result_changed: fn(*const FibonacciQObject), } unsafe impl Send for FibonacciEmitter {} impl FibonacciEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn input_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.input_changed)(ptr); } } pub fn result_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.result_changed)(ptr); } } } pub trait FibonacciTrait { fn new(emit: FibonacciEmitter) -> Self; fn emit(&self) -> &FibonacciEmitter; fn input(&self) -> u32; fn set_input(&mut self, value: u32); fn result(&self) -> u64; } #[no_mangle] pub extern "C" fn fibonacci_new( fibonacci: *mut FibonacciQObject, - input_changed: fn(*const FibonacciQObject), - result_changed: fn(*const FibonacciQObject), + fibonacci_input_changed: fn(*const FibonacciQObject), + fibonacci_result_changed: fn(*const FibonacciQObject), ) -> *mut Fibonacci { let fibonacci_emit = FibonacciEmitter { qobject: Arc::new(Mutex::new(fibonacci)), - input_changed: input_changed, - result_changed: result_changed, + input_changed: fibonacci_input_changed, + result_changed: fibonacci_result_changed, }; let d_fibonacci = Fibonacci::new(fibonacci_emit); Box::into_raw(Box::new(d_fibonacci)) } #[no_mangle] pub unsafe extern "C" fn fibonacci_free(ptr: *mut Fibonacci) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn fibonacci_input_get(ptr: *const Fibonacci) -> u32 { (&*ptr).input() } #[no_mangle] pub unsafe extern "C" fn fibonacci_input_set(ptr: *mut Fibonacci, v: u32) { (&mut *ptr).set_input(v); } #[no_mangle] pub unsafe extern "C" fn fibonacci_result_get(ptr: *const Fibonacci) -> u64 { (&*ptr).result() } pub struct FibonacciListQObject {} #[derive(Clone)] pub struct FibonacciListEmitter { qobject: Arc>, new_data_ready: fn(*const FibonacciListQObject), } unsafe impl Send for FibonacciListEmitter {} impl FibonacciListEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn new_data_ready(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr); } } } pub struct FibonacciListList { qobject: *const FibonacciListQObject, data_changed: fn(*const FibonacciListQObject, usize, usize), begin_reset_model: fn(*const FibonacciListQObject), end_reset_model: fn(*const FibonacciListQObject), begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), end_insert_rows: fn(*const FibonacciListQObject), begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), end_remove_rows: fn(*const FibonacciListQObject), } impl FibonacciListList { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait FibonacciListTrait { fn new(emit: FibonacciListEmitter, model: FibonacciListList) -> Self; fn emit(&self) -> &FibonacciListEmitter; fn row_count(&self) -> usize; fn insert_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn remove_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn can_fetch_more(&self) -> bool { false } fn fetch_more(&mut self) {} fn sort(&mut self, u8, SortOrder) {} fn fibonacci_number(&self, index: usize) -> u64; fn row(&self, index: usize) -> u64; } #[no_mangle] pub extern "C" fn fibonacci_list_new( fibonacci_list: *mut FibonacciListQObject, fibonacci_list_new_data_ready: fn(*const FibonacciListQObject), fibonacci_list_data_changed: fn(*const FibonacciListQObject, usize, usize), fibonacci_list_begin_reset_model: fn(*const FibonacciListQObject), fibonacci_list_end_reset_model: fn(*const FibonacciListQObject), fibonacci_list_begin_insert_rows: fn(*const FibonacciListQObject, usize, usize), fibonacci_list_end_insert_rows: fn(*const FibonacciListQObject), fibonacci_list_begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), fibonacci_list_end_remove_rows: fn(*const FibonacciListQObject), ) -> *mut FibonacciList { let fibonacci_list_emit = FibonacciListEmitter { qobject: Arc::new(Mutex::new(fibonacci_list)), new_data_ready: fibonacci_list_new_data_ready, }; let model = FibonacciListList { qobject: fibonacci_list, data_changed: fibonacci_list_data_changed, begin_reset_model: fibonacci_list_begin_reset_model, end_reset_model: fibonacci_list_end_reset_model, begin_insert_rows: fibonacci_list_begin_insert_rows, end_insert_rows: fibonacci_list_end_insert_rows, begin_remove_rows: fibonacci_list_begin_remove_rows, end_remove_rows: fibonacci_list_end_remove_rows, }; let d_fibonacci_list = FibonacciList::new(fibonacci_list_emit, model); Box::into_raw(Box::new(d_fibonacci_list)) } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_free(ptr: *mut FibonacciList) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_row_count(ptr: *const FibonacciList) -> c_int { to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_insert_rows(ptr: *mut FibonacciList, row: c_int, count: c_int) -> bool { (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_remove_rows(ptr: *mut FibonacciList, row: c_int, count: c_int) -> bool { (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_can_fetch_more(ptr: *const FibonacciList) -> bool { (&*ptr).can_fetch_more() } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_fetch_more(ptr: *mut FibonacciList) { (&mut *ptr).fetch_more() } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_sort( ptr: *mut FibonacciList, column: u8, order: SortOrder, ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub extern "C" fn fibonacci_list_data_fibonacci_number(ptr: *const FibonacciList, row: c_int) -> u64 { let o = unsafe { &*ptr }; o.fibonacci_number(to_usize(row)).into() } #[no_mangle] pub extern "C" fn fibonacci_list_data_row(ptr: *const FibonacciList, row: c_int) -> u64 { let o = unsafe { &*ptr }; o.row(to_usize(row)).into() } pub struct FileSystemTreeQObject {} #[derive(Clone)] pub struct FileSystemTreeEmitter { qobject: Arc>, path_changed: fn(*const FileSystemTreeQObject), new_data_ready: fn(*const FileSystemTreeQObject, index: usize, valid: bool), } unsafe impl Send for FileSystemTreeEmitter {} impl FileSystemTreeEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn path_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.path_changed)(ptr); } } pub fn new_data_ready(&self, item: Option) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr, item.unwrap_or(13), item.is_some()); } } } pub struct FileSystemTreeTree { qobject: *const FileSystemTreeQObject, data_changed: fn(*const FileSystemTreeQObject, usize, usize), begin_reset_model: fn(*const FileSystemTreeQObject), end_reset_model: fn(*const FileSystemTreeQObject), begin_insert_rows: fn(*const FileSystemTreeQObject, index: usize, valid: bool, usize, usize), end_insert_rows: fn(*const FileSystemTreeQObject), begin_remove_rows: fn(*const FileSystemTreeQObject, index: usize, valid: bool, usize, usize), end_remove_rows: fn(*const FileSystemTreeQObject), } impl FileSystemTreeTree { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, index: Option, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, index.unwrap_or(13), index.is_some(), first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, index: Option, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, index.unwrap_or(13), index.is_some(), first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait FileSystemTreeTrait { fn new(emit: FileSystemTreeEmitter, model: FileSystemTreeTree) -> Self; fn emit(&self) -> &FileSystemTreeEmitter; fn path(&self) -> Option<&str>; fn set_path(&mut self, value: Option); fn row_count(&self, Option) -> usize; fn can_fetch_more(&self, Option) -> bool { false } fn fetch_more(&mut self, Option) {} fn sort(&mut self, u8, SortOrder) {} fn index(&self, item: Option, row: usize) -> usize; fn parent(&self, index: usize) -> Option; fn row(&self, index: usize) -> usize; fn file_icon(&self, index: usize) -> &[u8]; fn file_name(&self, index: usize) -> String; fn file_path(&self, index: usize) -> Option; fn file_permissions(&self, index: usize) -> i32; fn file_size(&self, index: usize) -> Option; fn file_type(&self, index: usize) -> i32; } #[no_mangle] pub extern "C" fn file_system_tree_new( file_system_tree: *mut FileSystemTreeQObject, - path_changed: fn(*const FileSystemTreeQObject), + file_system_tree_path_changed: fn(*const FileSystemTreeQObject), file_system_tree_new_data_ready: fn(*const FileSystemTreeQObject, index: usize, valid: bool), file_system_tree_data_changed: fn(*const FileSystemTreeQObject, usize, usize), file_system_tree_begin_reset_model: fn(*const FileSystemTreeQObject), file_system_tree_end_reset_model: fn(*const FileSystemTreeQObject), file_system_tree_begin_insert_rows: fn(*const FileSystemTreeQObject, index: usize, valid: bool, usize, usize), file_system_tree_end_insert_rows: fn(*const FileSystemTreeQObject), file_system_tree_begin_remove_rows: fn(*const FileSystemTreeQObject, index: usize, valid: bool, usize, usize), file_system_tree_end_remove_rows: fn(*const FileSystemTreeQObject), ) -> *mut FileSystemTree { let file_system_tree_emit = FileSystemTreeEmitter { qobject: Arc::new(Mutex::new(file_system_tree)), - path_changed: path_changed, + path_changed: file_system_tree_path_changed, new_data_ready: file_system_tree_new_data_ready, }; let model = FileSystemTreeTree { qobject: file_system_tree, data_changed: file_system_tree_data_changed, begin_reset_model: file_system_tree_begin_reset_model, end_reset_model: file_system_tree_end_reset_model, begin_insert_rows: file_system_tree_begin_insert_rows, end_insert_rows: file_system_tree_end_insert_rows, begin_remove_rows: file_system_tree_begin_remove_rows, end_remove_rows: file_system_tree_end_remove_rows, }; let d_file_system_tree = FileSystemTree::new(file_system_tree_emit, model); Box::into_raw(Box::new(d_file_system_tree)) } #[no_mangle] pub unsafe extern "C" fn file_system_tree_free(ptr: *mut FileSystemTree) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub extern "C" fn file_system_tree_path_get( ptr: *const FileSystemTree, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.path(); if let Some(v) = v { let s: *const c_char = v.as_ptr() as (*const c_char); set(p, s, to_c_int(v.len())); } } #[no_mangle] pub extern "C" fn file_system_tree_path_set(ptr: *mut FileSystemTree, 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_path(Some(s)); } #[no_mangle] pub extern "C" fn file_system_tree_path_set_none(ptr: *mut FileSystemTree) { let o = unsafe { &mut *ptr }; o.set_path(None); } #[no_mangle] pub unsafe extern "C" fn file_system_tree_row_count( ptr: *const FileSystemTree, index: usize, valid: bool, ) -> c_int { to_c_int(if valid { (&*ptr).row_count(Some(index)) } else { (&*ptr).row_count(None) }) } #[no_mangle] pub unsafe extern "C" fn file_system_tree_can_fetch_more( ptr: *const FileSystemTree, index: usize, valid: bool, ) -> bool { if valid { (&*ptr).can_fetch_more(Some(index)) } else { (&*ptr).can_fetch_more(None) } } #[no_mangle] pub unsafe extern "C" fn file_system_tree_fetch_more(ptr: *mut FileSystemTree, index: usize, valid: bool) { if valid { (&mut *ptr).fetch_more(Some(index)) } else { (&mut *ptr).fetch_more(None) } } #[no_mangle] pub unsafe extern "C" fn file_system_tree_sort( ptr: *mut FileSystemTree, column: u8, order: SortOrder ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub unsafe extern "C" fn file_system_tree_index( ptr: *const FileSystemTree, index: usize, valid: bool, row: c_int, ) -> usize { if !valid { (&*ptr).index(None, to_usize(row)) } else { (&*ptr).index(Some(index), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn file_system_tree_parent(ptr: *const FileSystemTree, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { row: to_c_int((&*ptr).row(parent)), internal_id: parent, } } else { QModelIndex { row: -1, internal_id: 0, } } } #[no_mangle] pub unsafe extern "C" fn file_system_tree_row(ptr: *const FileSystemTree, index: usize) -> c_int { to_c_int((&*ptr).row(index)) } #[no_mangle] pub extern "C" fn file_system_tree_data_file_icon( ptr: *const FileSystemTree, index: usize, d: *mut QByteArray, set: fn(*mut QByteArray, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.file_icon(index); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn file_system_tree_data_file_name( ptr: *const FileSystemTree, index: usize, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.file_name(index); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn file_system_tree_data_file_path( ptr: *const FileSystemTree, index: usize, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.file_path(index); if let Some(data) = data { let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } } #[no_mangle] pub extern "C" fn file_system_tree_data_file_permissions(ptr: *const FileSystemTree, index: usize) -> i32 { let o = unsafe { &*ptr }; o.file_permissions(index).into() } #[no_mangle] pub extern "C" fn file_system_tree_data_file_size(ptr: *const FileSystemTree, index: usize) -> COption { let o = unsafe { &*ptr }; o.file_size(index).into() } #[no_mangle] pub extern "C" fn file_system_tree_data_file_type(ptr: *const FileSystemTree, index: usize) -> i32 { let o = unsafe { &*ptr }; o.file_type(index).into() } pub struct ProcessesQObject {} #[derive(Clone)] pub struct ProcessesEmitter { qobject: Arc>, active_changed: fn(*const ProcessesQObject), new_data_ready: fn(*const ProcessesQObject, index: usize, valid: bool), } unsafe impl Send for ProcessesEmitter {} impl ProcessesEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn active_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.active_changed)(ptr); } } pub fn new_data_ready(&self, item: Option) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr, item.unwrap_or(13), item.is_some()); } } } pub struct ProcessesTree { qobject: *const ProcessesQObject, data_changed: fn(*const ProcessesQObject, usize, usize), begin_reset_model: fn(*const ProcessesQObject), end_reset_model: fn(*const ProcessesQObject), begin_insert_rows: fn(*const ProcessesQObject, index: usize, valid: bool, usize, usize), end_insert_rows: fn(*const ProcessesQObject), begin_remove_rows: fn(*const ProcessesQObject, index: usize, valid: bool, usize, usize), end_remove_rows: fn(*const ProcessesQObject), } impl ProcessesTree { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, index: Option, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, index.unwrap_or(13), index.is_some(), first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, index: Option, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, index.unwrap_or(13), index.is_some(), first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait ProcessesTrait { fn new(emit: ProcessesEmitter, model: ProcessesTree) -> Self; fn emit(&self) -> &ProcessesEmitter; fn active(&self) -> bool; fn set_active(&mut self, value: bool); fn row_count(&self, Option) -> usize; fn can_fetch_more(&self, Option) -> bool { false } fn fetch_more(&mut self, Option) {} fn sort(&mut self, u8, SortOrder) {} fn index(&self, item: Option, row: usize) -> usize; fn parent(&self, index: usize) -> Option; fn row(&self, index: usize) -> usize; fn cmd(&self, index: usize) -> String; fn cpu_percentage(&self, index: usize) -> u8; fn cpu_usage(&self, index: usize) -> f32; fn memory(&self, index: usize) -> u64; fn name(&self, index: usize) -> &str; fn pid(&self, index: usize) -> u32; fn uid(&self, index: usize) -> u32; } #[no_mangle] pub extern "C" fn processes_new( processes: *mut ProcessesQObject, - active_changed: fn(*const ProcessesQObject), + processes_active_changed: fn(*const ProcessesQObject), processes_new_data_ready: fn(*const ProcessesQObject, index: usize, valid: bool), processes_data_changed: fn(*const ProcessesQObject, usize, usize), processes_begin_reset_model: fn(*const ProcessesQObject), processes_end_reset_model: fn(*const ProcessesQObject), processes_begin_insert_rows: fn(*const ProcessesQObject, index: usize, valid: bool, usize, usize), processes_end_insert_rows: fn(*const ProcessesQObject), processes_begin_remove_rows: fn(*const ProcessesQObject, index: usize, valid: bool, usize, usize), processes_end_remove_rows: fn(*const ProcessesQObject), ) -> *mut Processes { let processes_emit = ProcessesEmitter { qobject: Arc::new(Mutex::new(processes)), - active_changed: active_changed, + active_changed: processes_active_changed, new_data_ready: processes_new_data_ready, }; let model = ProcessesTree { qobject: processes, data_changed: processes_data_changed, begin_reset_model: processes_begin_reset_model, end_reset_model: processes_end_reset_model, begin_insert_rows: processes_begin_insert_rows, end_insert_rows: processes_end_insert_rows, begin_remove_rows: processes_begin_remove_rows, end_remove_rows: processes_end_remove_rows, }; let d_processes = Processes::new(processes_emit, model); Box::into_raw(Box::new(d_processes)) } #[no_mangle] pub unsafe extern "C" fn processes_free(ptr: *mut Processes) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn processes_active_get(ptr: *const Processes) -> bool { (&*ptr).active() } #[no_mangle] pub unsafe extern "C" fn processes_active_set(ptr: *mut Processes, v: bool) { (&mut *ptr).set_active(v); } #[no_mangle] pub unsafe extern "C" fn processes_row_count( ptr: *const Processes, index: usize, valid: bool, ) -> c_int { to_c_int(if valid { (&*ptr).row_count(Some(index)) } else { (&*ptr).row_count(None) }) } #[no_mangle] pub unsafe extern "C" fn processes_can_fetch_more( ptr: *const Processes, index: usize, valid: bool, ) -> bool { if valid { (&*ptr).can_fetch_more(Some(index)) } else { (&*ptr).can_fetch_more(None) } } #[no_mangle] pub unsafe extern "C" fn processes_fetch_more(ptr: *mut Processes, index: usize, valid: bool) { if valid { (&mut *ptr).fetch_more(Some(index)) } else { (&mut *ptr).fetch_more(None) } } #[no_mangle] pub unsafe extern "C" fn processes_sort( ptr: *mut Processes, column: u8, order: SortOrder ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub unsafe extern "C" fn processes_index( ptr: *const Processes, index: usize, valid: bool, row: c_int, ) -> usize { if !valid { (&*ptr).index(None, to_usize(row)) } else { (&*ptr).index(Some(index), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn processes_parent(ptr: *const Processes, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { row: to_c_int((&*ptr).row(parent)), internal_id: parent, } } else { QModelIndex { row: -1, internal_id: 0, } } } #[no_mangle] pub unsafe extern "C" fn processes_row(ptr: *const Processes, index: usize) -> c_int { to_c_int((&*ptr).row(index)) } #[no_mangle] pub extern "C" fn processes_data_cmd( ptr: *const Processes, index: usize, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.cmd(index); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn processes_data_cpu_percentage(ptr: *const Processes, index: usize) -> u8 { let o = unsafe { &*ptr }; o.cpu_percentage(index).into() } #[no_mangle] pub extern "C" fn processes_data_cpu_usage(ptr: *const Processes, index: usize) -> f32 { let o = unsafe { &*ptr }; o.cpu_usage(index).into() } #[no_mangle] pub extern "C" fn processes_data_memory(ptr: *const Processes, index: usize) -> u64 { let o = unsafe { &*ptr }; o.memory(index).into() } #[no_mangle] pub extern "C" fn processes_data_name( ptr: *const Processes, index: usize, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.name(index); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn processes_data_pid(ptr: *const Processes, index: usize) -> u32 { let o = unsafe { &*ptr }; o.pid(index).into() } #[no_mangle] pub extern "C" fn processes_data_uid(ptr: *const Processes, index: usize) -> u32 { let o = unsafe { &*ptr }; o.uid(index).into() } pub struct TimeSeriesQObject {} #[derive(Clone)] pub struct TimeSeriesEmitter { qobject: Arc>, new_data_ready: fn(*const TimeSeriesQObject), } unsafe impl Send for TimeSeriesEmitter {} impl TimeSeriesEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn new_data_ready(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr); } } } pub struct TimeSeriesList { qobject: *const TimeSeriesQObject, data_changed: fn(*const TimeSeriesQObject, usize, usize), begin_reset_model: fn(*const TimeSeriesQObject), end_reset_model: fn(*const TimeSeriesQObject), begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), end_insert_rows: fn(*const TimeSeriesQObject), begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), end_remove_rows: fn(*const TimeSeriesQObject), } impl TimeSeriesList { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait TimeSeriesTrait { fn new(emit: TimeSeriesEmitter, model: TimeSeriesList) -> Self; fn emit(&self) -> &TimeSeriesEmitter; fn row_count(&self) -> usize; fn insert_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn remove_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn can_fetch_more(&self) -> bool { false } fn fetch_more(&mut self) {} fn sort(&mut self, u8, SortOrder) {} fn cos(&self, index: usize) -> f32; fn set_cos(&mut self, index: usize, f32) -> bool; fn sin(&self, index: usize) -> f32; fn set_sin(&mut self, index: usize, f32) -> bool; fn time(&self, index: usize) -> f32; fn set_time(&mut self, index: usize, f32) -> bool; } #[no_mangle] pub extern "C" fn time_series_new( time_series: *mut TimeSeriesQObject, time_series_new_data_ready: fn(*const TimeSeriesQObject), time_series_data_changed: fn(*const TimeSeriesQObject, usize, usize), time_series_begin_reset_model: fn(*const TimeSeriesQObject), time_series_end_reset_model: fn(*const TimeSeriesQObject), time_series_begin_insert_rows: fn(*const TimeSeriesQObject, usize, usize), time_series_end_insert_rows: fn(*const TimeSeriesQObject), time_series_begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), time_series_end_remove_rows: fn(*const TimeSeriesQObject), ) -> *mut TimeSeries { let time_series_emit = TimeSeriesEmitter { qobject: Arc::new(Mutex::new(time_series)), new_data_ready: time_series_new_data_ready, }; let model = TimeSeriesList { qobject: time_series, data_changed: time_series_data_changed, begin_reset_model: time_series_begin_reset_model, end_reset_model: time_series_end_reset_model, begin_insert_rows: time_series_begin_insert_rows, end_insert_rows: time_series_end_insert_rows, begin_remove_rows: time_series_begin_remove_rows, end_remove_rows: time_series_end_remove_rows, }; let d_time_series = TimeSeries::new(time_series_emit, model); Box::into_raw(Box::new(d_time_series)) } #[no_mangle] pub unsafe extern "C" fn time_series_free(ptr: *mut TimeSeries) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn time_series_row_count(ptr: *const TimeSeries) -> c_int { to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn time_series_insert_rows(ptr: *mut TimeSeries, row: c_int, count: c_int) -> bool { (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn time_series_remove_rows(ptr: *mut TimeSeries, row: c_int, count: c_int) -> bool { (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn time_series_can_fetch_more(ptr: *const TimeSeries) -> bool { (&*ptr).can_fetch_more() } #[no_mangle] pub unsafe extern "C" fn time_series_fetch_more(ptr: *mut TimeSeries) { (&mut *ptr).fetch_more() } #[no_mangle] pub unsafe extern "C" fn time_series_sort( ptr: *mut TimeSeries, column: u8, order: SortOrder, ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub extern "C" fn time_series_data_cos(ptr: *const TimeSeries, row: c_int) -> f32 { let o = unsafe { &*ptr }; o.cos(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn time_series_set_data_cos( ptr: *mut TimeSeries, row: c_int, v: f32, ) -> bool { (&mut *ptr).set_cos(to_usize(row), v) } #[no_mangle] pub extern "C" fn time_series_data_sin(ptr: *const TimeSeries, row: c_int) -> f32 { let o = unsafe { &*ptr }; o.sin(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn time_series_set_data_sin( ptr: *mut TimeSeries, row: c_int, v: f32, ) -> bool { (&mut *ptr).set_sin(to_usize(row), v) } #[no_mangle] pub extern "C" fn time_series_data_time(ptr: *const TimeSeries, row: c_int) -> f32 { let o = unsafe { &*ptr }; o.time(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn time_series_set_data_time( ptr: *mut TimeSeries, row: c_int, v: f32, ) -> bool { (&mut *ptr).set_time(to_usize(row), v) } diff --git a/tests/rust_functions/src/interface.rs b/tests/rust_functions/src/interface.rs index a2aed64..68856da 100644 --- a/tests/rust_functions/src/interface.rs +++ b/tests/rust_functions/src/interface.rs @@ -1,169 +1,169 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; pub enum QString {} 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)) }; 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 } pub struct PersonQObject {} #[derive(Clone)] pub struct PersonEmitter { qobject: Arc>, 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; fn emit(&self) -> &PersonEmitter; fn user_name(&self) -> &str; fn set_user_name(&mut self, value: String); fn append(&mut self, suffix: String, amount: u32) -> (); fn double_name(&mut self) -> (); fn greet(&self, name: String) -> String; fn quote(&self, prefix: String, suffix: String) -> String; fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> String; fn vowels_in_name(&self) -> u8; } #[no_mangle] pub extern "C" fn person_new( person: *mut PersonQObject, - user_name_changed: fn(*const PersonQObject), + person_user_name_changed: fn(*const PersonQObject), ) -> *mut Person { let person_emit = PersonEmitter { qobject: Arc::new(Mutex::new(person)), - user_name_changed: user_name_changed, + user_name_changed: person_user_name_changed, }; let d_person = Person::new(person_emit); Box::into_raw(Box::new(d_person)) } #[no_mangle] pub unsafe extern "C" fn person_free(ptr: *mut Person) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub extern "C" fn person_user_name_get( ptr: *const Person, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { 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())); } #[no_mangle] 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); } #[no_mangle] pub extern "C" fn person_append(ptr: *mut Person, suffix_str: *const c_ushort, suffix_len: c_int, amount: u32) -> () { let mut suffix = String::new(); set_string_from_utf16(&mut suffix, suffix_str, suffix_len); let o = unsafe { &mut *ptr }; let r = o.append(suffix, amount); r } #[no_mangle] pub extern "C" fn person_double_name(ptr: *mut Person) -> () { let o = unsafe { &mut *ptr }; let r = o.double_name(); r } #[no_mangle] pub extern "C" fn person_greet(ptr: *const Person, name_str: *const c_ushort, name_len: c_int, d: *mut QString, set: fn(*mut QString, str: *const c_char, len: c_int)) { let mut name = String::new(); set_string_from_utf16(&mut name, name_str, name_len); let o = unsafe { &*ptr }; let r = o.greet(name); let s: *const c_char = r.as_ptr() as (*const c_char); set(d, s, r.len() as i32); } #[no_mangle] pub 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: fn(*mut QString, str: *const c_char, len: c_int)) { let mut prefix = String::new(); set_string_from_utf16(&mut prefix, prefix_str, prefix_len); let mut suffix = String::new(); set_string_from_utf16(&mut suffix, suffix_str, suffix_len); let o = unsafe { &*ptr }; let r = o.quote(prefix, suffix); let s: *const c_char = r.as_ptr() as (*const c_char); set(d, s, r.len() as i32); } #[no_mangle] pub 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 QString, set: fn(*mut QString, str: *const c_char, len: c_int)) { let prefix = unsafe { slice::from_raw_parts(prefix_str as *const u8, to_usize(prefix_len)) }; let suffix = unsafe { slice::from_raw_parts(suffix_str as *const u8, to_usize(suffix_len)) }; let o = unsafe { &*ptr }; let r = o.quote_bytes(prefix, suffix); let s: *const c_char = r.as_ptr() as (*const c_char); set(d, s, r.len() as i32); } #[no_mangle] pub extern "C" fn person_vowels_in_name(ptr: *const Person) -> u8 { let o = unsafe { &*ptr }; let r = o.vowels_in_name(); r } diff --git a/tests/rust_list/src/interface.rs b/tests/rust_list/src/interface.rs index 2ffc972..7be7363 100644 --- a/tests/rust_list/src/interface.rs +++ b/tests/rust_list/src/interface.rs @@ -1,414 +1,415 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; #[repr(C)] pub struct COption { data: T, some: bool, } impl From> for COption where T: Default, { fn from(t: Option) -> COption { if let Some(v) = t { COption { data: v, some: true, } } else { COption { data: T::default(), some: false, } } } } pub enum QString {} 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)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); s.clear(); s.extend(characters); } #[repr(C)] +#[derive(PartialEq, Eq, Debug)] pub enum SortOrder { Ascending = 0, Descending = 1, } #[repr(C)] pub struct QModelIndex { row: c_int, internal_id: usize, } 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 } pub struct NoRoleQObject {} #[derive(Clone)] pub struct NoRoleEmitter { qobject: Arc>, new_data_ready: fn(*const NoRoleQObject), } unsafe impl Send for NoRoleEmitter {} impl NoRoleEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn new_data_ready(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr); } } } pub struct NoRoleList { qobject: *const NoRoleQObject, data_changed: fn(*const NoRoleQObject, usize, usize), begin_reset_model: fn(*const NoRoleQObject), end_reset_model: fn(*const NoRoleQObject), begin_insert_rows: fn(*const NoRoleQObject, usize, usize), end_insert_rows: fn(*const NoRoleQObject), begin_remove_rows: fn(*const NoRoleQObject, usize, usize), end_remove_rows: fn(*const NoRoleQObject), } impl NoRoleList { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait NoRoleTrait { fn new(emit: NoRoleEmitter, model: NoRoleList) -> Self; fn emit(&self) -> &NoRoleEmitter; fn row_count(&self) -> usize; fn insert_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn remove_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn can_fetch_more(&self) -> bool { false } fn fetch_more(&mut self) {} fn sort(&mut self, u8, SortOrder) {} fn user_age(&self, index: usize) -> u8; fn set_user_age(&mut self, index: usize, u8) -> bool; fn user_name(&self, index: usize) -> &str; fn set_user_name(&mut self, index: usize, String) -> bool; } #[no_mangle] pub extern "C" fn no_role_new( no_role: *mut NoRoleQObject, no_role_new_data_ready: fn(*const NoRoleQObject), no_role_data_changed: fn(*const NoRoleQObject, usize, usize), no_role_begin_reset_model: fn(*const NoRoleQObject), no_role_end_reset_model: fn(*const NoRoleQObject), no_role_begin_insert_rows: fn(*const NoRoleQObject, usize, usize), no_role_end_insert_rows: fn(*const NoRoleQObject), no_role_begin_remove_rows: fn(*const NoRoleQObject, usize, usize), no_role_end_remove_rows: fn(*const NoRoleQObject), ) -> *mut NoRole { let no_role_emit = NoRoleEmitter { qobject: Arc::new(Mutex::new(no_role)), new_data_ready: no_role_new_data_ready, }; let model = NoRoleList { qobject: no_role, data_changed: no_role_data_changed, begin_reset_model: no_role_begin_reset_model, end_reset_model: no_role_end_reset_model, begin_insert_rows: no_role_begin_insert_rows, end_insert_rows: no_role_end_insert_rows, begin_remove_rows: no_role_begin_remove_rows, end_remove_rows: no_role_end_remove_rows, }; let d_no_role = NoRole::new(no_role_emit, model); Box::into_raw(Box::new(d_no_role)) } #[no_mangle] pub unsafe extern "C" fn no_role_free(ptr: *mut NoRole) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn no_role_row_count(ptr: *const NoRole) -> c_int { to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn no_role_insert_rows(ptr: *mut NoRole, row: c_int, count: c_int) -> bool { (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn no_role_remove_rows(ptr: *mut NoRole, row: c_int, count: c_int) -> bool { (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn no_role_can_fetch_more(ptr: *const NoRole) -> bool { (&*ptr).can_fetch_more() } #[no_mangle] pub unsafe extern "C" fn no_role_fetch_more(ptr: *mut NoRole) { (&mut *ptr).fetch_more() } #[no_mangle] pub unsafe extern "C" fn no_role_sort( ptr: *mut NoRole, column: u8, order: SortOrder, ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub extern "C" fn no_role_data_user_age(ptr: *const NoRole, row: c_int) -> u8 { let o = unsafe { &*ptr }; o.user_age(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn no_role_set_data_user_age( ptr: *mut NoRole, row: c_int, v: u8, ) -> bool { (&mut *ptr).set_user_age(to_usize(row), v) } #[no_mangle] pub extern "C" fn no_role_data_user_name( ptr: *const NoRole, row: c_int, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.user_name(to_usize(row)); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn no_role_set_data_user_name( ptr: *mut NoRole, row: c_int, s: *const c_ushort, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); o.set_user_name(to_usize(row), v) } pub struct PersonsQObject {} #[derive(Clone)] pub struct PersonsEmitter { qobject: Arc>, new_data_ready: fn(*const PersonsQObject), } unsafe impl Send for PersonsEmitter {} impl PersonsEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn new_data_ready(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr); } } } pub struct PersonsList { qobject: *const PersonsQObject, data_changed: fn(*const PersonsQObject, usize, usize), begin_reset_model: fn(*const PersonsQObject), end_reset_model: fn(*const PersonsQObject), begin_insert_rows: fn(*const PersonsQObject, usize, usize), end_insert_rows: fn(*const PersonsQObject), begin_remove_rows: fn(*const PersonsQObject, usize, usize), end_remove_rows: fn(*const PersonsQObject), } impl PersonsList { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait PersonsTrait { fn new(emit: PersonsEmitter, model: PersonsList) -> Self; fn emit(&self) -> &PersonsEmitter; fn row_count(&self) -> usize; fn insert_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn remove_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn can_fetch_more(&self) -> bool { false } fn fetch_more(&mut self) {} fn sort(&mut self, u8, SortOrder) {} fn user_name(&self, index: usize) -> &str; fn set_user_name(&mut self, index: usize, String) -> bool; } #[no_mangle] pub extern "C" fn persons_new( persons: *mut PersonsQObject, persons_new_data_ready: fn(*const PersonsQObject), persons_data_changed: fn(*const PersonsQObject, usize, usize), persons_begin_reset_model: fn(*const PersonsQObject), persons_end_reset_model: fn(*const PersonsQObject), persons_begin_insert_rows: fn(*const PersonsQObject, usize, usize), persons_end_insert_rows: fn(*const PersonsQObject), persons_begin_remove_rows: fn(*const PersonsQObject, usize, usize), persons_end_remove_rows: fn(*const PersonsQObject), ) -> *mut Persons { let persons_emit = PersonsEmitter { qobject: Arc::new(Mutex::new(persons)), new_data_ready: persons_new_data_ready, }; let model = PersonsList { qobject: persons, data_changed: persons_data_changed, begin_reset_model: persons_begin_reset_model, end_reset_model: persons_end_reset_model, begin_insert_rows: persons_begin_insert_rows, end_insert_rows: persons_end_insert_rows, begin_remove_rows: persons_begin_remove_rows, end_remove_rows: persons_end_remove_rows, }; let d_persons = Persons::new(persons_emit, model); Box::into_raw(Box::new(d_persons)) } #[no_mangle] pub unsafe extern "C" fn persons_free(ptr: *mut Persons) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn persons_row_count(ptr: *const Persons) -> c_int { to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn persons_insert_rows(ptr: *mut Persons, row: c_int, count: c_int) -> bool { (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn persons_remove_rows(ptr: *mut Persons, row: c_int, count: c_int) -> bool { (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn persons_can_fetch_more(ptr: *const Persons) -> bool { (&*ptr).can_fetch_more() } #[no_mangle] pub unsafe extern "C" fn persons_fetch_more(ptr: *mut Persons) { (&mut *ptr).fetch_more() } #[no_mangle] pub unsafe extern "C" fn persons_sort( ptr: *mut Persons, column: u8, order: SortOrder, ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub extern "C" fn persons_data_user_name( ptr: *const Persons, row: c_int, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.user_name(to_usize(row)); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn persons_set_data_user_name( ptr: *mut Persons, row: c_int, s: *const c_ushort, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); o.set_user_name(to_usize(row), v) } diff --git a/tests/rust_list_types/src/interface.rs b/tests/rust_list_types/src/interface.rs index bcc76fa..428a8df 100644 --- a/tests/rust_list_types/src/interface.rs +++ b/tests/rust_list_types/src/interface.rs @@ -1,526 +1,527 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; #[repr(C)] pub struct COption { data: T, some: bool, } impl From> for COption where T: Default, { fn from(t: Option) -> COption { if let Some(v) = t { COption { data: v, some: true, } } else { COption { data: T::default(), some: false, } } } } pub enum QString {} 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)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); s.clear(); s.extend(characters); } pub enum QByteArray {} #[repr(C)] +#[derive(PartialEq, Eq, Debug)] pub enum SortOrder { Ascending = 0, Descending = 1, } #[repr(C)] pub struct QModelIndex { row: c_int, internal_id: usize, } 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 } pub struct ListQObject {} #[derive(Clone)] pub struct ListEmitter { qobject: Arc>, new_data_ready: fn(*const ListQObject), } unsafe impl Send for ListEmitter {} impl ListEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn new_data_ready(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr); } } } pub struct ListList { qobject: *const ListQObject, data_changed: fn(*const ListQObject, usize, usize), begin_reset_model: fn(*const ListQObject), end_reset_model: fn(*const ListQObject), begin_insert_rows: fn(*const ListQObject, usize, usize), end_insert_rows: fn(*const ListQObject), begin_remove_rows: fn(*const ListQObject, usize, usize), end_remove_rows: fn(*const ListQObject), } impl ListList { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait ListTrait { fn new(emit: ListEmitter, model: ListList) -> Self; fn emit(&self) -> &ListEmitter; fn row_count(&self) -> usize; fn insert_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn remove_rows(&mut self, _row: usize, _count: usize) -> bool { false } fn can_fetch_more(&self) -> bool { false } fn fetch_more(&mut self) {} fn sort(&mut self, u8, SortOrder) {} fn boolean(&self, index: usize) -> bool; fn set_boolean(&mut self, index: usize, bool) -> bool; fn bytearray(&self, index: usize) -> &[u8]; fn set_bytearray(&mut self, index: usize, &[u8]) -> bool; fn f32(&self, index: usize) -> f32; fn set_f32(&mut self, index: usize, f32) -> bool; fn f64(&self, index: usize) -> f64; fn set_f64(&mut self, index: usize, f64) -> bool; fn i16(&self, index: usize) -> i16; fn set_i16(&mut self, index: usize, i16) -> bool; fn i32(&self, index: usize) -> i32; fn set_i32(&mut self, index: usize, i32) -> bool; fn i64(&self, index: usize) -> i64; fn set_i64(&mut self, index: usize, i64) -> bool; fn i8(&self, index: usize) -> i8; fn set_i8(&mut self, index: usize, i8) -> bool; fn optional_boolean(&self, index: usize) -> Option; fn set_optional_boolean(&mut self, index: usize, Option) -> bool; fn optional_bytearray(&self, index: usize) -> Option<&[u8]>; fn set_optional_bytearray(&mut self, index: usize, Option<&[u8]>) -> bool; fn optional_string(&self, index: usize) -> Option<&str>; fn set_optional_string(&mut self, index: usize, Option) -> bool; fn string(&self, index: usize) -> &str; fn set_string(&mut self, index: usize, String) -> bool; fn u16(&self, index: usize) -> u16; fn set_u16(&mut self, index: usize, u16) -> bool; fn u32(&self, index: usize) -> u32; fn set_u32(&mut self, index: usize, u32) -> bool; fn u64(&self, index: usize) -> u64; fn set_u64(&mut self, index: usize, u64) -> bool; fn u8(&self, index: usize) -> u8; fn set_u8(&mut self, index: usize, u8) -> bool; } #[no_mangle] pub extern "C" fn list_new( list: *mut ListQObject, list_new_data_ready: fn(*const ListQObject), list_data_changed: fn(*const ListQObject, usize, usize), list_begin_reset_model: fn(*const ListQObject), list_end_reset_model: fn(*const ListQObject), list_begin_insert_rows: fn(*const ListQObject, usize, usize), list_end_insert_rows: fn(*const ListQObject), list_begin_remove_rows: fn(*const ListQObject, usize, usize), list_end_remove_rows: fn(*const ListQObject), ) -> *mut List { let list_emit = ListEmitter { qobject: Arc::new(Mutex::new(list)), new_data_ready: list_new_data_ready, }; let model = ListList { qobject: list, data_changed: list_data_changed, begin_reset_model: list_begin_reset_model, end_reset_model: list_end_reset_model, begin_insert_rows: list_begin_insert_rows, end_insert_rows: list_end_insert_rows, begin_remove_rows: list_begin_remove_rows, end_remove_rows: list_end_remove_rows, }; let d_list = List::new(list_emit, model); Box::into_raw(Box::new(d_list)) } #[no_mangle] pub unsafe extern "C" fn list_free(ptr: *mut List) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn list_row_count(ptr: *const List) -> c_int { to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn list_insert_rows(ptr: *mut List, row: c_int, count: c_int) -> bool { (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn list_remove_rows(ptr: *mut List, row: c_int, count: c_int) -> bool { (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn list_can_fetch_more(ptr: *const List) -> bool { (&*ptr).can_fetch_more() } #[no_mangle] pub unsafe extern "C" fn list_fetch_more(ptr: *mut List) { (&mut *ptr).fetch_more() } #[no_mangle] pub unsafe extern "C" fn list_sort( ptr: *mut List, column: u8, order: SortOrder, ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub extern "C" fn list_data_boolean(ptr: *const List, row: c_int) -> bool { let o = unsafe { &*ptr }; o.boolean(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_boolean( ptr: *mut List, row: c_int, v: bool, ) -> bool { (&mut *ptr).set_boolean(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_bytearray( ptr: *const List, row: c_int, d: *mut QByteArray, set: fn(*mut QByteArray, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.bytearray(to_usize(row)); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn list_set_data_bytearray( ptr: *mut List, row: c_int, s: *const c_char, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, to_usize(len)) }; o.set_bytearray(to_usize(row), slice) } #[no_mangle] pub extern "C" fn list_data_f32(ptr: *const List, row: c_int) -> f32 { let o = unsafe { &*ptr }; o.f32(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_f32( ptr: *mut List, row: c_int, v: f32, ) -> bool { (&mut *ptr).set_f32(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_f64(ptr: *const List, row: c_int) -> f64 { let o = unsafe { &*ptr }; o.f64(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_f64( ptr: *mut List, row: c_int, v: f64, ) -> bool { (&mut *ptr).set_f64(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i16(ptr: *const List, row: c_int) -> i16 { let o = unsafe { &*ptr }; o.i16(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_i16( ptr: *mut List, row: c_int, v: i16, ) -> bool { (&mut *ptr).set_i16(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i32(ptr: *const List, row: c_int) -> i32 { let o = unsafe { &*ptr }; o.i32(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_i32( ptr: *mut List, row: c_int, v: i32, ) -> bool { (&mut *ptr).set_i32(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i64(ptr: *const List, row: c_int) -> i64 { let o = unsafe { &*ptr }; o.i64(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_i64( ptr: *mut List, row: c_int, v: i64, ) -> bool { (&mut *ptr).set_i64(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i8(ptr: *const List, row: c_int) -> i8 { let o = unsafe { &*ptr }; o.i8(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_i8( ptr: *mut List, row: c_int, v: i8, ) -> bool { (&mut *ptr).set_i8(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_optional_boolean(ptr: *const List, row: c_int) -> COption { let o = unsafe { &*ptr }; o.optional_boolean(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_optional_boolean( ptr: *mut List, row: c_int, v: bool, ) -> bool { (&mut *ptr).set_optional_boolean(to_usize(row), Some(v)) } #[no_mangle] pub unsafe extern "C" fn list_set_data_optional_boolean_none(ptr: *mut List, row: c_int) -> bool { (&mut *ptr).set_optional_boolean(to_usize(row), None) } #[no_mangle] pub extern "C" fn list_data_optional_bytearray( ptr: *const List, row: c_int, d: *mut QByteArray, set: fn(*mut QByteArray, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.optional_bytearray(to_usize(row)); if let Some(data) = data { let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } } #[no_mangle] pub extern "C" fn list_set_data_optional_bytearray( ptr: *mut List, row: c_int, s: *const c_char, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, to_usize(len)) }; o.set_optional_bytearray(to_usize(row), Some(slice)) } #[no_mangle] pub unsafe extern "C" fn list_set_data_optional_bytearray_none(ptr: *mut List, row: c_int) -> bool { (&mut *ptr).set_optional_bytearray(to_usize(row), None) } #[no_mangle] pub extern "C" fn list_data_optional_string( ptr: *const List, row: c_int, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.optional_string(to_usize(row)); if let Some(data) = data { let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } } #[no_mangle] pub extern "C" fn list_set_data_optional_string( ptr: *mut List, row: c_int, s: *const c_ushort, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); o.set_optional_string(to_usize(row), Some(v)) } #[no_mangle] pub unsafe extern "C" fn list_set_data_optional_string_none(ptr: *mut List, row: c_int) -> bool { (&mut *ptr).set_optional_string(to_usize(row), None) } #[no_mangle] pub extern "C" fn list_data_string( ptr: *const List, row: c_int, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.string(to_usize(row)); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn list_set_data_string( ptr: *mut List, row: c_int, s: *const c_ushort, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); o.set_string(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u16(ptr: *const List, row: c_int) -> u16 { let o = unsafe { &*ptr }; o.u16(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_u16( ptr: *mut List, row: c_int, v: u16, ) -> bool { (&mut *ptr).set_u16(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u32(ptr: *const List, row: c_int) -> u32 { let o = unsafe { &*ptr }; o.u32(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_u32( ptr: *mut List, row: c_int, v: u32, ) -> bool { (&mut *ptr).set_u32(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u64(ptr: *const List, row: c_int) -> u64 { let o = unsafe { &*ptr }; o.u64(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_u64( ptr: *mut List, row: c_int, v: u64, ) -> bool { (&mut *ptr).set_u64(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u8(ptr: *const List, row: c_int) -> u8 { let o = unsafe { &*ptr }; o.u8(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn list_set_data_u8( ptr: *mut List, row: c_int, v: u8, ) -> bool { (&mut *ptr).set_u8(to_usize(row), v) } diff --git a/tests/rust_object/src/interface.rs b/tests/rust_object/src/interface.rs index 0dc2d3a..f8fedeb 100644 --- a/tests/rust_object/src/interface.rs +++ b/tests/rust_object/src/interface.rs @@ -1,108 +1,108 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; pub enum QString {} 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)) }; 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 } pub struct PersonQObject {} #[derive(Clone)] pub struct PersonEmitter { qobject: Arc>, 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; fn emit(&self) -> &PersonEmitter; fn user_name(&self) -> &str; fn set_user_name(&mut self, value: String); } #[no_mangle] pub extern "C" fn person_new( person: *mut PersonQObject, - user_name_changed: fn(*const PersonQObject), + person_user_name_changed: fn(*const PersonQObject), ) -> *mut Person { let person_emit = PersonEmitter { qobject: Arc::new(Mutex::new(person)), - user_name_changed: user_name_changed, + user_name_changed: person_user_name_changed, }; let d_person = Person::new(person_emit); Box::into_raw(Box::new(d_person)) } #[no_mangle] pub unsafe extern "C" fn person_free(ptr: *mut Person) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub extern "C" fn person_user_name_get( ptr: *const Person, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { 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())); } #[no_mangle] 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); } diff --git a/tests/rust_object_types/src/interface.rs b/tests/rust_object_types/src/interface.rs index cf3e522..49c65e3 100644 --- a/tests/rust_object_types/src/interface.rs +++ b/tests/rust_object_types/src/interface.rs @@ -1,535 +1,535 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; #[repr(C)] pub struct COption { data: T, some: bool, } impl From> for COption where T: Default, { fn from(t: Option) -> COption { if let Some(v) = t { COption { data: v, some: true, } } else { COption { data: T::default(), some: false, } } } } pub enum QString {} 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)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); s.clear(); s.extend(characters); } pub enum QByteArray {} 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 } pub struct ObjectQObject {} #[derive(Clone)] pub struct ObjectEmitter { qobject: Arc>, boolean_changed: fn(*const ObjectQObject), bytearray_changed: fn(*const ObjectQObject), f32_changed: fn(*const ObjectQObject), f64_changed: fn(*const ObjectQObject), i16_changed: fn(*const ObjectQObject), i32_changed: fn(*const ObjectQObject), i64_changed: fn(*const ObjectQObject), i8_changed: fn(*const ObjectQObject), optional_boolean_changed: fn(*const ObjectQObject), optional_bytearray_changed: fn(*const ObjectQObject), optional_string_changed: fn(*const ObjectQObject), optional_u64_changed: fn(*const ObjectQObject), string_changed: fn(*const ObjectQObject), u16_changed: fn(*const ObjectQObject), u32_changed: fn(*const ObjectQObject), u64_changed: fn(*const ObjectQObject), u8_changed: fn(*const ObjectQObject), } unsafe impl Send for ObjectEmitter {} impl ObjectEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn boolean_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.boolean_changed)(ptr); } } pub fn bytearray_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.bytearray_changed)(ptr); } } pub fn f32_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.f32_changed)(ptr); } } pub fn f64_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.f64_changed)(ptr); } } pub fn i16_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.i16_changed)(ptr); } } pub fn i32_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.i32_changed)(ptr); } } pub fn i64_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.i64_changed)(ptr); } } pub fn i8_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.i8_changed)(ptr); } } pub fn optional_boolean_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.optional_boolean_changed)(ptr); } } pub fn optional_bytearray_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.optional_bytearray_changed)(ptr); } } pub fn optional_string_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.optional_string_changed)(ptr); } } pub fn optional_u64_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.optional_u64_changed)(ptr); } } pub fn string_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.string_changed)(ptr); } } pub fn u16_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.u16_changed)(ptr); } } pub fn u32_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.u32_changed)(ptr); } } pub fn u64_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.u64_changed)(ptr); } } pub fn u8_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.u8_changed)(ptr); } } } pub trait ObjectTrait { fn new(emit: ObjectEmitter) -> Self; fn emit(&self) -> &ObjectEmitter; fn boolean(&self) -> bool; fn set_boolean(&mut self, value: bool); fn bytearray(&self) -> &[u8]; fn set_bytearray(&mut self, value: &[u8]); fn f32(&self) -> f32; fn set_f32(&mut self, value: f32); fn f64(&self) -> f64; fn set_f64(&mut self, value: f64); fn i16(&self) -> i16; fn set_i16(&mut self, value: i16); fn i32(&self) -> i32; fn set_i32(&mut self, value: i32); fn i64(&self) -> i64; fn set_i64(&mut self, value: i64); fn i8(&self) -> i8; fn set_i8(&mut self, value: i8); fn optional_boolean(&self) -> Option; fn set_optional_boolean(&mut self, value: Option); fn optional_bytearray(&self) -> Option<&[u8]>; fn set_optional_bytearray(&mut self, value: Option<&[u8]>); fn optional_string(&self) -> Option<&str>; fn set_optional_string(&mut self, value: Option); fn optional_u64(&self) -> Option; fn set_optional_u64(&mut self, value: Option); fn string(&self) -> &str; fn set_string(&mut self, value: String); fn u16(&self) -> u16; fn set_u16(&mut self, value: u16); fn u32(&self) -> u32; fn set_u32(&mut self, value: u32); fn u64(&self) -> u64; fn set_u64(&mut self, value: u64); fn u8(&self) -> u8; fn set_u8(&mut self, value: u8); } #[no_mangle] pub extern "C" fn object_new( object: *mut ObjectQObject, - boolean_changed: fn(*const ObjectQObject), - bytearray_changed: fn(*const ObjectQObject), - f32_changed: fn(*const ObjectQObject), - f64_changed: fn(*const ObjectQObject), - i16_changed: fn(*const ObjectQObject), - i32_changed: fn(*const ObjectQObject), - i64_changed: fn(*const ObjectQObject), - i8_changed: fn(*const ObjectQObject), - optional_boolean_changed: fn(*const ObjectQObject), - optional_bytearray_changed: fn(*const ObjectQObject), - optional_string_changed: fn(*const ObjectQObject), - optional_u64_changed: fn(*const ObjectQObject), - string_changed: fn(*const ObjectQObject), - u16_changed: fn(*const ObjectQObject), - u32_changed: fn(*const ObjectQObject), - u64_changed: fn(*const ObjectQObject), - u8_changed: fn(*const ObjectQObject), + object_boolean_changed: fn(*const ObjectQObject), + object_bytearray_changed: fn(*const ObjectQObject), + object_f32_changed: fn(*const ObjectQObject), + object_f64_changed: fn(*const ObjectQObject), + object_i16_changed: fn(*const ObjectQObject), + object_i32_changed: fn(*const ObjectQObject), + object_i64_changed: fn(*const ObjectQObject), + object_i8_changed: fn(*const ObjectQObject), + object_optional_boolean_changed: fn(*const ObjectQObject), + object_optional_bytearray_changed: fn(*const ObjectQObject), + object_optional_string_changed: fn(*const ObjectQObject), + object_optional_u64_changed: fn(*const ObjectQObject), + object_string_changed: fn(*const ObjectQObject), + object_u16_changed: fn(*const ObjectQObject), + object_u32_changed: fn(*const ObjectQObject), + object_u64_changed: fn(*const ObjectQObject), + object_u8_changed: fn(*const ObjectQObject), ) -> *mut Object { let object_emit = ObjectEmitter { qobject: Arc::new(Mutex::new(object)), - boolean_changed: boolean_changed, - bytearray_changed: bytearray_changed, - f32_changed: f32_changed, - f64_changed: f64_changed, - i16_changed: i16_changed, - i32_changed: i32_changed, - i64_changed: i64_changed, - i8_changed: i8_changed, - optional_boolean_changed: optional_boolean_changed, - optional_bytearray_changed: optional_bytearray_changed, - optional_string_changed: optional_string_changed, - optional_u64_changed: optional_u64_changed, - string_changed: string_changed, - u16_changed: u16_changed, - u32_changed: u32_changed, - u64_changed: u64_changed, - u8_changed: u8_changed, + boolean_changed: object_boolean_changed, + bytearray_changed: object_bytearray_changed, + f32_changed: object_f32_changed, + f64_changed: object_f64_changed, + i16_changed: object_i16_changed, + i32_changed: object_i32_changed, + i64_changed: object_i64_changed, + i8_changed: object_i8_changed, + optional_boolean_changed: object_optional_boolean_changed, + optional_bytearray_changed: object_optional_bytearray_changed, + optional_string_changed: object_optional_string_changed, + optional_u64_changed: object_optional_u64_changed, + string_changed: object_string_changed, + u16_changed: object_u16_changed, + u32_changed: object_u32_changed, + u64_changed: object_u64_changed, + u8_changed: object_u8_changed, }; let d_object = Object::new(object_emit); Box::into_raw(Box::new(d_object)) } #[no_mangle] pub unsafe extern "C" fn object_free(ptr: *mut Object) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn object_boolean_get(ptr: *const Object) -> bool { (&*ptr).boolean() } #[no_mangle] pub unsafe extern "C" fn object_boolean_set(ptr: *mut Object, v: bool) { (&mut *ptr).set_boolean(v); } #[no_mangle] pub extern "C" fn object_bytearray_get( ptr: *const Object, p: *mut QByteArray, set: fn(*mut QByteArray, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.bytearray(); let s: *const c_char = v.as_ptr() as (*const c_char); set(p, s, to_c_int(v.len())); } #[no_mangle] pub extern "C" fn object_bytearray_set(ptr: *mut Object, v: *const c_char, len: c_int) { let o = unsafe { &mut *ptr }; let v = unsafe { slice::from_raw_parts(v as *const u8, to_usize(len)) }; o.set_bytearray(v); } #[no_mangle] pub unsafe extern "C" fn object_f32_get(ptr: *const Object) -> f32 { (&*ptr).f32() } #[no_mangle] pub unsafe extern "C" fn object_f32_set(ptr: *mut Object, v: f32) { (&mut *ptr).set_f32(v); } #[no_mangle] pub unsafe extern "C" fn object_f64_get(ptr: *const Object) -> f64 { (&*ptr).f64() } #[no_mangle] pub unsafe extern "C" fn object_f64_set(ptr: *mut Object, v: f64) { (&mut *ptr).set_f64(v); } #[no_mangle] pub unsafe extern "C" fn object_i16_get(ptr: *const Object) -> i16 { (&*ptr).i16() } #[no_mangle] pub unsafe extern "C" fn object_i16_set(ptr: *mut Object, v: i16) { (&mut *ptr).set_i16(v); } #[no_mangle] pub unsafe extern "C" fn object_i32_get(ptr: *const Object) -> i32 { (&*ptr).i32() } #[no_mangle] pub unsafe extern "C" fn object_i32_set(ptr: *mut Object, v: i32) { (&mut *ptr).set_i32(v); } #[no_mangle] pub unsafe extern "C" fn object_i64_get(ptr: *const Object) -> i64 { (&*ptr).i64() } #[no_mangle] pub unsafe extern "C" fn object_i64_set(ptr: *mut Object, v: i64) { (&mut *ptr).set_i64(v); } #[no_mangle] pub unsafe extern "C" fn object_i8_get(ptr: *const Object) -> i8 { (&*ptr).i8() } #[no_mangle] pub unsafe extern "C" fn object_i8_set(ptr: *mut Object, v: i8) { (&mut *ptr).set_i8(v); } #[no_mangle] pub unsafe extern "C" fn object_optional_boolean_get(ptr: *const Object) -> COption { match (&*ptr).optional_boolean() { Some(value) => COption { data: value, some: true }, None => COption { data: bool::default(), some: false} } } #[no_mangle] pub unsafe extern "C" fn object_optional_boolean_set(ptr: *mut Object, v: bool) { (&mut *ptr).set_optional_boolean(Some(v)); } #[no_mangle] pub extern "C" fn object_optional_boolean_set_none(ptr: *mut Object) { let o = unsafe { &mut *ptr }; o.set_optional_boolean(None); } #[no_mangle] pub extern "C" fn object_optional_bytearray_get( ptr: *const Object, p: *mut QByteArray, set: fn(*mut QByteArray, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.optional_bytearray(); if let Some(v) = v { let s: *const c_char = v.as_ptr() as (*const c_char); set(p, s, to_c_int(v.len())); } } #[no_mangle] pub extern "C" fn object_optional_bytearray_set(ptr: *mut Object, v: *const c_char, len: c_int) { let o = unsafe { &mut *ptr }; let v = unsafe { slice::from_raw_parts(v as *const u8, to_usize(len)) }; o.set_optional_bytearray(Some(v.into())); } #[no_mangle] pub extern "C" fn object_optional_bytearray_set_none(ptr: *mut Object) { let o = unsafe { &mut *ptr }; o.set_optional_bytearray(None); } #[no_mangle] pub extern "C" fn object_optional_string_get( ptr: *const Object, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.optional_string(); if let Some(v) = v { let s: *const c_char = v.as_ptr() as (*const c_char); set(p, s, to_c_int(v.len())); } } #[no_mangle] pub extern "C" fn object_optional_string_set(ptr: *mut Object, 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_optional_string(Some(s)); } #[no_mangle] pub extern "C" fn object_optional_string_set_none(ptr: *mut Object) { let o = unsafe { &mut *ptr }; o.set_optional_string(None); } #[no_mangle] pub unsafe extern "C" fn object_optional_u64_get(ptr: *const Object) -> COption { match (&*ptr).optional_u64() { Some(value) => COption { data: value, some: true }, None => COption { data: u64::default(), some: false} } } #[no_mangle] pub unsafe extern "C" fn object_optional_u64_set(ptr: *mut Object, v: u64) { (&mut *ptr).set_optional_u64(Some(v)); } #[no_mangle] pub extern "C" fn object_optional_u64_set_none(ptr: *mut Object) { let o = unsafe { &mut *ptr }; o.set_optional_u64(None); } #[no_mangle] pub extern "C" fn object_string_get( ptr: *const Object, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.string(); let s: *const c_char = v.as_ptr() as (*const c_char); set(p, s, to_c_int(v.len())); } #[no_mangle] pub extern "C" fn object_string_set(ptr: *mut Object, 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_string(s); } #[no_mangle] pub unsafe extern "C" fn object_u16_get(ptr: *const Object) -> u16 { (&*ptr).u16() } #[no_mangle] pub unsafe extern "C" fn object_u16_set(ptr: *mut Object, v: u16) { (&mut *ptr).set_u16(v); } #[no_mangle] pub unsafe extern "C" fn object_u32_get(ptr: *const Object) -> u32 { (&*ptr).u32() } #[no_mangle] pub unsafe extern "C" fn object_u32_set(ptr: *mut Object, v: u32) { (&mut *ptr).set_u32(v); } #[no_mangle] pub unsafe extern "C" fn object_u64_get(ptr: *const Object) -> u64 { (&*ptr).u64() } #[no_mangle] pub unsafe extern "C" fn object_u64_set(ptr: *mut Object, v: u64) { (&mut *ptr).set_u64(v); } #[no_mangle] pub unsafe extern "C" fn object_u8_get(ptr: *const Object) -> u8 { (&*ptr).u8() } #[no_mangle] pub unsafe extern "C" fn object_u8_set(ptr: *mut Object, v: u8) { (&mut *ptr).set_u8(v); } diff --git a/tests/rust_objects/src/interface.rs b/tests/rust_objects/src/interface.rs index 371ca53..30cb4c7 100644 --- a/tests/rust_objects/src/interface.rs +++ b/tests/rust_objects/src/interface.rs @@ -1,218 +1,218 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; pub enum QString {} 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)) }; 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 } pub struct GroupQObject {} #[derive(Clone)] pub struct GroupEmitter { qobject: Arc>, } unsafe impl Send for GroupEmitter {} impl GroupEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } } pub trait GroupTrait { fn new(emit: GroupEmitter, person: Person) -> Self; fn emit(&self) -> &GroupEmitter; fn person(&self) -> &Person; fn person_mut(&mut self) -> &mut Person; } #[no_mangle] pub extern "C" fn group_new( group: *mut GroupQObject, person: *mut PersonQObject, object: *mut InnerObjectQObject, - description_changed: fn(*const InnerObjectQObject), + object_description_changed: fn(*const InnerObjectQObject), ) -> *mut Group { let object_emit = InnerObjectEmitter { qobject: Arc::new(Mutex::new(object)), - description_changed: description_changed, + description_changed: object_description_changed, }; let d_object = InnerObject::new(object_emit); let person_emit = PersonEmitter { qobject: Arc::new(Mutex::new(person)), }; let d_person = Person::new(person_emit, d_object); let group_emit = GroupEmitter { qobject: Arc::new(Mutex::new(group)), }; let d_group = Group::new(group_emit, d_person); Box::into_raw(Box::new(d_group)) } #[no_mangle] pub unsafe extern "C" fn group_free(ptr: *mut Group) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn group_person_get(ptr: *mut Group) -> *mut Person { (&mut *ptr).person_mut() } pub struct InnerObjectQObject {} #[derive(Clone)] pub struct InnerObjectEmitter { qobject: Arc>, description_changed: fn(*const InnerObjectQObject), } unsafe impl Send for InnerObjectEmitter {} impl InnerObjectEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn description_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.description_changed)(ptr); } } } pub trait InnerObjectTrait { fn new(emit: InnerObjectEmitter) -> Self; fn emit(&self) -> &InnerObjectEmitter; fn description(&self) -> &str; fn set_description(&mut self, value: String); } #[no_mangle] pub extern "C" fn inner_object_new( inner_object: *mut InnerObjectQObject, - description_changed: fn(*const InnerObjectQObject), + inner_object_description_changed: fn(*const InnerObjectQObject), ) -> *mut InnerObject { let inner_object_emit = InnerObjectEmitter { qobject: Arc::new(Mutex::new(inner_object)), - description_changed: description_changed, + description_changed: inner_object_description_changed, }; let d_inner_object = InnerObject::new(inner_object_emit); Box::into_raw(Box::new(d_inner_object)) } #[no_mangle] pub unsafe extern "C" fn inner_object_free(ptr: *mut InnerObject) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub extern "C" fn inner_object_description_get( ptr: *const InnerObject, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.description(); let s: *const c_char = v.as_ptr() as (*const c_char); set(p, s, to_c_int(v.len())); } #[no_mangle] pub extern "C" fn inner_object_description_set(ptr: *mut InnerObject, 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_description(s); } pub struct PersonQObject {} #[derive(Clone)] pub struct PersonEmitter { qobject: Arc>, } unsafe impl Send for PersonEmitter {} impl PersonEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } } pub trait PersonTrait { fn new(emit: PersonEmitter, object: InnerObject) -> Self; fn emit(&self) -> &PersonEmitter; fn object(&self) -> &InnerObject; fn object_mut(&mut self) -> &mut InnerObject; } #[no_mangle] pub extern "C" fn person_new( person: *mut PersonQObject, object: *mut InnerObjectQObject, - description_changed: fn(*const InnerObjectQObject), + object_description_changed: fn(*const InnerObjectQObject), ) -> *mut Person { let object_emit = InnerObjectEmitter { qobject: Arc::new(Mutex::new(object)), - description_changed: description_changed, + description_changed: object_description_changed, }; let d_object = InnerObject::new(object_emit); let person_emit = PersonEmitter { qobject: Arc::new(Mutex::new(person)), }; let d_person = Person::new(person_emit, d_object); Box::into_raw(Box::new(d_person)) } #[no_mangle] pub unsafe extern "C" fn person_free(ptr: *mut Person) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn person_object_get(ptr: *mut Person) -> *mut InnerObject { (&mut *ptr).object_mut() } diff --git a/tests/rust_tree/src/interface.rs b/tests/rust_tree/src/interface.rs index 1d15dab..7bb133d 100644 --- a/tests/rust_tree/src/interface.rs +++ b/tests/rust_tree/src/interface.rs @@ -1,283 +1,284 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; use std::sync::{Arc, Mutex}; use std::ptr::null; use implementation::*; #[repr(C)] pub struct COption { data: T, some: bool, } impl From> for COption where T: Default, { fn from(t: Option) -> COption { if let Some(v) = t { COption { data: v, some: true, } } else { COption { data: T::default(), some: false, } } } } pub enum QString {} 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)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); s.clear(); s.extend(characters); } #[repr(C)] +#[derive(PartialEq, Eq, Debug)] pub enum SortOrder { Ascending = 0, Descending = 1, } #[repr(C)] pub struct QModelIndex { row: c_int, internal_id: usize, } 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 } pub struct PersonsQObject {} #[derive(Clone)] pub struct PersonsEmitter { qobject: Arc>, new_data_ready: fn(*const PersonsQObject, index: usize, valid: bool), } unsafe impl Send for PersonsEmitter {} impl PersonsEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn new_data_ready(&self, item: Option) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr, item.unwrap_or(13), item.is_some()); } } } pub struct PersonsTree { qobject: *const PersonsQObject, data_changed: fn(*const PersonsQObject, usize, usize), begin_reset_model: fn(*const PersonsQObject), end_reset_model: fn(*const PersonsQObject), begin_insert_rows: fn(*const PersonsQObject, index: usize, valid: bool, usize, usize), end_insert_rows: fn(*const PersonsQObject), begin_remove_rows: fn(*const PersonsQObject, index: usize, valid: bool, usize, usize), end_remove_rows: fn(*const PersonsQObject), } impl PersonsTree { pub fn data_changed(&self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } pub fn begin_reset_model(&self) { (self.begin_reset_model)(self.qobject); } pub fn end_reset_model(&self) { (self.end_reset_model)(self.qobject); } pub fn begin_insert_rows(&self, index: Option, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, index.unwrap_or(13), index.is_some(), first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self, index: Option, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, index.unwrap_or(13), index.is_some(), first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } } pub trait PersonsTrait { fn new(emit: PersonsEmitter, model: PersonsTree) -> Self; fn emit(&self) -> &PersonsEmitter; fn row_count(&self, Option) -> usize; fn can_fetch_more(&self, Option) -> bool { false } fn fetch_more(&mut self, Option) {} fn sort(&mut self, u8, SortOrder) {} fn index(&self, item: Option, row: usize) -> usize; fn parent(&self, index: usize) -> Option; fn row(&self, index: usize) -> usize; fn user_name(&self, index: usize) -> &str; fn set_user_name(&mut self, index: usize, String) -> bool; } #[no_mangle] pub extern "C" fn persons_new( persons: *mut PersonsQObject, persons_new_data_ready: fn(*const PersonsQObject, index: usize, valid: bool), persons_data_changed: fn(*const PersonsQObject, usize, usize), persons_begin_reset_model: fn(*const PersonsQObject), persons_end_reset_model: fn(*const PersonsQObject), persons_begin_insert_rows: fn(*const PersonsQObject, index: usize, valid: bool, usize, usize), persons_end_insert_rows: fn(*const PersonsQObject), persons_begin_remove_rows: fn(*const PersonsQObject, index: usize, valid: bool, usize, usize), persons_end_remove_rows: fn(*const PersonsQObject), ) -> *mut Persons { let persons_emit = PersonsEmitter { qobject: Arc::new(Mutex::new(persons)), new_data_ready: persons_new_data_ready, }; let model = PersonsTree { qobject: persons, data_changed: persons_data_changed, begin_reset_model: persons_begin_reset_model, end_reset_model: persons_end_reset_model, begin_insert_rows: persons_begin_insert_rows, end_insert_rows: persons_end_insert_rows, begin_remove_rows: persons_begin_remove_rows, end_remove_rows: persons_end_remove_rows, }; let d_persons = Persons::new(persons_emit, model); Box::into_raw(Box::new(d_persons)) } #[no_mangle] pub unsafe extern "C" fn persons_free(ptr: *mut Persons) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn persons_row_count( ptr: *const Persons, index: usize, valid: bool, ) -> c_int { to_c_int(if valid { (&*ptr).row_count(Some(index)) } else { (&*ptr).row_count(None) }) } #[no_mangle] pub unsafe extern "C" fn persons_can_fetch_more( ptr: *const Persons, index: usize, valid: bool, ) -> bool { if valid { (&*ptr).can_fetch_more(Some(index)) } else { (&*ptr).can_fetch_more(None) } } #[no_mangle] pub unsafe extern "C" fn persons_fetch_more(ptr: *mut Persons, index: usize, valid: bool) { if valid { (&mut *ptr).fetch_more(Some(index)) } else { (&mut *ptr).fetch_more(None) } } #[no_mangle] pub unsafe extern "C" fn persons_sort( ptr: *mut Persons, column: u8, order: SortOrder ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub unsafe extern "C" fn persons_index( ptr: *const Persons, index: usize, valid: bool, row: c_int, ) -> usize { if !valid { (&*ptr).index(None, to_usize(row)) } else { (&*ptr).index(Some(index), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn persons_parent(ptr: *const Persons, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { row: to_c_int((&*ptr).row(parent)), internal_id: parent, } } else { QModelIndex { row: -1, internal_id: 0, } } } #[no_mangle] pub unsafe extern "C" fn persons_row(ptr: *const Persons, index: usize) -> c_int { to_c_int((&*ptr).row(index)) } #[no_mangle] pub extern "C" fn persons_data_user_name( ptr: *const Persons, index: usize, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.user_name(index); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } #[no_mangle] pub extern "C" fn persons_set_data_user_name( ptr: *mut Persons, index: usize, s: *const c_ushort, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); o.set_user_name(index, v) }