diff --git a/demo/rust/src/implementation/file_system_tree.rs b/demo/rust/src/implementation/file_system_tree.rs index a2116a4..4c078fd 100644 --- a/demo/rust/src/implementation/file_system_tree.rs +++ b/demo/rust/src/implementation/file_system_tree.rs @@ -1,302 +1,302 @@ // Copyright 2017 Jos van den Oever // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License or (at your option) version 3 or any later version // accepted by the membership of KDE e.V. (or its successor approved // by the membership of KDE e.V.), which shall act as a proxy // defined in Section 14 of version 3 of the license. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . use interface::*; use std::fs::*; use std::fs::read_dir; use std::path::PathBuf; use std::ffi::OsString; use std::default::Default; use std::thread; use std::sync::{Arc, Mutex}; use std::marker::Sync; use std::collections::HashMap; pub struct DirEntry { name: OsString, metadata: Option, path: Option, icon: Vec, } type Incoming = Arc>>>; impl Item for DirEntry { fn new(name: &str) -> DirEntry { DirEntry { name: OsString::from(name), metadata: metadata(name).ok(), path: None, icon: Vec::new() } } fn can_fetch_more(&self) -> bool { self.metadata.as_ref().map_or(false, |m| m.is_dir()) } fn file_name(&self) -> String { self.name.to_string_lossy().to_string() } fn file_path(&self) -> Option { self.path.as_ref().map(|p| p.to_string_lossy().into()) } fn file_permissions(&self) -> i32 { 42 } fn file_type(&self) -> i32 { 0 } fn file_size(&self) -> Option { self.metadata.as_ref().map(|m| m.len()) } fn icon(&self) -> &[u8] { &self.icon } fn retrieve(id: usize, parents: Vec<&DirEntry>, q: Incoming, emit: FileSystemTreeEmitter) { let mut v = Vec::new(); let path: PathBuf = parents.into_iter().map(|e| &e.name).collect(); thread::spawn(move || { if let Ok(it) = read_dir(&path) { for i in it.filter_map(|v| v.ok()) { let de = DirEntry { name: i.file_name(), metadata: i.metadata().ok(), path: Some(i.path()), icon: Vec::new(), }; v.push(de); } } v.sort_by(|a, b| a.name.cmp(&b.name)); let mut map = q.lock().unwrap(); if !map.contains_key(&id) { map.insert(id, v); emit.new_data_ready(Some(id)); } }); } } impl Default for DirEntry { fn default() -> DirEntry { DirEntry { name: OsString::new(), metadata: None, path: None, icon: Vec::new(), } } } pub trait Item: Default { fn new(name: &str) -> Self; fn can_fetch_more(&self) -> bool; fn retrieve(id: usize, parents: Vec<&Self>, q: Incoming, emit: FileSystemTreeEmitter); fn file_name(&self) -> String; fn file_path(&self) -> Option; fn file_permissions(&self) -> i32; fn file_type(&self) -> i32; fn file_size(&self) -> Option; fn icon(&self) -> &[u8]; } pub type FileSystemTree = RGeneralItemModel; struct Entry { parent: Option, row: usize, children: Option>, data: T, } pub struct RGeneralItemModel { emit: FileSystemTreeEmitter, model: FileSystemTreeTree, entries: Vec>, path: Option, incoming: Incoming, } impl RGeneralItemModel where T: Sync + Send, { fn reset(&mut self) { self.model.begin_reset_model(); self.entries.clear(); if let Some(ref path) = self.path { let root = Entry { parent: None, row: 0, children: None, data: T::new(path), }; self.entries.push(root); } self.model.end_reset_model(); } - fn get(&self, item: usize) -> &Entry { - &self.entries[item] + fn get(&self, index: usize) -> &Entry { + &self.entries[index] } - fn retrieve(&mut self, item: usize) { - let parents = self.get_parents(item); + fn retrieve(&mut self, index: usize) { + let parents = self.get_parents(index); let incoming = self.incoming.clone(); - T::retrieve(item, parents, incoming, self.emit.clone()); + T::retrieve(index, parents, incoming, self.emit.clone()); } fn process_incoming(&mut self) { if let Ok(ref mut incoming) = self.incoming.try_lock() { for (id, entries) in incoming.drain() { if self.entries[id].children.is_some() { continue; } let mut new_entries = Vec::new(); let mut children = Vec::new(); { for (r, d) in entries.into_iter().enumerate() { let e = Entry { parent: Some(id), row: r, children: None, data: d, }; children.push(self.entries.len() + r); new_entries.push(e); } if !new_entries.is_empty() { self.model.begin_insert_rows( Some(id), 0, (new_entries.len() - 1), ); } } self.entries[id].children = Some(children); if !new_entries.is_empty() { self.entries.append(&mut new_entries); self.model.end_insert_rows(); } } } } fn get_parents(&self, id: usize) -> Vec<&T> { let mut pos = Some(id); let mut e = Vec::new(); while let Some(p) = pos { e.push(p); pos = self.entries[p].parent; } e.into_iter().rev().map(|i| &self.entries[i].data).collect() } } impl FileSystemTreeTrait for RGeneralItemModel where T: Sync + Send, { fn new(emit: FileSystemTreeEmitter, model: FileSystemTreeTree) -> Self { let mut tree = RGeneralItemModel { emit: emit, model: model, entries: Vec::new(), path: None, incoming: Arc::new(Mutex::new(HashMap::new())), }; tree.reset(); tree } fn emit(&self) -> &FileSystemTreeEmitter { &self.emit } fn path(&self) -> Option<&str> { self.path.as_ref().map(|s|&s[..]) } fn set_path(&mut self, value: Option) { if self.path != value { self.path = value; self.emit.path_changed(); self.reset(); } } - fn can_fetch_more(&self, item: Option) -> bool { - if let Some(item) = item { - let entry = self.get(item); + fn can_fetch_more(&self, index: Option) -> bool { + if let Some(index) = index { + let entry = self.get(index); entry.children.is_none() && entry.data.can_fetch_more() } else { false } } - fn fetch_more(&mut self, item: Option) { + fn fetch_more(&mut self, index: Option) { self.process_incoming(); - if !self.can_fetch_more(item) { + if !self.can_fetch_more(index) { return; } - if let Some(item) = item { - self.retrieve(item); + if let Some(index) = index { + self.retrieve(index); } } - fn row_count(&self, item: Option) -> usize { + fn row_count(&self, index: Option) -> usize { if self.entries.is_empty() { return 0; } - if let Some(i) = item { + if let Some(i) = index { let entry = self.get(i); if let Some(ref children) = entry.children { children.len() } else { // model does lazy loading, signal that data may be available - if self.can_fetch_more(item) { - self.emit.new_data_ready(item); + if self.can_fetch_more(index) { + self.emit.new_data_ready(index); } 0 } } else { 1 } } - fn index(&self, item: Option, row: usize) -> usize { - if let Some(item) = item { - self.get(item).children.as_ref().unwrap()[row] + fn index(&self, index: Option, row: usize) -> usize { + if let Some(index) = index { + self.get(index).children.as_ref().unwrap()[row] } else { 0 } } - fn parent(&self, item: usize) -> Option { - self.entries[item].parent + fn parent(&self, index: usize) -> Option { + self.entries[index].parent } - fn row(&self, item: usize) -> usize { - self.entries[item].row + fn row(&self, index: usize) -> usize { + self.entries[index].row } - fn file_name(&self, item: usize) -> String { - self.get(item).data.file_name() + fn file_name(&self, index: usize) -> String { + self.get(index).data.file_name() } - fn file_permissions(&self, item: usize) -> i32 { - self.get(item).data.file_permissions() + fn file_permissions(&self, index: usize) -> i32 { + self.get(index).data.file_permissions() } #[allow(unused_variables)] - fn file_icon(&self, item: usize) -> &[u8] { - self.get(item).data.icon() + fn file_icon(&self, index: usize) -> &[u8] { + self.get(index).data.icon() } - fn file_path(&self, item: usize) -> Option { - self.get(item).data.file_path() + fn file_path(&self, index: usize) -> Option { + self.get(index).data.file_path() } - fn file_type(&self, item: usize) -> i32 { - self.get(item).data.file_type() + fn file_type(&self, index: usize) -> i32 { + self.get(index).data.file_type() } - fn file_size(&self, item: usize) -> Option { - self.get(item).data.file_size() + fn file_size(&self, index: usize) -> Option { + self.get(index).data.file_size() } } diff --git a/demo/rust/src/implementation/processes.rs b/demo/rust/src/implementation/processes.rs index 7d4a188..f975963 100644 --- a/demo/rust/src/implementation/processes.rs +++ b/demo/rust/src/implementation/processes.rs @@ -1,434 +1,434 @@ // Copyright 2017 Jos van den Oever // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of // the License or (at your option) version 3 or any later version // accepted by the membership of KDE e.V. (or its successor approved // by the membership of KDE e.V.), which shall act as a proxy // defined in Section 14 of version 3 of the license. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . use interface::*; use sysinfo::*; use std::sync::{Arc, Mutex}; use std::collections::HashMap; use libc::pid_t; use std::thread; use std::time::Duration; use std::sync::mpsc::{channel, Sender, Receiver, RecvTimeoutError}; struct ProcessItem { row: usize, tasks: Vec, process: Process, } #[derive(Default)] struct ProcessTree { top: Vec, processes: HashMap, cpusum: f32, } enum ChangeState { Active, Inactive, Quit } pub struct Processes { emit: ProcessesEmitter, model: ProcessesTree, p: ProcessTree, incoming: Arc>>, active: bool, channel: Sender } fn check_process_hierarchy(parent: Option, processes: &HashMap) { for (pid, process) in processes { assert_eq!(process.pid, *pid); if !parent.is_none() { assert_eq!(process.parent, parent); } check_process_hierarchy(Some(*pid), &process.tasks); } } fn collect_processes( tasks: &HashMap, mut processes: &mut HashMap, ) -> f32 { let mut cpusum = 0.0; for process in tasks.values() { processes.insert( process.pid, ProcessItem { row: 0, tasks: Vec::new(), process: process.clone(), }, ); let s = collect_processes(&process.tasks, &mut processes); cpusum += process.cpu_usage + s; } cpusum } // reconstruct process hierarchy fn handle_tasks(processes: &mut HashMap) -> Vec { let mut top = Vec::new(); let pids: Vec = processes.keys().cloned().collect(); for pid in pids { if let Some(parent) = processes[&pid].process.parent { let p = processes.get_mut(&parent).unwrap(); p.tasks.push(pid); } else { top.push(pid); } } top } fn update_rows(list: &[pid_t], processes: &mut HashMap) { for (row, pid) in list.iter().enumerate() { processes.get_mut(pid).unwrap().row = row; let l = processes[pid].tasks.clone(); update_rows(&l, processes); } } fn sort_tasks(p: &mut ProcessTree) { for process in p.processes.values_mut() { process.tasks.sort(); } p.top.sort(); update_rows(&p.top, &mut p.processes); } fn update() -> ProcessTree { let mut p = ProcessTree::default(); let mut sysinfo = System::new(); sysinfo.refresh_processes(); let list = sysinfo.get_process_list(); check_process_hierarchy(None, list); p.cpusum = collect_processes(list, &mut p.processes); p.top = handle_tasks(&mut p.processes); sort_tasks(&mut p); p } fn update_thread( emit: ProcessesEmitter, incoming: Arc>>, mut active: bool, status_channel: Receiver, ) { thread::spawn(move || { loop { let timeout = if active { *incoming.lock().unwrap() = Some(update()); emit.new_data_ready(None); Duration::from_secs(1) } else { Duration::from_secs(10_000) }; match status_channel.recv_timeout(timeout) { Err(RecvTimeoutError::Timeout) => {}, Err(RecvTimeoutError::Disconnected) | Ok(ChangeState::Quit) => { return; }, Ok(ChangeState::Active) => { active = true; }, Ok(ChangeState::Inactive) => { active = false; }, } } }); } impl Processes { - fn get(&self, item: usize) -> &ProcessItem { - let pid = item as pid_t; + fn get(&self, index: usize) -> &ProcessItem { + let pid = index as pid_t; &self.p.processes[&pid] } - fn process(&self, item: usize) -> &Process { - let pid = item as pid_t; + fn process(&self, index: usize) -> &Process { + let pid = index as pid_t; &self.p.processes[&pid].process } } fn move_process( pid: pid_t, amap: &mut HashMap, bmap: &mut HashMap, ) { if let Some(e) = bmap.remove(&pid) { amap.insert(pid, e); let ts = amap[&pid].tasks.clone(); for t in ts { move_process(t, amap, bmap); } } } fn remove_row( model: &ProcessesTree, parent: pid_t, row: usize, map: &mut HashMap, ) { let pid = map[&parent].tasks[row]; println!( "removing {} '{}' {}", pid, map[&pid].process.exe, map[&pid].process.cmd.join(" ") ); model.begin_remove_rows(Some(parent as usize), row, row); map.remove(&pid); let len = { let tasks = &mut map.get_mut(&parent).unwrap().tasks; tasks.remove(row); tasks.len() }; for r in row..len { let pid = map.get_mut(&parent).unwrap().tasks[r]; map.get_mut(&pid).unwrap().row = r; } model.end_remove_rows(); } fn insert_row( model: &ProcessesTree, parent: pid_t, row: usize, map: &mut HashMap, pid: pid_t, source: &mut HashMap, ) { println!( "adding {} '{}' {}", pid, source[&pid].process.exe, source[&pid].process.cmd.join(" ") ); model.begin_insert_rows(Some(parent as usize), row, row); move_process(pid, map, source); let len = { let tasks = &mut map.get_mut(&parent).unwrap().tasks; tasks.insert(row, pid); tasks.len() }; for r in row..len { let pid = map.get_mut(&parent).unwrap().tasks[r]; map.get_mut(&pid).unwrap().row = r; } model.end_insert_rows(); } fn cmp_f32(a: f32, b: f32) -> bool { ((a - b) / a).abs() < 0.01 } fn sync_row(model: &ProcessesTree, pid: pid_t, a: &mut Process, b: &Process) -> f32 { let mut changed = a.name != b.name; if changed { a.name.clone_from(&b.name); } if !cmp_f32(a.cpu_usage, b.cpu_usage) { a.cpu_usage = b.cpu_usage; changed = true; } if a.cmd != b.cmd { a.cmd.clone_from(&b.cmd); changed = true; } if a.exe != b.exe { a.exe.clone_from(&b.exe); changed = true; } if a.memory != b.memory { a.memory = b.memory; changed = true; } if changed { model.data_changed(pid as usize, pid as usize); } b.cpu_usage } fn sync_tree( model: &ProcessesTree, parent: pid_t, amap: &mut HashMap, bmap: &mut HashMap, ) -> f32 { let mut a = 0; let mut b = 0; let mut alen = amap[&parent].tasks.len(); let blen = bmap[&parent].tasks.len(); let mut cpu_total = bmap[&parent].process.cpu_usage; while a < alen && b < blen { let apid = amap[&parent].tasks[a]; let bpid = bmap[&parent].tasks[b]; if apid < bpid { // a process has disappeared remove_row(model, parent, a, amap); alen -= 1; } else if apid > bpid { // a process has appeared insert_row(model, parent, a, amap, bpid, bmap); cpu_total += amap[&bpid].process.cpu_usage; a += 1; alen += 1; b += 1; } else { cpu_total += sync_row( model, apid, &mut amap.get_mut(&apid).unwrap().process, &bmap[&apid].process, ); cpu_total += sync_tree(model, apid, amap, bmap); a += 1; b += 1; } } while a < blen { let bpid = bmap[&parent].tasks[b]; insert_row(model, parent, a, amap, bpid, bmap); a += 1; alen += 1; b += 1; } while b < alen { remove_row(model, parent, a, amap); alen -= 1; } if !cmp_f32(cpu_total, bmap[&parent].process.cpu_usage) { amap.get_mut(&parent).unwrap().process.cpu_usage = cpu_total; model.data_changed(parent as usize, parent as usize); } assert_eq!(a, b); cpu_total } impl ProcessesTrait for Processes { fn new(emit: ProcessesEmitter, model: ProcessesTree) -> Processes { let (tx, rx) = channel(); let p = Processes { emit: emit.clone(), model: model, p: ProcessTree::default(), incoming: Arc::new(Mutex::new(None)), active: false, channel: tx, }; update_thread(emit, p.incoming.clone(), p.active, rx); p } fn emit(&self) -> &ProcessesEmitter { &self.emit } - fn row_count(&self, item: Option) -> usize { - if let Some(item) = item { - self.get(item).tasks.len() + fn row_count(&self, index: Option) -> usize { + if let Some(index) = index { + self.get(index).tasks.len() } else { self.p.top.len() } } - fn index(&self, item: Option, row: usize) -> usize { - if let Some(item) = item { - self.get(item).tasks[row] as usize + fn index(&self, index: Option, row: usize) -> usize { + if let Some(index) = index { + self.get(index).tasks[row] as usize } else { self.p.top[row] as usize } } - fn parent(&self, item: usize) -> Option { - self.get(item).process.parent.map(|pid| pid as usize) + fn parent(&self, index: usize) -> Option { + self.get(index).process.parent.map(|pid| pid as usize) } - fn can_fetch_more(&self, item: Option) -> bool { - if item.is_some() || !self.active { + fn can_fetch_more(&self, index: Option) -> bool { + if index.is_some() || !self.active { return false; } if let Ok(ref incoming) = self.incoming.try_lock() { incoming.is_some() } else { false } } - fn fetch_more(&mut self, item: Option) { - if item.is_some() || !self.active { + fn fetch_more(&mut self, index: Option) { + if index.is_some() || !self.active { return; } let new = if let Ok(ref mut incoming) = self.incoming.try_lock() { incoming.take() } else { None }; if let Some(mut new) = new { // alert! at the top level, only adding is supported! if self.p.top.is_empty() { self.model.begin_reset_model(); self.p = new; self.model.end_reset_model(); } else { let top = self.p.top.clone(); for pid in top { sync_tree(&self.model, pid, &mut self.p.processes, &mut new.processes); } } } } - fn row(&self, item: usize) -> usize { - self.get(item).row + fn row(&self, index: usize) -> usize { + self.get(index).row } - fn pid(&self, item: usize) -> u32 { - self.process(item).pid as u32 + fn pid(&self, index: usize) -> u32 { + self.process(index).pid as u32 } - fn uid(&self, item: usize) -> u32 { - self.process(item).uid as u32 + fn uid(&self, index: usize) -> u32 { + self.process(index).uid as u32 } - fn cpu_usage(&self, item: usize) -> f32 { - self.process(item).cpu_usage + fn cpu_usage(&self, index: usize) -> f32 { + self.process(index).cpu_usage } - fn cpu_percentage(&self, item: usize) -> u8 { - let cpu = self.process(item).cpu_usage / self.p.cpusum; + fn cpu_percentage(&self, index: usize) -> u8 { + let cpu = self.process(index).cpu_usage / self.p.cpusum; (cpu * 100.0) as u8 } - fn memory(&self, item: usize) -> u64 { - self.process(item).memory + fn memory(&self, index: usize) -> u64 { + self.process(index).memory } - fn name(&self, item: usize) -> &str { - &self.process(item).name + fn name(&self, index: usize) -> &str { + &self.process(index).name } - fn cmd(&self, item: usize) -> String { - self.process(item).cmd.join(" ") + fn cmd(&self, index: usize) -> String { + self.process(index).cmd.join(" ") } fn active(&self) -> bool { self.active } fn set_active(&mut self, active: bool) { if self.active != active { self.active = active; if active { self.channel.send(ChangeState::Active) } else { self.channel.send(ChangeState::Inactive) }.expect("Process thread died."); } } } impl Drop for Processes { fn drop(&mut self) { self.channel.send(ChangeState::Quit).expect("Process thread died."); } } diff --git a/demo/rust/src/interface.rs b/demo/rust/src/interface.rs index e411a3d..7a6a3b3 100644 --- a/demo/rust/src/interface.rs +++ b/demo/rust/src/interface.rs @@ -1,1214 +1,1214 @@ /* 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)] 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_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_new_data_ready: fn(*const FileSystemTreeQObject, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + 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, item: usize, valid: bool, usize, usize), + 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_new_data_ready: fn(*const ProcessesQObject, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + 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, item: usize, valid: bool, usize, usize), + 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, }; 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, 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, 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), ) -> *mut Fibonacci { let fibonacci_emit = FibonacciEmitter { qobject: Arc::new(Mutex::new(fibonacci)), input_changed: input_changed, result_changed: 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, item: usize) -> u64; - fn row(&self, item: usize) -> u64; + 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, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + begin_insert_rows: fn(*const FileSystemTreeQObject, index: usize, valid: bool, usize, usize), end_insert_rows: fn(*const FileSystemTreeQObject), - begin_remove_rows: fn(*const FileSystemTreeQObject, item: usize, valid: bool, usize, usize), + 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, item: Option, first: usize, last: usize) { - (self.begin_insert_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + 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, item: Option, first: usize, last: usize) { - (self.begin_remove_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + 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, item: usize) -> Option; - fn row(&self, item: usize) -> usize; - fn file_icon(&self, item: usize) -> &[u8]; - fn file_name(&self, item: usize) -> String; - fn file_path(&self, item: usize) -> Option; - fn file_permissions(&self, item: usize) -> i32; - fn file_size(&self, item: usize) -> Option; - fn file_type(&self, item: usize) -> i32; + 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_new_data_ready: fn(*const FileSystemTreeQObject, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + 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, item: usize, valid: bool, usize, usize), + 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, 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, - item: usize, + index: usize, valid: bool, ) -> c_int { to_c_int(if valid { - (&*ptr).row_count(Some(item)) + (&*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, - item: usize, + index: usize, valid: bool, ) -> bool { if valid { - (&*ptr).can_fetch_more(Some(item)) + (&*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, item: usize, valid: bool) { +pub unsafe extern "C" fn file_system_tree_fetch_more(ptr: *mut FileSystemTree, index: usize, valid: bool) { if valid { - (&mut *ptr).fetch_more(Some(item)) + (&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, - item: usize, + index: usize, valid: bool, row: c_int, ) -> usize { if !valid { (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), to_usize(row)) + (&*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, item: usize) -> c_int { - to_c_int((&*ptr).row(item)) +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, item: usize, + 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(item); + 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, item: usize, + 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(item); + 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, item: usize, + 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(item); + 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, item: usize) -> i32 { +pub extern "C" fn file_system_tree_data_file_permissions(ptr: *const FileSystemTree, index: usize) -> i32 { let o = unsafe { &*ptr }; - o.file_permissions(item).into() + o.file_permissions(index).into() } #[no_mangle] -pub extern "C" fn file_system_tree_data_file_size(ptr: *const FileSystemTree, item: usize) -> COption { +pub extern "C" fn file_system_tree_data_file_size(ptr: *const FileSystemTree, index: usize) -> COption { let o = unsafe { &*ptr }; - o.file_size(item).into() + o.file_size(index).into() } #[no_mangle] -pub extern "C" fn file_system_tree_data_file_type(ptr: *const FileSystemTree, item: usize) -> i32 { +pub extern "C" fn file_system_tree_data_file_type(ptr: *const FileSystemTree, index: usize) -> i32 { let o = unsafe { &*ptr }; - o.file_type(item).into() + 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, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + begin_insert_rows: fn(*const ProcessesQObject, index: usize, valid: bool, usize, usize), end_insert_rows: fn(*const ProcessesQObject), - begin_remove_rows: fn(*const ProcessesQObject, item: usize, valid: bool, usize, usize), + 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, item: Option, first: usize, last: usize) { - (self.begin_insert_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + 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, item: Option, first: usize, last: usize) { - (self.begin_remove_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + 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, item: usize) -> Option; - fn row(&self, item: usize) -> usize; - fn cmd(&self, item: usize) -> String; - fn cpu_percentage(&self, item: usize) -> u8; - fn cpu_usage(&self, item: usize) -> f32; - fn memory(&self, item: usize) -> u64; - fn name(&self, item: usize) -> &str; - fn pid(&self, item: usize) -> u32; - fn uid(&self, item: usize) -> u32; + 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_new_data_ready: fn(*const ProcessesQObject, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + 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, item: usize, valid: bool, usize, usize), + 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, 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, - item: usize, + index: usize, valid: bool, ) -> c_int { to_c_int(if valid { - (&*ptr).row_count(Some(item)) + (&*ptr).row_count(Some(index)) } else { (&*ptr).row_count(None) }) } #[no_mangle] pub unsafe extern "C" fn processes_can_fetch_more( ptr: *const Processes, - item: usize, + index: usize, valid: bool, ) -> bool { if valid { - (&*ptr).can_fetch_more(Some(item)) + (&*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, item: usize, valid: bool) { +pub unsafe extern "C" fn processes_fetch_more(ptr: *mut Processes, index: usize, valid: bool) { if valid { - (&mut *ptr).fetch_more(Some(item)) + (&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, - item: usize, + index: usize, valid: bool, row: c_int, ) -> usize { if !valid { (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), to_usize(row)) + (&*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, item: usize) -> c_int { - to_c_int((&*ptr).row(item)) +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, item: usize, + 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(item); + 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, item: usize) -> u8 { +pub extern "C" fn processes_data_cpu_percentage(ptr: *const Processes, index: usize) -> u8 { let o = unsafe { &*ptr }; - o.cpu_percentage(item).into() + o.cpu_percentage(index).into() } #[no_mangle] -pub extern "C" fn processes_data_cpu_usage(ptr: *const Processes, item: usize) -> f32 { +pub extern "C" fn processes_data_cpu_usage(ptr: *const Processes, index: usize) -> f32 { let o = unsafe { &*ptr }; - o.cpu_usage(item).into() + o.cpu_usage(index).into() } #[no_mangle] -pub extern "C" fn processes_data_memory(ptr: *const Processes, item: usize) -> u64 { +pub extern "C" fn processes_data_memory(ptr: *const Processes, index: usize) -> u64 { let o = unsafe { &*ptr }; - o.memory(item).into() + o.memory(index).into() } #[no_mangle] pub extern "C" fn processes_data_name( - ptr: *const Processes, item: usize, + 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(item); + 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, item: usize) -> u32 { +pub extern "C" fn processes_data_pid(ptr: *const Processes, index: usize) -> u32 { let o = unsafe { &*ptr }; - o.pid(item).into() + o.pid(index).into() } #[no_mangle] -pub extern "C" fn processes_data_uid(ptr: *const Processes, item: usize) -> u32 { +pub extern "C" fn processes_data_uid(ptr: *const Processes, index: usize) -> u32 { let o = unsafe { &*ptr }; - o.uid(item).into() + 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, item: usize) -> f32; - fn set_cos(&mut self, item: usize, f32) -> bool; - fn sin(&self, item: usize) -> f32; - fn set_sin(&mut self, item: usize, f32) -> bool; - fn time(&self, item: usize) -> f32; - fn set_time(&mut self, item: usize, f32) -> bool; + 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/examples/todos/rust/src/implementation.rs b/examples/todos/rust/src/implementation.rs index 0b93095..b300ff0 100644 --- a/examples/todos/rust/src/implementation.rs +++ b/examples/todos/rust/src/implementation.rs @@ -1,127 +1,127 @@ use interface::*; #[derive(Default, Clone)] struct TodosItem { completed: bool, description: String, } pub struct Todos { emit: TodosEmitter, model: TodosList, list: Vec, active_count: usize, } impl Todos { fn update_active_count(&mut self) { let ac = self.list.iter().filter(|i| !i.completed).count(); if self.active_count != ac { self.active_count = ac; self.emit.active_count_changed(); } } } impl TodosTrait for Todos { fn new(emit: TodosEmitter, model: TodosList) -> Todos { Todos { emit: emit, model: model, list: vec![TodosItem::default(); 0], active_count: 0, } } fn emit(&self) -> &TodosEmitter { &self.emit } fn active_count(&self) -> u64 { self.active_count as u64 } fn count(&self) -> u64 { self.list.len() as u64 } fn row_count(&self) -> usize { self.list.len() } - fn completed(&self, item: usize) -> bool { - if item >= self.list.len() { + fn completed(&self, index: usize) -> bool { + if index >= self.list.len() { return false; } - self.list[item].completed + self.list[index].completed } - fn set_completed(&mut self, item: usize, v: bool) -> bool { - if item >= self.list.len() { + fn set_completed(&mut self, index: usize, v: bool) -> bool { + if index >= self.list.len() { return false; } - self.list[item].completed = v; + self.list[index].completed = v; self.update_active_count(); true } - fn description(&self, item: usize) -> &str { - if item < self.list.len() { - &self.list[item].description + fn description(&self, index: usize) -> &str { + if index < self.list.len() { + &self.list[index].description } else { "" } } - fn set_description(&mut self, item: usize, v: String) -> bool { - if item >= self.list.len() { + fn set_description(&mut self, index: usize, v: String) -> bool { + if index >= self.list.len() { return false; } - self.list[item].description = v; + self.list[index].description = v; true } fn insert_rows(&mut self, row: usize, count: usize) -> bool { if count == 0 || row > self.list.len() { return false; } self.model.begin_insert_rows(row, row + count - 1); for i in 0..count { self.list.insert(row + i, TodosItem::default()); } self.model.end_insert_rows(); self.active_count += count; self.emit.active_count_changed(); self.emit.count_changed(); true } fn remove_rows(&mut self, row: usize, count: usize) -> bool { if count == 0 || row + count > self.list.len() { return false; } self.model.begin_remove_rows(row, row + count - 1); self.list.drain(row..row + count); self.model.end_remove_rows(); self.emit.count_changed(); self.update_active_count(); true } fn clear_completed(&mut self) -> () { self.model.begin_reset_model(); self.list.retain(|i| !i.completed); self.model.end_reset_model(); self.emit.count_changed(); } fn add(&mut self, description: String) { let end = self.list.len(); self.model.begin_insert_rows(end, end); self.list.insert(end, TodosItem { completed: false, description }); self.model.end_insert_rows(); self.active_count += 1; self.emit.active_count_changed(); self.emit.count_changed(); self.model.begin_reset_model(); self.model.end_reset_model(); } fn remove(&mut self, index: u64) -> bool { self.remove_rows(index as usize, 1) } fn set_all(&mut self, completed: bool) { for i in &mut self.list { i.completed = completed; } self.model.data_changed(0, self.list.len() - 1); self.update_active_count(); } } diff --git a/examples/todos/rust/src/interface.rs b/examples/todos/rust/src/interface.rs index a5135ad..8de0c1a 100644 --- a/examples/todos/rust/src/interface.rs +++ b/examples/todos/rust/src/interface.rs @@ -1,319 +1,319 @@ /* 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)] 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 TodosQObject {} #[derive(Clone)] pub struct TodosEmitter { qobject: Arc>, active_count_changed: fn(*const TodosQObject), count_changed: fn(*const TodosQObject), new_data_ready: fn(*const TodosQObject), } unsafe impl Send for TodosEmitter {} impl TodosEmitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } pub fn active_count_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.active_count_changed)(ptr); } } pub fn count_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.count_changed)(ptr); } } pub fn new_data_ready(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr); } } } pub struct TodosList { qobject: *const TodosQObject, data_changed: fn(*const TodosQObject, usize, usize), begin_reset_model: fn(*const TodosQObject), end_reset_model: fn(*const TodosQObject), begin_insert_rows: fn(*const TodosQObject, usize, usize), end_insert_rows: fn(*const TodosQObject), begin_remove_rows: fn(*const TodosQObject, usize, usize), end_remove_rows: fn(*const TodosQObject), } impl TodosList { 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 TodosTrait { fn new(emit: TodosEmitter, model: TodosList) -> Self; fn emit(&self) -> &TodosEmitter; fn active_count(&self) -> u64; fn count(&self) -> u64; fn add(&mut self, description: String) -> (); fn clear_completed(&mut self) -> (); fn remove(&mut self, index: u64) -> bool; fn set_all(&mut self, completed: bool) -> (); 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 completed(&self, item: usize) -> bool; - fn set_completed(&mut self, item: usize, bool) -> bool; - fn description(&self, item: usize) -> &str; - fn set_description(&mut self, item: usize, String) -> bool; + fn completed(&self, index: usize) -> bool; + fn set_completed(&mut self, index: usize, bool) -> bool; + fn description(&self, index: usize) -> &str; + fn set_description(&mut self, index: usize, String) -> bool; } #[no_mangle] pub extern "C" fn todos_new( todos: *mut TodosQObject, active_count_changed: fn(*const TodosQObject), count_changed: fn(*const TodosQObject), todos_new_data_ready: fn(*const TodosQObject), todos_data_changed: fn(*const TodosQObject, usize, usize), todos_begin_reset_model: fn(*const TodosQObject), todos_end_reset_model: fn(*const TodosQObject), todos_begin_insert_rows: fn(*const TodosQObject, usize, usize), todos_end_insert_rows: fn(*const TodosQObject), todos_begin_remove_rows: fn(*const TodosQObject, usize, usize), todos_end_remove_rows: fn(*const TodosQObject), ) -> *mut Todos { let todos_emit = TodosEmitter { qobject: Arc::new(Mutex::new(todos)), active_count_changed: active_count_changed, count_changed: count_changed, new_data_ready: todos_new_data_ready, }; let model = TodosList { qobject: todos, data_changed: todos_data_changed, begin_reset_model: todos_begin_reset_model, end_reset_model: todos_end_reset_model, begin_insert_rows: todos_begin_insert_rows, end_insert_rows: todos_end_insert_rows, begin_remove_rows: todos_begin_remove_rows, end_remove_rows: todos_end_remove_rows, }; let d_todos = Todos::new(todos_emit, model); Box::into_raw(Box::new(d_todos)) } #[no_mangle] pub unsafe extern "C" fn todos_free(ptr: *mut Todos) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] pub unsafe extern "C" fn todos_active_count_get(ptr: *const Todos) -> u64 { (&*ptr).active_count() } #[no_mangle] pub unsafe extern "C" fn todos_count_get(ptr: *const Todos) -> u64 { (&*ptr).count() } #[no_mangle] pub extern "C" fn todos_add(ptr: *mut Todos, description_str: *const c_ushort, description_len: c_int) -> () { let mut description = String::new(); set_string_from_utf16(&mut description, description_str, description_len); let o = unsafe { &mut *ptr }; let r = o.add(description); r } #[no_mangle] pub extern "C" fn todos_clear_completed(ptr: *mut Todos) -> () { let o = unsafe { &mut *ptr }; let r = o.clear_completed(); r } #[no_mangle] pub extern "C" fn todos_remove(ptr: *mut Todos, index: u64) -> bool { let o = unsafe { &mut *ptr }; let r = o.remove(index); r } #[no_mangle] pub extern "C" fn todos_set_all(ptr: *mut Todos, completed: bool) -> () { let o = unsafe { &mut *ptr }; let r = o.set_all(completed); r } #[no_mangle] pub unsafe extern "C" fn todos_row_count(ptr: *const Todos) -> c_int { to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn todos_insert_rows(ptr: *mut Todos, row: c_int, count: c_int) -> bool { (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn todos_remove_rows(ptr: *mut Todos, row: c_int, count: c_int) -> bool { (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn todos_can_fetch_more(ptr: *const Todos) -> bool { (&*ptr).can_fetch_more() } #[no_mangle] pub unsafe extern "C" fn todos_fetch_more(ptr: *mut Todos) { (&mut *ptr).fetch_more() } #[no_mangle] pub unsafe extern "C" fn todos_sort( ptr: *mut Todos, column: u8, order: SortOrder, ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub extern "C" fn todos_data_completed(ptr: *const Todos, row: c_int) -> bool { let o = unsafe { &*ptr }; o.completed(to_usize(row)).into() } #[no_mangle] pub unsafe extern "C" fn todos_set_data_completed( ptr: *mut Todos, row: c_int, v: bool, ) -> bool { (&mut *ptr).set_completed(to_usize(row), v) } #[no_mangle] pub extern "C" fn todos_data_description( ptr: *const Todos, row: c_int, d: *mut QString, set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.description(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 todos_set_data_description( ptr: *mut Todos, 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_description(to_usize(row), v) } diff --git a/src/rust.cpp b/src/rust.cpp index 1d47e6e..e9c7e5d 100644 --- a/src/rust.cpp +++ b/src/rust.cpp @@ -1,1054 +1,1054 @@ /* * Copyright 2017 Jos van den Oever * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "structs.h" #include "helper.h" template QString rustType(const T& p) { if (p.optional) { return "Option<" + p.type.rustType + ">"; } return p.type.rustType; } template QString rustReturnType(const T& p) { QString type = p.type.rustType; if (type == "String" && !p.rustByValue) { type = "str"; } if (type == "Vec" && !p.rustByValue) { type = "[u8]"; } if (p.type.isComplex() && !p.rustByValue) { type = "&" + type; } if (p.optional) { return "Option<" + type + ">"; } return type; } template QString rustCType(const T& p) { if (p.optional) { return "COption<" + p.type.rustType + ">"; } return p.type.rustType; } template QString rustTypeInit(const T& p) { if (p.optional) { return "None"; } return p.type.rustTypeInit; } void rConstructorArgsDecl(QTextStream& r, const QString& name, const Object& o, const Configuration& conf) { r << QString(" %2: *mut %1QObject").arg(o.name, snakeCase(name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { r << QString(",\n"); rConstructorArgsDecl(r, p.name, conf.findObject(p.type.name), conf); } else { r << QString(",\n %2_changed: fn(*const %1QObject)") .arg(o.name, snakeCase(p.name)); } } if (o.type == ObjectType::List) { r << QString(",\n %2_new_data_ready: fn(*const %1QObject)") .arg(o.name, snakeCase(o.name)); } else if (o.type == ObjectType::Tree) { - r << QString(",\n %2_new_data_ready: fn(*const %1QObject, item: usize, valid: bool)") + r << QString(",\n %2_new_data_ready: fn(*const %1QObject, index: usize, valid: bool)") .arg(o.name, snakeCase(o.name)); } if (o.type != ObjectType::Object) { QString indexDecl; if (o.type == ObjectType::Tree) { - indexDecl = " item: usize, valid: bool,"; + indexDecl = " index: usize, valid: bool,"; } r << QString(R"(, %3_data_changed: fn(*const %1QObject, usize, usize), %3_begin_reset_model: fn(*const %1QObject), %3_end_reset_model: fn(*const %1QObject), %3_begin_insert_rows: fn(*const %1QObject,%2 usize, usize), %3_end_insert_rows: fn(*const %1QObject), %3_begin_remove_rows: fn(*const %1QObject,%2 usize, usize), %3_end_remove_rows: fn(*const %1QObject))").arg(o.name, indexDecl, snakeCase(o.name)); } } void rConstructorArgs(QTextStream& r, const QString& name, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { rConstructorArgs(r, p.name, conf.findObject(p.type.name), conf); } } r << QString(R"( let %2_emit = %1Emitter { qobject: Arc::new(Mutex::new(%2)), )").arg(o.name, snakeCase(name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) continue; r << QString(" %1_changed: %1_changed,\n").arg(snakeCase(p.name)); } if (o.type != ObjectType::Object) { r << QString(" new_data_ready: %1_new_data_ready,\n") .arg(snakeCase(o.name)); } QString model = ""; if (o.type != ObjectType::Object) { const QString type = o.type == ObjectType::List ? "List" : "Tree"; model = ", model"; r << QString(R"( }; let model = %1%2 { qobject: %3, data_changed: %4_data_changed, begin_reset_model: %4_begin_reset_model, end_reset_model: %4_end_reset_model, begin_insert_rows: %4_begin_insert_rows, end_insert_rows: %4_end_insert_rows, begin_remove_rows: %4_begin_remove_rows, end_remove_rows: %4_end_remove_rows, )").arg(o.name, type, snakeCase(name), snakeCase(o.name)); } r << QString(" };\n let d_%3 = %1::new(%3_emit%2") .arg(o.name, model, snakeCase(name)); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { r << ",\n d_" << snakeCase(p.name); } } r << ");\n"; } void writeFunction(QTextStream& r, const Function& f, const QString& lcname, const Object& o) { const QString lc(snakeCase(f.name)); r << QString(R"( #[no_mangle] pub extern "C" fn %1_%2(ptr: *%3 %4)").arg(lcname, lc, f.mut ? "mut" : "const", o.name); // write all the input arguments, for QString and QByteArray, write // pointers to their content and the length which is int in Qt for (auto a = f.args.begin(); a < f.args.end(); a++) { r << ", "; if (a->type.name == "QString") { r << QString("%1_str: *const c_ushort, %1_len: c_int").arg(a->name); } else if (a->type.name == "QByteArray") { r << QString("%1_str: *const c_char, %1_len: c_int").arg(a->name); } else { r << a->name << ": " << a->type.rustType; } } // If the return type is QString or QByteArray, append a pointer to the // variable that will be set to the argument list. Also add a setter // function. if (f.type.isComplex()) { r << QString(", d: *mut %1, set: fn(*mut %1, str: *const c_char, len: c_int)) {\n").arg(f.type.name); } else { r << ") -> " << f.type.rustType << " {\n"; } for (auto a = f.args.begin(); a < f.args.end(); a++) { if (a->type.name == "QString") { r << " let mut " << a->name << " = String::new();\n"; r << QString(" set_string_from_utf16(&mut %1, %1_str, %1_len);\n").arg(a->name); } else if (a->type.name == "QByteArray") { r << QString(" let %1 = unsafe { slice::from_raw_parts(%1_str as *const u8, to_usize(%1_len)) };\n").arg(a->name); } } if (f.mut) { r << " let o = unsafe { &mut *ptr };\n"; } else { r << " let o = unsafe { &*ptr };\n"; } r << " let r = o." << lc << "("; for (auto a = f.args.begin(); a < f.args.end(); a++) { if (a != f.args.begin()) { r << ", "; } r << a->name; } r << ");\n"; if (f.type.isComplex()) { r << " let s: *const c_char = r.as_ptr() as (*const c_char);\n"; r << " set(d, s, r.len() as i32);\n"; } else { r << " r\n"; } r << "}\n"; } void writeRustInterfaceObject(QTextStream& r, const Object& o, const Configuration& conf) { const QString lcname(snakeCase(o.name)); r << QString(R"( pub struct %1QObject {} #[derive(Clone)] pub struct %1Emitter { qobject: Arc>, )").arg(o.name); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { continue; } r << QString(" %2_changed: fn(*const %1QObject),\n") .arg(o.name, snakeCase(p.name)); } if (o.type == ObjectType::List) { r << QString(" new_data_ready: fn(*const %1QObject),\n") .arg(o.name); } else if (o.type == ObjectType::Tree) { - r << QString(" new_data_ready: fn(*const %1QObject, item: usize, valid: bool),\n") + r << QString(" new_data_ready: fn(*const %1QObject, index: usize, valid: bool),\n") .arg(o.name); } r << QString(R"(} unsafe impl Send for %1Emitter {} impl %1Emitter { fn clear(&self) { *self.qobject.lock().unwrap() = null(); } )").arg(o.name); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { continue; } r << QString(R"( pub fn %1_changed(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.%1_changed)(ptr); } } )").arg(snakeCase(p.name)); } if (o.type == ObjectType::List) { r << R"( pub fn new_data_ready(&self) { let ptr = *self.qobject.lock().unwrap(); if !ptr.is_null() { (self.new_data_ready)(ptr); } } )"; } else if (o.type == ObjectType::Tree) { r << R"( 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()); } } )"; } QString modelStruct = ""; if (o.type != ObjectType::Object) { QString type = o.type == ObjectType::List ? "List" : "Tree"; modelStruct = ", model: " + o.name + type; QString index; QString indexDecl; QString indexCDecl; if (o.type == ObjectType::Tree) { - indexDecl = " item: Option,"; - indexCDecl = " item: usize, valid: bool,"; - index = " item.unwrap_or(13), item.is_some(),"; + indexDecl = " index: Option,"; + indexCDecl = " index: usize, valid: bool,"; + index = " index.unwrap_or(13), index.is_some(),"; } r << QString(R"(} pub struct %1%2 { qobject: *const %1QObject, data_changed: fn(*const %1QObject, usize, usize), begin_reset_model: fn(*const %1QObject), end_reset_model: fn(*const %1QObject), begin_insert_rows: fn(*const %1QObject,%5 usize, usize), end_insert_rows: fn(*const %1QObject), begin_remove_rows: fn(*const %1QObject,%5 usize, usize), end_remove_rows: fn(*const %1QObject), } impl %1%2 { 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,%3 first: usize, last: usize) { (self.begin_insert_rows)(self.qobject,%4 first, last); } pub fn end_insert_rows(&self) { (self.end_insert_rows)(self.qobject); } pub fn begin_remove_rows(&self,%3 first: usize, last: usize) { (self.begin_remove_rows)(self.qobject,%4 first, last); } pub fn end_remove_rows(&self) { (self.end_remove_rows)(self.qobject); } )").arg(o.name, type, indexDecl, index, indexCDecl); } r << QString(R"(} pub trait %1Trait { fn new(emit: %1Emitter%2)").arg(o.name, modelStruct); for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { r << ",\n " << snakeCase(p.name) << ": " << p.type.name; } } r << QString(R"() -> Self; fn emit(&self) -> &%1Emitter; )").arg(o.name); for (const Property& p: o.properties) { const QString lc(snakeCase(p.name)); if (p.type.type == BindingType::Object) { r << QString(" fn %1(&self) -> &%2;\n").arg(lc, rustType(p)); r << QString(" fn %1_mut(&mut self) -> &mut %2;\n").arg(lc, rustType(p)); } else { r << QString(" fn %1(&self) -> %2;\n").arg(lc, rustReturnType(p)); if (p.write) { if (p.type.name == "QByteArray") { if (p.optional) { r << QString(" fn set_%1(&mut self, value: Option<&[u8]>);\n").arg(lc); } else { r << QString(" fn set_%1(&mut self, value: &[u8]);\n").arg(lc); } } else { r << QString(" fn set_%1(&mut self, value: %2);\n").arg(lc, rustType(p)); } } } } for (const Function& f: o.functions) { const QString lc(snakeCase(f.name)); QString argList; if (f.args.size() > 0) { for (auto a = f.args.begin(); a < f.args.end(); a++) { auto t = a->type.name == "QByteArray" ?"&[u8]" :a->type.rustType; argList.append(QString(", %1: %2").arg(a->name, t)); } } r << QString(" fn %1(&%2self%4) -> %3;\n") .arg(lc, f.mut ? "mut " : "", f.type.rustType, argList); } if (o.type == ObjectType::List) { r << R"( 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) {} )"; } else if (o.type == ObjectType::Tree) { r << R"( 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, item: usize) -> Option; - fn row(&self, item: usize) -> usize; + fn parent(&self, index: usize) -> Option; + fn row(&self, index: usize) -> usize; )"; } if (o.type != ObjectType::Object) { for (auto ip: o.itemProperties) { - r << QString(" fn %1(&self, item: usize) -> %2;\n") + r << QString(" fn %1(&self, index: usize) -> %2;\n") .arg(snakeCase(ip.name), rustReturnType(ip)); if (ip.write) { if (ip.type.name == "QByteArray") { if (ip.optional) { - r << QString(" fn set_%1(&mut self, item: usize, Option<&[u8]>) -> bool;\n") + r << QString(" fn set_%1(&mut self, index: usize, Option<&[u8]>) -> bool;\n") .arg(snakeCase(ip.name)); } else { - r << QString(" fn set_%1(&mut self, item: usize, &[u8]) -> bool;\n") + r << QString(" fn set_%1(&mut self, index: usize, &[u8]) -> bool;\n") .arg(snakeCase(ip.name)); } } else { - r << QString(" fn set_%1(&mut self, item: usize, %2) -> bool;\n") + r << QString(" fn set_%1(&mut self, index: usize, %2) -> bool;\n") .arg(snakeCase(ip.name), rustType(ip)); } } } } r << QString(R"(} #[no_mangle] pub extern "C" fn %1_new( )").arg(lcname); rConstructorArgsDecl(r, lcname, o, conf); r << QString(",\n) -> *mut %1 {\n").arg(o.name); rConstructorArgs(r, lcname, o, conf); r << QString(R"( Box::into_raw(Box::new(d_%2)) } #[no_mangle] pub unsafe extern "C" fn %2_free(ptr: *mut %1) { Box::from_raw(ptr).emit().clear(); } )").arg(o.name, lcname); for (const Property& p: o.properties) { const QString base = QString("%1_%2").arg(lcname, snakeCase(p.name)); QString ret = ") -> " + rustType(p); if (p.type.type == BindingType::Object) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_get(ptr: *mut %1) -> *mut %4 { (&mut *ptr).%3_mut() } )").arg(o.name, base, snakeCase(p.name), rustType(p)); } else if (p.type.isComplex() && !p.optional) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_get( ptr: *const %1, p: *mut %4, set: fn(*mut %4, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.%3(); let s: *const c_char = v.as_ptr() as (*const c_char); set(p, s, to_c_int(v.len())); } )").arg(o.name, base, snakeCase(p.name), p.type.name); if (p.write && p.type.name == "QString") { r << QString(R"( #[no_mangle] pub extern "C" fn %2_set(ptr: *mut %1, 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_%3(s); } )").arg(o.name, base, snakeCase(p.name)); } else if (p.write) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_set(ptr: *mut %1, 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_%3(v); } )").arg(o.name, base, snakeCase(p.name)); } } else if (p.type.isComplex()) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_get( ptr: *const %1, p: *mut %4, set: fn(*mut %4, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.%3(); 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())); } } )").arg(o.name, base, snakeCase(p.name), p.type.name); if (p.write && p.type.name == "QString") { r << QString(R"( #[no_mangle] pub extern "C" fn %2_set(ptr: *mut %1, 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_%3(Some(s)); } )").arg(o.name, base, snakeCase(p.name)); } else if (p.write) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_set(ptr: *mut %1, 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_%3(Some(v.into())); } )").arg(o.name, base, snakeCase(p.name)); } } else if (p.optional) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_get(ptr: *const %1) -> COption<%4> { match (&*ptr).%3() { Some(value) => COption { data: value, some: true }, None => COption { data: %4::default(), some: false} } } )").arg(o.name, base, snakeCase(p.name), p.type.rustType); if (p.write) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_set(ptr: *mut %1, v: %4) { (&mut *ptr).set_%3(Some(v)); } )").arg(o.name, base, snakeCase(p.name), p.type.rustType); } } else { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_get(ptr: *const %1) -> %4 { (&*ptr).%3() } )").arg(o.name, base, snakeCase(p.name), rustType(p)); if (p.write) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_set(ptr: *mut %1, v: %4) { (&mut *ptr).set_%3(v); } )").arg(o.name, base, snakeCase(p.name), rustType(p)); } } if (p.write && p.optional) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_set_none(ptr: *mut %1) { let o = unsafe { &mut *ptr }; o.set_%3(None); } )").arg(o.name, base, snakeCase(p.name)); } } for (const Function& f: o.functions) { writeFunction(r, f, lcname, o); } if (o.type == ObjectType::List) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_row_count(ptr: *const %1) -> c_int { to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn %2_insert_rows(ptr: *mut %1, row: c_int, count: c_int) -> bool { (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn %2_remove_rows(ptr: *mut %1, row: c_int, count: c_int) -> bool { (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn %2_can_fetch_more(ptr: *const %1) -> bool { (&*ptr).can_fetch_more() } #[no_mangle] pub unsafe extern "C" fn %2_fetch_more(ptr: *mut %1) { (&mut *ptr).fetch_more() } #[no_mangle] pub unsafe extern "C" fn %2_sort( ptr: *mut %1, column: u8, order: SortOrder, ) { (&mut *ptr).sort(column, order) } )").arg(o.name, lcname); } else if (o.type == ObjectType::Tree) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_row_count( ptr: *const %1, - item: usize, + index: usize, valid: bool, ) -> c_int { to_c_int(if valid { - (&*ptr).row_count(Some(item)) + (&*ptr).row_count(Some(index)) } else { (&*ptr).row_count(None) }) } #[no_mangle] pub unsafe extern "C" fn %2_can_fetch_more( ptr: *const %1, - item: usize, + index: usize, valid: bool, ) -> bool { if valid { - (&*ptr).can_fetch_more(Some(item)) + (&*ptr).can_fetch_more(Some(index)) } else { (&*ptr).can_fetch_more(None) } } #[no_mangle] -pub unsafe extern "C" fn %2_fetch_more(ptr: *mut %1, item: usize, valid: bool) { +pub unsafe extern "C" fn %2_fetch_more(ptr: *mut %1, index: usize, valid: bool) { if valid { - (&mut *ptr).fetch_more(Some(item)) + (&mut *ptr).fetch_more(Some(index)) } else { (&mut *ptr).fetch_more(None) } } #[no_mangle] pub unsafe extern "C" fn %2_sort( ptr: *mut %1, column: u8, order: SortOrder ) { (&mut *ptr).sort(column, order) } #[no_mangle] pub unsafe extern "C" fn %2_index( ptr: *const %1, - item: usize, + index: usize, valid: bool, row: c_int, ) -> usize { if !valid { (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), to_usize(row)) + (&*ptr).index(Some(index), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn %2_parent(ptr: *const %1, 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 %2_row(ptr: *const %1, item: usize) -> c_int { - to_c_int((&*ptr).row(item)) +pub unsafe extern "C" fn %2_row(ptr: *const %1, index: usize) -> c_int { + to_c_int((&*ptr).row(index)) } )").arg(o.name, lcname); } if (o.type != ObjectType::Object) { QString indexDecl = ", row: c_int"; QString index = "to_usize(row)"; if (o.type == ObjectType::Tree) { - indexDecl = ", item: usize"; - index = "item"; + indexDecl = ", index: usize"; + index = "index"; } for (auto ip: o.itemProperties) { if (ip.type.isComplex() && !ip.optional) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_data_%3( ptr: *const %1%4, d: *mut %6, set: fn(*mut %6, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.%3(%5); let s: *const c_char = data.as_ptr() as (*const c_char); set(d, s, to_c_int(data.len())); } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, ip.type.name); } else if (ip.type.isComplex()) { r << QString(R"( #[no_mangle] pub extern "C" fn %2_data_%3( ptr: *const %1%4, d: *mut %6, set: fn(*mut %6, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; let data = o.%3(%5); 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())); } } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, ip.type.name); } else { r << QString(R"( #[no_mangle] pub extern "C" fn %2_data_%3(ptr: *const %1%5) -> %4 { let o = unsafe { &*ptr }; o.%3(%6).into() } )").arg(o.name, lcname, snakeCase(ip.name), rustCType(ip), indexDecl, index); } if (ip.write) { QString val = "v"; if (ip.optional) { val = "Some(" + val + ")"; } if (ip.type.name == "QString") { r << QString(R"( #[no_mangle] pub extern "C" fn %2_set_data_%3( ptr: *mut %1%4, 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_%3(%5, %6) } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, val); } else if (ip.type.name == "QByteArray") { r << QString(R"( #[no_mangle] pub extern "C" fn %2_set_data_%3( ptr: *mut %1%4, 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_%3(%5, %6) } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, ip.optional ?"Some(slice)" :"slice"); } else { const QString type = ip.type.rustType; r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_set_data_%3( ptr: *mut %1%4, v: %6, ) -> bool { (&mut *ptr).set_%3(%5, %7) } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, type, val); } } if (ip.write && ip.optional) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_set_data_%3_none(ptr: *mut %1%4) -> bool { (&mut *ptr).set_%3(%5, None) } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index); } } } } QString rustFile(const QDir rustdir, const QString& module) { QDir src(rustdir.absoluteFilePath("src")); QString modulePath = src.absoluteFilePath(module + "/mod.rs"); if (QFile::exists(modulePath)) { return modulePath; } return src.absoluteFilePath(module + ".rs"); } void writeRustTypes(const Configuration& conf, QTextStream& r) { bool hasOption = false; bool hasString = false; bool hasStringWrite = false; bool hasByteArray = false; bool hasListOrTree = false; for (auto o: conf.objects) { hasListOrTree |= o.type != ObjectType::Object; for (auto p: o.properties) { hasOption |= p.optional; hasString |= p.type.type == BindingType::QString; hasStringWrite |= p.type.type == BindingType::QString && p.write; hasByteArray |= p.type.type == BindingType::QByteArray; } for (auto p: o.itemProperties) { hasOption |= p.optional; hasString |= p.type.type == BindingType::QString; hasByteArray |= p.type.type == BindingType::QByteArray; } } if (hasOption || hasListOrTree) { r << R"( #[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, } } } } )"; } if (hasString) { r << R"( 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); } )"; } if (hasByteArray) { r << R"( pub enum QByteArray {} )"; } if (hasListOrTree) { r << R"( #[repr(C)] pub enum SortOrder { Ascending = 0, Descending = 1, } #[repr(C)] pub struct QModelIndex { row: c_int, internal_id: usize, } )"; } if (hasString || hasByteArray || hasListOrTree) { r << R"( fn to_usize(n: c_int) -> usize { if n < 0 { panic!("Cannot cast {} to usize", n); } n as usize } )"; } if (hasString || hasByteArray || hasListOrTree) { r << R"( 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 } )"; } } void writeRustInterface(const Configuration& conf) { DifferentFileWriter w(rustFile(conf.rustdir, conf.interfaceModule)); QTextStream r(&w.buffer); r << QString(R"(/* 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 %1::*; )").arg(conf.implementationModule); writeRustTypes(conf, r); for (auto object: conf.objects) { writeRustInterfaceObject(r, object, conf); } } void writeRustImplementationObject(QTextStream& r, const Object& o) { const QString lcname(snakeCase(o.name)); if (o.type != ObjectType::Object) { r << "#[derive(Default, Clone)]\n"; r << QString("struct %1Item {\n").arg(o.name); for (auto ip: o.itemProperties) { const QString lc(snakeCase(ip.name)); r << QString(" %1: %2,\n").arg(lc, ip.type.rustType); } r << "}\n\n"; } QString modelStruct = ""; r << QString("pub struct %1 {\n emit: %1Emitter,\n").arg((o.name)); if (o.type == ObjectType::List) { modelStruct = ", model: " + o.name + "List"; r << QString(" model: %1List,\n").arg(o.name); } else if (o.type == ObjectType::Tree) { modelStruct = ", model: " + o.name + "Tree"; r << QString(" model: %1Tree,\n").arg(o.name); } for (const Property& p: o.properties) { const QString lc(snakeCase(p.name)); r << QString(" %1: %2,\n").arg(lc, rustType(p)); } if (o.type != ObjectType::Object) { r << QString(" list: Vec<%1Item>,\n").arg(o.name); } r << "}\n\n"; for (const Property& p: o.properties) { if (p.type.type == BindingType::Object) { modelStruct += ", " + p.name + ": " + p.type.name; } } r << QString(R"(impl %1Trait for %1 { fn new(emit: %1Emitter%2) -> %1 { %1 { emit: emit, )").arg(o.name, modelStruct); if (o.type != ObjectType::Object) { r << QString(" model: model,\n"); } for (const Property& p: o.properties) { const QString lc(snakeCase(p.name)); if (p.type.type == BindingType::Object) { r << QString(" %1: %1,\n").arg(lc); } else { r << QString(" %1: %2,\n").arg(lc, rustTypeInit(p)); } } r << QString(R"( } } fn emit(&self) -> &%1Emitter { &self.emit } )").arg(o.name); for (const Property& p: o.properties) { const QString lc(snakeCase(p.name)); if (p.type.type == BindingType::Object) { r << QString(R"( fn %1(&self) -> &%2 { &self.%1 } fn %1_mut(&mut self) -> &mut %2 { &mut self.%1 } )").arg(lc, rustReturnType(p)); } else { r << QString(" fn %1(&self) -> %2 {\n").arg(lc, rustReturnType(p)); if (p.type.isComplex()) { if (p.optional) { /* if (rustType(p) == "Option") { r << QString(" self.%1.as_ref().map(|p|p.as_str())\n").arg(lc); } else { } */ r << QString(" self.%1.as_ref().map(|p|&p[..])\n").arg(lc); } else { r << QString(" &self.%1\n").arg(lc); } } else { r << QString(" self.%1\n").arg(lc); } r << " }\n"; if (p.write) { r << QString(R"( fn set_%1(&mut self, value: %2) { self.%1 = value; self.emit.%1_changed(); } )").arg(lc, rustType(p)); } } } if (o.type == ObjectType::List) { r << " fn row_count(&self) -> usize {\n self.list.len()\n }\n"; } else if (o.type == ObjectType::Tree) { r << R"( fn row_count(&self, item: Option) -> usize { self.list.len() } fn index(&self, item: Option, row: usize) -> usize { 0 } - fn parent(&self, item: usize) -> Option { + fn parent(&self, index: usize) -> Option { None } - fn row(&self, item: usize) -> usize { + fn row(&self, index: usize) -> usize { item } )"; } if (o.type != ObjectType::Object) { QString index; if (o.type == ObjectType::Tree) { - index = ", item: usize"; + index = ", index: usize"; } for (auto ip: o.itemProperties) { const QString lc(snakeCase(ip.name)); - r << QString(" fn %1(&self, item: usize) -> %2 {\n") + r << QString(" fn %1(&self, index: usize) -> %2 {\n") .arg(lc, rustReturnType(ip)); if (ip.type.isComplex()) { r << " &self.list[item]." << lc << "\n"; } else { r << " self.list[item]." << lc << "\n"; } r << " }\n"; if (ip.write) { - r << QString(" fn set_%1(&mut self, item: usize, v: %2) -> bool {\n") + r << QString(" fn set_%1(&mut self, index: usize, v: %2) -> bool {\n") .arg(snakeCase(ip.name), rustType(ip)); r << " self.list[item]." << lc << " = v;\n"; r << " true\n"; r << " }\n"; } } } r << "}\n\n"; } void writeRustImplementation(const Configuration& conf) { DifferentFileWriter w(rustFile(conf.rustdir, conf.implementationModule), conf.overwriteImplementation); QTextStream r(&w.buffer); r << QString(R"(#![allow(unused_imports)] #![allow(unused_variables)] #![allow(dead_code)] use %1::*; )").arg(conf.interfaceModule); for (auto object: conf.objects) { writeRustImplementationObject(r, object); } } diff --git a/tests/rust_list/src/implementation.rs b/tests/rust_list/src/implementation.rs index 6149563..d71a2c2 100644 --- a/tests/rust_list/src/implementation.rs +++ b/tests/rust_list/src/implementation.rs @@ -1,75 +1,75 @@ #![allow(unused_imports)] #![allow(unused_variables)] #![allow(dead_code)] use interface::*; #[derive(Default, Clone)] struct PersonsItem { user_name: String, age: u8, } pub struct Persons { emit: PersonsEmitter, model: PersonsList, list: Vec, } impl PersonsTrait for Persons { fn new(emit: PersonsEmitter, model: PersonsList) -> Persons { Persons { emit: emit, model: model, list: vec![PersonsItem::default(); 10], } } fn emit(&self) -> &PersonsEmitter { &self.emit } fn row_count(&self) -> usize { self.list.len() } - fn user_name(&self, item: usize) -> &str { - &self.list[item].user_name + fn user_name(&self, index: usize) -> &str { + &self.list[index].user_name } - fn set_user_name(&mut self, item: usize, v: String) -> bool { - self.list[item].user_name = v; + fn set_user_name(&mut self, index: usize, v: String) -> bool { + self.list[index].user_name = v; true } } pub struct NoRole { emit: NoRoleEmitter, model: NoRoleList, list: Vec } impl NoRoleTrait for NoRole { fn new(emit: NoRoleEmitter, model: NoRoleList) -> NoRole { NoRole { emit: emit, model: model, list: vec![PersonsItem::default(); 10], } } fn emit(&self) -> &NoRoleEmitter { &self.emit } fn row_count(&self) -> usize { self.list.len() } - fn user_name(&self, item: usize) -> &str { - &self.list[item].user_name + fn user_name(&self, index: usize) -> &str { + &self.list[index].user_name } - fn set_user_name(&mut self, item: usize, v: String) -> bool { - self.list[item].user_name = v; + fn set_user_name(&mut self, index: usize, v: String) -> bool { + self.list[index].user_name = v; true } - fn user_age(&self, item: usize) -> u8 { - self.list[item].age + fn user_age(&self, index: usize) -> u8 { + self.list[index].age } - fn set_user_age(&mut self, item: usize, v: u8) -> bool { - self.list[item].age = v; + fn set_user_age(&mut self, index: usize, v: u8) -> bool { + self.list[index].age = v; true } } diff --git a/tests/rust_list/src/interface.rs b/tests/rust_list/src/interface.rs index 972e14e..2ffc972 100644 --- a/tests/rust_list/src/interface.rs +++ b/tests/rust_list/src/interface.rs @@ -1,414 +1,414 @@ /* 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)] 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, item: usize) -> u8; - fn set_user_age(&mut self, item: usize, u8) -> bool; - fn user_name(&self, item: usize) -> &str; - fn set_user_name(&mut self, item: usize, String) -> bool; + 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, item: usize) -> &str; - fn set_user_name(&mut self, item: usize, String) -> bool; + 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/implementation.rs b/tests/rust_list_types/src/implementation.rs index 770e85d..f9a62ef 100644 --- a/tests/rust_list_types/src/implementation.rs +++ b/tests/rust_list_types/src/implementation.rs @@ -1,169 +1,169 @@ #![allow(unused_imports)] #![allow(unused_variables)] #![allow(dead_code)] use interface::*; #[derive(Default, Clone)] struct ListItem { boolean: bool, optional_boolean: Option, i8: i8, u8: u8, i16: i16, u16: u16, i32: i32, u32: u32, i64: i64, u64: u64, f32: f32, f64: f64, bytearray: Vec, optional_bytearray: Option>, string: String, optional_string: Option, } pub struct List { emit: ListEmitter, model: ListList, list: Vec, } impl ListTrait for List { fn new(emit: ListEmitter, model: ListList) -> List { List { emit: emit, model: model, list: vec![ListItem::default(); 10], } } fn emit(&self) -> &ListEmitter { &self.emit } fn row_count(&self) -> usize { self.list.len() } - fn boolean(&self, item: usize) -> bool { - self.list[item].boolean + fn boolean(&self, index: usize) -> bool { + self.list[index].boolean } - fn set_boolean(&mut self, item: usize, v: bool) -> bool { - self.list[item].boolean = v; + fn set_boolean(&mut self, index: usize, v: bool) -> bool { + self.list[index].boolean = v; true } - fn optional_boolean(&self, item: usize) -> Option { - self.list[item].optional_boolean + fn optional_boolean(&self, index: usize) -> Option { + self.list[index].optional_boolean } - fn set_optional_boolean(&mut self, item: usize, v: Option) -> bool { - self.list[item].optional_boolean = v; + fn set_optional_boolean(&mut self, index: usize, v: Option) -> bool { + self.list[index].optional_boolean = v; true } - fn i8(&self, item: usize) -> i8 { - self.list[item].i8 + fn i8(&self, index: usize) -> i8 { + self.list[index].i8 } - fn set_i8(&mut self, item: usize, v: i8) -> bool { - self.list[item].i8 = v; + fn set_i8(&mut self, index: usize, v: i8) -> bool { + self.list[index].i8 = v; true } - fn u8(&self, item: usize) -> u8 { - self.list[item].u8 + fn u8(&self, index: usize) -> u8 { + self.list[index].u8 } - fn set_u8(&mut self, item: usize, v: u8) -> bool { - self.list[item].u8 = v; + fn set_u8(&mut self, index: usize, v: u8) -> bool { + self.list[index].u8 = v; true } - fn i16(&self, item: usize) -> i16 { - self.list[item].i16 + fn i16(&self, index: usize) -> i16 { + self.list[index].i16 } - fn set_i16(&mut self, item: usize, v: i16) -> bool { - self.list[item].i16 = v; + fn set_i16(&mut self, index: usize, v: i16) -> bool { + self.list[index].i16 = v; true } - fn u16(&self, item: usize) -> u16 { - self.list[item].u16 + fn u16(&self, index: usize) -> u16 { + self.list[index].u16 } - fn set_u16(&mut self, item: usize, v: u16) -> bool { - self.list[item].u16 = v; + fn set_u16(&mut self, index: usize, v: u16) -> bool { + self.list[index].u16 = v; true } - fn i32(&self, item: usize) -> i32 { - self.list[item].i32 + fn i32(&self, index: usize) -> i32 { + self.list[index].i32 } - fn set_i32(&mut self, item: usize, v: i32) -> bool { - self.list[item].i32 = v; + fn set_i32(&mut self, index: usize, v: i32) -> bool { + self.list[index].i32 = v; true } - fn u32(&self, item: usize) -> u32 { - self.list[item].u32 + fn u32(&self, index: usize) -> u32 { + self.list[index].u32 } - fn set_u32(&mut self, item: usize, v: u32) -> bool { - self.list[item].u32 = v; + fn set_u32(&mut self, index: usize, v: u32) -> bool { + self.list[index].u32 = v; true } - fn i64(&self, item: usize) -> i64 { - self.list[item].i64 + fn i64(&self, index: usize) -> i64 { + self.list[index].i64 } - fn set_i64(&mut self, item: usize, v: i64) -> bool { - self.list[item].i64 = v; + fn set_i64(&mut self, index: usize, v: i64) -> bool { + self.list[index].i64 = v; true } - fn u64(&self, item: usize) -> u64 { - self.list[item].u64 + fn u64(&self, index: usize) -> u64 { + self.list[index].u64 } - fn set_u64(&mut self, item: usize, v: u64) -> bool { - self.list[item].u64 = v; + fn set_u64(&mut self, index: usize, v: u64) -> bool { + self.list[index].u64 = v; true } - fn f32(&self, item: usize) -> f32 { - self.list[item].f32 + fn f32(&self, index: usize) -> f32 { + self.list[index].f32 } - fn set_f32(&mut self, item: usize, v: f32) -> bool { - self.list[item].f32 = v; + fn set_f32(&mut self, index: usize, v: f32) -> bool { + self.list[index].f32 = v; true } - fn f64(&self, item: usize) -> f64 { - self.list[item].f64 + fn f64(&self, index: usize) -> f64 { + self.list[index].f64 } - fn set_f64(&mut self, item: usize, v: f64) -> bool { - self.list[item].f64 = v; + fn set_f64(&mut self, index: usize, v: f64) -> bool { + self.list[index].f64 = v; true } - fn string(&self, item: usize) -> &str { - &self.list[item].string + fn string(&self, index: usize) -> &str { + &self.list[index].string } - fn set_string(&mut self, item: usize, v: String) -> bool { - self.list[item].string = v; + fn set_string(&mut self, index: usize, v: String) -> bool { + self.list[index].string = v; true } - fn optional_string(&self, item: usize) -> Option<&str> { - self.list[item].optional_string.as_ref().map(|p|&p[..]) + fn optional_string(&self, index: usize) -> Option<&str> { + self.list[index].optional_string.as_ref().map(|p|&p[..]) } - fn set_optional_string(&mut self, item: usize, v: Option) -> bool { - self.list[item].optional_string = v; + fn set_optional_string(&mut self, index: usize, v: Option) -> bool { + self.list[index].optional_string = v; true } - fn bytearray(&self, item: usize) -> &[u8] { - &self.list[item].bytearray + fn bytearray(&self, index: usize) -> &[u8] { + &self.list[index].bytearray } - fn set_bytearray(&mut self, item: usize, v: &[u8]) -> bool { - self.list[item].bytearray.truncate(0); - self.list[item].bytearray.extend_from_slice(v); + fn set_bytearray(&mut self, index: usize, v: &[u8]) -> bool { + self.list[index].bytearray.truncate(0); + self.list[index].bytearray.extend_from_slice(v); true } - fn optional_bytearray(&self, item: usize) -> Option<&[u8]> { - self.list[item].optional_bytearray.as_ref().map(|p|&p[..]) + fn optional_bytearray(&self, index: usize) -> Option<&[u8]> { + self.list[index].optional_bytearray.as_ref().map(|p|&p[..]) } - fn set_optional_bytearray(&mut self, item: usize, v: Option<&[u8]>) -> bool { - match (v, &mut self.list[item].optional_bytearray) { + fn set_optional_bytearray(&mut self, index: usize, v: Option<&[u8]>) -> bool { + match (v, &mut self.list[index].optional_bytearray) { (Some(value), &mut Some(ref mut b)) => { b.truncate(0); b.extend_from_slice(value); }, (Some(value), b) => { *b = Some(value.into()) }, (None, b) => {*b = None;} }; true } } diff --git a/tests/rust_list_types/src/interface.rs b/tests/rust_list_types/src/interface.rs index be4f34b..bcc76fa 100644 --- a/tests/rust_list_types/src/interface.rs +++ b/tests/rust_list_types/src/interface.rs @@ -1,526 +1,526 @@ /* 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)] 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, item: usize) -> bool; - fn set_boolean(&mut self, item: usize, bool) -> bool; - fn bytearray(&self, item: usize) -> &[u8]; - fn set_bytearray(&mut self, item: usize, &[u8]) -> bool; - fn f32(&self, item: usize) -> f32; - fn set_f32(&mut self, item: usize, f32) -> bool; - fn f64(&self, item: usize) -> f64; - fn set_f64(&mut self, item: usize, f64) -> bool; - fn i16(&self, item: usize) -> i16; - fn set_i16(&mut self, item: usize, i16) -> bool; - fn i32(&self, item: usize) -> i32; - fn set_i32(&mut self, item: usize, i32) -> bool; - fn i64(&self, item: usize) -> i64; - fn set_i64(&mut self, item: usize, i64) -> bool; - fn i8(&self, item: usize) -> i8; - fn set_i8(&mut self, item: usize, i8) -> bool; - fn optional_boolean(&self, item: usize) -> Option; - fn set_optional_boolean(&mut self, item: usize, Option) -> bool; - fn optional_bytearray(&self, item: usize) -> Option<&[u8]>; - fn set_optional_bytearray(&mut self, item: usize, Option<&[u8]>) -> bool; - fn optional_string(&self, item: usize) -> Option<&str>; - fn set_optional_string(&mut self, item: usize, Option) -> bool; - fn string(&self, item: usize) -> &str; - fn set_string(&mut self, item: usize, String) -> bool; - fn u16(&self, item: usize) -> u16; - fn set_u16(&mut self, item: usize, u16) -> bool; - fn u32(&self, item: usize) -> u32; - fn set_u32(&mut self, item: usize, u32) -> bool; - fn u64(&self, item: usize) -> u64; - fn set_u64(&mut self, item: usize, u64) -> bool; - fn u8(&self, item: usize) -> u8; - fn set_u8(&mut self, item: usize, u8) -> bool; + 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_tree/src/implementation.rs b/tests/rust_tree/src/implementation.rs index 1ca5526..47c1c9b 100644 --- a/tests/rust_tree/src/implementation.rs +++ b/tests/rust_tree/src/implementation.rs @@ -1,48 +1,48 @@ #![allow(unused_imports)] #![allow(unused_variables)] #![allow(dead_code)] use interface::*; #[derive(Default, Clone)] struct PersonsItem { user_name: String, } pub struct Persons { emit: PersonsEmitter, model: PersonsTree, list: Vec, } impl PersonsTrait for Persons { fn new(emit: PersonsEmitter, model: PersonsTree) -> Persons { Persons { emit: emit, model: model, list: vec![PersonsItem::default(); 10], } } fn emit(&self) -> &PersonsEmitter { &self.emit } - fn row_count(&self, item: Option) -> usize { + fn row_count(&self, index: Option) -> usize { self.list.len() } - fn index(&self, item: Option, row: usize) -> usize { + fn index(&self, index: Option, row: usize) -> usize { 0 } - fn parent(&self, item: usize) -> Option { + fn parent(&self, index: usize) -> Option { None } - fn row(&self, item: usize) -> usize { - item + fn row(&self, index: usize) -> usize { + index } - fn user_name(&self, item: usize) -> &str { - &self.list[item].user_name + fn user_name(&self, index: usize) -> &str { + &self.list[index].user_name } - fn set_user_name(&mut self, item: usize, v: String) -> bool { - self.list[item].user_name = v; + fn set_user_name(&mut self, index: usize, v: String) -> bool { + self.list[index].user_name = v; true } } diff --git a/tests/rust_tree/src/interface.rs b/tests/rust_tree/src/interface.rs index afa7b8e..1d15dab 100644 --- a/tests/rust_tree/src/interface.rs +++ b/tests/rust_tree/src/interface.rs @@ -1,283 +1,283 @@ /* 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)] 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, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + begin_insert_rows: fn(*const PersonsQObject, index: usize, valid: bool, usize, usize), end_insert_rows: fn(*const PersonsQObject), - begin_remove_rows: fn(*const PersonsQObject, item: usize, valid: bool, usize, usize), + 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, item: Option, first: usize, last: usize) { - (self.begin_insert_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + 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, item: Option, first: usize, last: usize) { - (self.begin_remove_rows)(self.qobject, item.unwrap_or(13), item.is_some(), first, last); + 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, item: usize) -> Option; - fn row(&self, item: usize) -> usize; - fn user_name(&self, item: usize) -> &str; - fn set_user_name(&mut self, item: usize, String) -> bool; + 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, item: usize, valid: bool), + 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, item: usize, valid: bool, usize, usize), + 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, item: usize, valid: bool, usize, usize), + 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, - item: usize, + index: usize, valid: bool, ) -> c_int { to_c_int(if valid { - (&*ptr).row_count(Some(item)) + (&*ptr).row_count(Some(index)) } else { (&*ptr).row_count(None) }) } #[no_mangle] pub unsafe extern "C" fn persons_can_fetch_more( ptr: *const Persons, - item: usize, + index: usize, valid: bool, ) -> bool { if valid { - (&*ptr).can_fetch_more(Some(item)) + (&*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, item: usize, valid: bool) { +pub unsafe extern "C" fn persons_fetch_more(ptr: *mut Persons, index: usize, valid: bool) { if valid { - (&mut *ptr).fetch_more(Some(item)) + (&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, - item: usize, + index: usize, valid: bool, row: c_int, ) -> usize { if !valid { (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), to_usize(row)) + (&*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, item: usize) -> c_int { - to_c_int((&*ptr).row(item)) +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, item: usize, + 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(item); + 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, item: usize, + 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(item, v) + o.set_user_name(index, v) }