diff --git a/demo/rust/src/implementation/fibonacci.rs b/demo/rust/src/implementation/fibonacci.rs index b2fb3b2..43bcc20 100644 --- a/demo/rust/src/implementation/fibonacci.rs +++ b/demo/rust/src/implementation/fibonacci.rs @@ -62,7 +62,7 @@ impl FibonacciTrait for Fibonacci { fn set_input(&mut self, value: u32) { self.input = value; self.emit.input_changed(); - let emit = self.emit.clone(); + let mut emit = self.emit.clone(); let result = self.result.clone(); result.swap(0, Ordering::SeqCst); emit.result_changed(); diff --git a/demo/rust/src/implementation/file_system_tree.rs b/demo/rust/src/implementation/file_system_tree.rs index 38000dd..4285207 100644 --- a/demo/rust/src/implementation/file_system_tree.rs +++ b/demo/rust/src/implementation/file_system_tree.rs @@ -23,9 +23,8 @@ use std::path::PathBuf; use std::ffi::OsString; use std::default::Default; use std::thread; -use std::sync::{Arc, Mutex}; +use std::sync::mpsc::{Receiver, Sender, channel}; use std::marker::Sync; -use std::collections::HashMap; pub struct DirEntry { name: OsString, @@ -34,15 +33,13 @@ pub struct DirEntry { 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() + icon: Vec::new(), } } fn can_fetch_more(&self) -> bool { @@ -66,10 +63,19 @@ impl Item for DirEntry { fn icon(&self) -> &[u8] { &self.icon } - fn retrieve(id: usize, parents: Vec<&DirEntry>, q: Incoming, emit: FileSystemTreeEmitter) { - let mut v = Vec::new(); + fn retrieve(id: usize, parents: Vec<&Self>, outgoing: &Sender<(usize, PathBuf)>) { let path: PathBuf = parents.into_iter().map(|e| &e.name).collect(); - thread::spawn(move || { + if let Err(e) = outgoing.send((id, path)) { + eprintln!("{}", e); + } + } + fn read_files( + incoming: Receiver<(usize, PathBuf)>, + outgoing: Sender<(usize, Vec)>, + mut emit: FileSystemTreeEmitter, + ) { + thread::spawn(move || while let Ok((id, path)) = incoming.recv() { + let mut v = Vec::new(); if let Ok(it) = read_dir(&path) { for i in it.filter_map(|v| v.ok()) { let de = DirEntry { @@ -82,11 +88,11 @@ impl Item for DirEntry { } } 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)); - } + if let Ok(()) = outgoing.send((id, v)) { + emit.new_data_ready(Some(id)); + } else { + return; + } }); } } @@ -105,7 +111,12 @@ impl Default for DirEntry { 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 retrieve(id: usize, parents: Vec<&Self>, outgoing: &Sender<(usize, PathBuf)>); + fn read_files( + incoming: Receiver<(usize, PathBuf)>, + outgoing: Sender<(usize, Vec)>, + emit: FileSystemTreeEmitter, + ); fn file_name(&self) -> String; fn file_path(&self) -> Option; fn file_permissions(&self) -> i32; @@ -128,7 +139,8 @@ pub struct RGeneralItemModel { model: FileSystemTreeTree, entries: Vec>, path: Option, - incoming: Incoming, + outgoing: Sender<(usize, PathBuf)>, + incoming: Receiver<(usize, Vec)>, } impl RGeneralItemModel @@ -152,43 +164,37 @@ where fn get(&self, index: usize) -> &Entry { &self.entries[index] } - fn retrieve(&mut self, index: usize) { + fn retrieve(&self, index: usize) { let parents = self.get_parents(index); - let incoming = self.incoming.clone(); - T::retrieve(index, parents, incoming, self.emit.clone()); + T::retrieve(index, parents, &self.outgoing); } 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, - ); - } - } + while let Ok((id, entries)) = self.incoming.try_recv() { + 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() { + new_entries.push(Entry { + parent: Some(id), + row: r, + children: None, + data: d, + }); + children.push(self.entries.len() + r); + } + if new_entries.is_empty() { self.entries[id].children = Some(children); - if !new_entries.is_empty() { - self.entries.append(&mut new_entries); - self.model.end_insert_rows(); - } + } else { + self.model.begin_insert_rows( + Some(id), + 0, + new_entries.len() - 1, + ); + self.entries[id].children = Some(children); + self.entries.append(&mut new_entries); + self.model.end_insert_rows(); } } } @@ -207,13 +213,17 @@ impl FileSystemTreeTrait for RGeneralItemModel where T: Sync + Send, { - fn new(emit: FileSystemTreeEmitter, model: FileSystemTreeTree) -> Self { - let mut tree = RGeneralItemModel { + fn new(mut emit: FileSystemTreeEmitter, model: FileSystemTreeTree) -> Self { + let (outgoing, thread_incoming) = channel(); + let (thread_outgoing, incoming) = channel::<(usize, Vec)>(); + T::read_files(thread_incoming, thread_outgoing, emit.clone()); + let mut tree: RGeneralItemModel = RGeneralItemModel { emit, model, entries: Vec::new(), path: None, - incoming: Arc::new(Mutex::new(HashMap::new())), + outgoing, + incoming, }; tree.reset(); tree @@ -222,7 +232,7 @@ where &self.emit } fn path(&self) -> Option<&str> { - self.path.as_ref().map(|s|&s[..]) + self.path.as_ref().map(|s| &s[..]) } fn set_path(&mut self, value: Option) { if self.path != value { @@ -232,21 +242,15 @@ where } } 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 - } + let entry = self.get(index.unwrap_or(0)); + entry.children.is_none() && entry.data.can_fetch_more() } fn fetch_more(&mut self, index: Option) { self.process_incoming(); if !self.can_fetch_more(index) { return; } - if let Some(index) = index { - self.retrieve(index); - } + self.retrieve(index.unwrap_or(0)); } fn row_count(&self, index: Option) -> usize { if self.entries.is_empty() { @@ -257,10 +261,7 @@ where 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(index) { - self.emit.new_data_ready(index); - } + self.retrieve(i); 0 } } else { diff --git a/demo/rust/src/implementation/processes.rs b/demo/rust/src/implementation/processes.rs index 44cdc4b..dcc1b98 100644 --- a/demo/rust/src/implementation/processes.rs +++ b/demo/rust/src/implementation/processes.rs @@ -127,7 +127,7 @@ fn update() -> ProcessTree { } fn update_thread( - emit: ProcessesEmitter, + mut emit: ProcessesEmitter, incoming: Arc>>, mut active: bool, status_channel: Receiver, @@ -178,7 +178,7 @@ fn move_process( } fn remove_row( - model: &ProcessesTree, + model: &mut ProcessesTree, parent: pid_t, row: usize, map: &mut HashMap, @@ -205,7 +205,7 @@ fn remove_row( } fn insert_row( - model: &ProcessesTree, + model: &mut ProcessesTree, parent: pid_t, row: usize, map: &mut HashMap, @@ -236,7 +236,7 @@ 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 { +fn sync_row(model: &mut 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); @@ -264,7 +264,7 @@ fn sync_row(model: &ProcessesTree, pid: pid_t, a: &mut Process, b: &Process) -> } fn sync_tree( - model: &ProcessesTree, + model: &mut ProcessesTree, parent: pid_t, amap: &mut HashMap, bmap: &mut HashMap, @@ -321,7 +321,7 @@ fn sync_tree( } impl ProcessesTrait for Processes { - fn new(emit: ProcessesEmitter, model: ProcessesTree) -> Processes { + fn new(mut emit: ProcessesEmitter, model: ProcessesTree) -> Processes { let (tx, rx) = channel(); let p = Processes { emit: emit.clone(), @@ -382,7 +382,7 @@ impl ProcessesTrait for Processes { } else { let top = self.p.top.clone(); for pid in top { - sync_tree(&self.model, pid, &mut self.p.processes, &mut new.processes); + sync_tree(&mut self.model, pid, &mut self.p.processes, &mut new.processes); } } } diff --git a/demo/rust/src/interface.rs b/demo/rust/src/interface.rs index 112f102..866bbaa 100644 --- a/demo/rust/src/interface.rs +++ b/demo/rust/src/interface.rs @@ -94,7 +94,6 @@ fn to_c_int(n: usize) -> c_int { pub struct DemoQObject {} -#[derive(Clone)] pub struct DemoEmitter { qobject: Arc>, } @@ -102,6 +101,17 @@ pub struct DemoEmitter { unsafe impl Send for DemoEmitter {} impl DemoEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> DemoEmitter { + DemoEmitter { + qobject: self.qobject.clone(), + } + } fn clear(&self) { let n: *const DemoQObject = null(); self.qobject.store(n as *mut DemoQObject, Ordering::SeqCst); @@ -132,62 +142,62 @@ pub trait DemoTrait { pub extern "C" fn demo_new( demo: *mut DemoQObject, fibonacci: *mut FibonacciQObject, - fibonacci_input_changed: fn(*const FibonacciQObject), - fibonacci_result_changed: fn(*const FibonacciQObject), + fibonacci_input_changed: fn(*mut FibonacciQObject), + fibonacci_result_changed: fn(*mut FibonacciQObject), fibonacci_list: *mut FibonacciListQObject, - fibonacci_list_new_data_ready: fn(*const FibonacciListQObject), - fibonacci_list_layout_about_to_be_changed: fn(*const FibonacciListQObject), - fibonacci_list_layout_changed: 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_move_rows: fn(*const FibonacciListQObject, usize, usize, usize), - fibonacci_list_end_move_rows: fn(*const FibonacciListQObject), - fibonacci_list_begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), - fibonacci_list_end_remove_rows: fn(*const FibonacciListQObject), + fibonacci_list_new_data_ready: fn(*mut FibonacciListQObject), + fibonacci_list_layout_about_to_be_changed: fn(*mut FibonacciListQObject), + fibonacci_list_layout_changed: fn(*mut FibonacciListQObject), + fibonacci_list_data_changed: fn(*mut FibonacciListQObject, usize, usize), + fibonacci_list_begin_reset_model: fn(*mut FibonacciListQObject), + fibonacci_list_end_reset_model: fn(*mut FibonacciListQObject), + fibonacci_list_begin_insert_rows: fn(*mut FibonacciListQObject, usize, usize), + fibonacci_list_end_insert_rows: fn(*mut FibonacciListQObject), + fibonacci_list_begin_move_rows: fn(*mut FibonacciListQObject, usize, usize, usize), + fibonacci_list_end_move_rows: fn(*mut FibonacciListQObject), + fibonacci_list_begin_remove_rows: fn(*mut FibonacciListQObject, usize, usize), + fibonacci_list_end_remove_rows: fn(*mut FibonacciListQObject), file_system_tree: *mut FileSystemTreeQObject, - file_system_tree_path_changed: fn(*const FileSystemTreeQObject), - file_system_tree_new_data_ready: fn(*const FileSystemTreeQObject, index: COption), - file_system_tree_layout_about_to_be_changed: fn(*const FileSystemTreeQObject), - file_system_tree_layout_changed: fn(*const FileSystemTreeQObject), - file_system_tree_data_changed: fn(*const FileSystemTreeQObject, usize, usize), - file_system_tree_begin_reset_model: fn(*const FileSystemTreeQObject), - file_system_tree_end_reset_model: fn(*const FileSystemTreeQObject), - file_system_tree_begin_insert_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize), - file_system_tree_end_insert_rows: fn(*const FileSystemTreeQObject), - file_system_tree_begin_move_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize, index: COption, usize), - file_system_tree_end_move_rows: fn(*const FileSystemTreeQObject), - file_system_tree_begin_remove_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize), - file_system_tree_end_remove_rows: fn(*const FileSystemTreeQObject), + file_system_tree_path_changed: fn(*mut FileSystemTreeQObject), + file_system_tree_new_data_ready: fn(*mut FileSystemTreeQObject, index: COption), + file_system_tree_layout_about_to_be_changed: fn(*mut FileSystemTreeQObject), + file_system_tree_layout_changed: fn(*mut FileSystemTreeQObject), + file_system_tree_data_changed: fn(*mut FileSystemTreeQObject, usize, usize), + file_system_tree_begin_reset_model: fn(*mut FileSystemTreeQObject), + file_system_tree_end_reset_model: fn(*mut FileSystemTreeQObject), + file_system_tree_begin_insert_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize), + file_system_tree_end_insert_rows: fn(*mut FileSystemTreeQObject), + file_system_tree_begin_move_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize, index: COption, usize), + file_system_tree_end_move_rows: fn(*mut FileSystemTreeQObject), + file_system_tree_begin_remove_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize), + file_system_tree_end_remove_rows: fn(*mut FileSystemTreeQObject), processes: *mut ProcessesQObject, - processes_active_changed: fn(*const ProcessesQObject), - processes_new_data_ready: fn(*const ProcessesQObject, index: COption), - processes_layout_about_to_be_changed: fn(*const ProcessesQObject), - processes_layout_changed: fn(*const ProcessesQObject), - processes_data_changed: fn(*const ProcessesQObject, usize, usize), - processes_begin_reset_model: fn(*const ProcessesQObject), - processes_end_reset_model: fn(*const ProcessesQObject), - processes_begin_insert_rows: fn(*const ProcessesQObject, index: COption, usize, usize), - processes_end_insert_rows: fn(*const ProcessesQObject), - processes_begin_move_rows: fn(*const ProcessesQObject, index: COption, usize, usize, index: COption, usize), - processes_end_move_rows: fn(*const ProcessesQObject), - processes_begin_remove_rows: fn(*const ProcessesQObject, index: COption, usize, usize), - processes_end_remove_rows: fn(*const ProcessesQObject), + processes_active_changed: fn(*mut ProcessesQObject), + processes_new_data_ready: fn(*mut ProcessesQObject, index: COption), + processes_layout_about_to_be_changed: fn(*mut ProcessesQObject), + processes_layout_changed: fn(*mut ProcessesQObject), + processes_data_changed: fn(*mut ProcessesQObject, usize, usize), + processes_begin_reset_model: fn(*mut ProcessesQObject), + processes_end_reset_model: fn(*mut ProcessesQObject), + processes_begin_insert_rows: fn(*mut ProcessesQObject, index: COption, usize, usize), + processes_end_insert_rows: fn(*mut ProcessesQObject), + processes_begin_move_rows: fn(*mut ProcessesQObject, index: COption, usize, usize, index: COption, usize), + processes_end_move_rows: fn(*mut ProcessesQObject), + processes_begin_remove_rows: fn(*mut ProcessesQObject, index: COption, usize, usize), + processes_end_remove_rows: fn(*mut ProcessesQObject), time_series: *mut TimeSeriesQObject, - time_series_new_data_ready: fn(*const TimeSeriesQObject), - time_series_layout_about_to_be_changed: fn(*const TimeSeriesQObject), - time_series_layout_changed: 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_move_rows: fn(*const TimeSeriesQObject, usize, usize, usize), - time_series_end_move_rows: fn(*const TimeSeriesQObject), - time_series_begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), - time_series_end_remove_rows: fn(*const TimeSeriesQObject), + time_series_new_data_ready: fn(*mut TimeSeriesQObject), + time_series_layout_about_to_be_changed: fn(*mut TimeSeriesQObject), + time_series_layout_changed: fn(*mut TimeSeriesQObject), + time_series_data_changed: fn(*mut TimeSeriesQObject, usize, usize), + time_series_begin_reset_model: fn(*mut TimeSeriesQObject), + time_series_end_reset_model: fn(*mut TimeSeriesQObject), + time_series_begin_insert_rows: fn(*mut TimeSeriesQObject, usize, usize), + time_series_end_insert_rows: fn(*mut TimeSeriesQObject), + time_series_begin_move_rows: fn(*mut TimeSeriesQObject, usize, usize, usize), + time_series_end_move_rows: fn(*mut TimeSeriesQObject), + time_series_begin_remove_rows: fn(*mut TimeSeriesQObject, usize, usize), + time_series_end_remove_rows: fn(*mut TimeSeriesQObject), ) -> *mut Demo { let fibonacci_emit = FibonacciEmitter { qobject: Arc::new(AtomicPtr::new(fibonacci)), @@ -317,27 +327,39 @@ pub unsafe extern "C" fn demo_time_series_get(ptr: *mut Demo) -> *mut TimeSeries pub struct FibonacciQObject {} -#[derive(Clone)] pub struct FibonacciEmitter { qobject: Arc>, - input_changed: fn(*const FibonacciQObject), - result_changed: fn(*const FibonacciQObject), + input_changed: fn(*mut FibonacciQObject), + result_changed: fn(*mut FibonacciQObject), } unsafe impl Send for FibonacciEmitter {} impl FibonacciEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> FibonacciEmitter { + FibonacciEmitter { + qobject: self.qobject.clone(), + input_changed: self.input_changed, + result_changed: self.result_changed, + } + } fn clear(&self) { let n: *const FibonacciQObject = null(); self.qobject.store(n as *mut FibonacciQObject, Ordering::SeqCst); } - pub fn input_changed(&self) { + pub fn input_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.input_changed)(ptr); } } - pub fn result_changed(&self) { + pub fn result_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.result_changed)(ptr); @@ -356,8 +378,8 @@ pub trait FibonacciTrait { #[no_mangle] pub extern "C" fn fibonacci_new( fibonacci: *mut FibonacciQObject, - fibonacci_input_changed: fn(*const FibonacciQObject), - fibonacci_result_changed: fn(*const FibonacciQObject), + fibonacci_input_changed: fn(*mut FibonacciQObject), + fibonacci_result_changed: fn(*mut FibonacciQObject), ) -> *mut Fibonacci { let fibonacci_emit = FibonacciEmitter { qobject: Arc::new(AtomicPtr::new(fibonacci)), @@ -390,20 +412,31 @@ pub unsafe extern "C" fn fibonacci_result_get(ptr: *const Fibonacci) -> u64 { pub struct FibonacciListQObject {} -#[derive(Clone)] pub struct FibonacciListEmitter { qobject: Arc>, - new_data_ready: fn(*const FibonacciListQObject), + new_data_ready: fn(*mut FibonacciListQObject), } unsafe impl Send for FibonacciListEmitter {} impl FibonacciListEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> FibonacciListEmitter { + FibonacciListEmitter { + qobject: self.qobject.clone(), + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const FibonacciListQObject = null(); self.qobject.store(n as *mut FibonacciListQObject, Ordering::SeqCst); } - pub fn new_data_ready(&self) { + pub fn new_data_ready(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr); @@ -413,52 +446,52 @@ impl FibonacciListEmitter { #[derive(Clone)] pub struct FibonacciListList { - qobject: *const FibonacciListQObject, - layout_about_to_be_changed: fn(*const FibonacciListQObject), - layout_changed: fn(*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_move_rows: fn(*const FibonacciListQObject, usize, usize, usize), - end_move_rows: fn(*const FibonacciListQObject), - begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), - end_remove_rows: fn(*const FibonacciListQObject), + qobject: *mut FibonacciListQObject, + layout_about_to_be_changed: fn(*mut FibonacciListQObject), + layout_changed: fn(*mut FibonacciListQObject), + data_changed: fn(*mut FibonacciListQObject, usize, usize), + begin_reset_model: fn(*mut FibonacciListQObject), + end_reset_model: fn(*mut FibonacciListQObject), + begin_insert_rows: fn(*mut FibonacciListQObject, usize, usize), + end_insert_rows: fn(*mut FibonacciListQObject), + begin_move_rows: fn(*mut FibonacciListQObject, usize, usize, usize), + end_move_rows: fn(*mut FibonacciListQObject), + begin_remove_rows: fn(*mut FibonacciListQObject, usize, usize), + end_remove_rows: fn(*mut FibonacciListQObject), } impl FibonacciListList { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, first: usize, last: usize, destination: usize) { + pub fn begin_move_rows(&mut self, first: usize, last: usize, destination: usize) { (self.begin_move_rows)(self.qobject, first, last, destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -481,18 +514,18 @@ pub trait FibonacciListTrait { #[no_mangle] pub extern "C" fn fibonacci_list_new( fibonacci_list: *mut FibonacciListQObject, - fibonacci_list_new_data_ready: fn(*const FibonacciListQObject), - fibonacci_list_layout_about_to_be_changed: fn(*const FibonacciListQObject), - fibonacci_list_layout_changed: 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_move_rows: fn(*const FibonacciListQObject, usize, usize, usize), - fibonacci_list_end_move_rows: fn(*const FibonacciListQObject), - fibonacci_list_begin_remove_rows: fn(*const FibonacciListQObject, usize, usize), - fibonacci_list_end_remove_rows: fn(*const FibonacciListQObject), + fibonacci_list_new_data_ready: fn(*mut FibonacciListQObject), + fibonacci_list_layout_about_to_be_changed: fn(*mut FibonacciListQObject), + fibonacci_list_layout_changed: fn(*mut FibonacciListQObject), + fibonacci_list_data_changed: fn(*mut FibonacciListQObject, usize, usize), + fibonacci_list_begin_reset_model: fn(*mut FibonacciListQObject), + fibonacci_list_end_reset_model: fn(*mut FibonacciListQObject), + fibonacci_list_begin_insert_rows: fn(*mut FibonacciListQObject, usize, usize), + fibonacci_list_end_insert_rows: fn(*mut FibonacciListQObject), + fibonacci_list_begin_move_rows: fn(*mut FibonacciListQObject, usize, usize, usize), + fibonacci_list_end_move_rows: fn(*mut FibonacciListQObject), + fibonacci_list_begin_remove_rows: fn(*mut FibonacciListQObject, usize, usize), + fibonacci_list_end_remove_rows: fn(*mut FibonacciListQObject), ) -> *mut FibonacciList { let fibonacci_list_emit = FibonacciListEmitter { qobject: Arc::new(AtomicPtr::new(fibonacci_list)), @@ -564,27 +597,39 @@ pub unsafe extern "C" fn fibonacci_list_data_row(ptr: *const FibonacciList, row: pub struct FileSystemTreeQObject {} -#[derive(Clone)] pub struct FileSystemTreeEmitter { qobject: Arc>, - path_changed: fn(*const FileSystemTreeQObject), - new_data_ready: fn(*const FileSystemTreeQObject, index: COption), + path_changed: fn(*mut FileSystemTreeQObject), + new_data_ready: fn(*mut FileSystemTreeQObject, index: COption), } unsafe impl Send for FileSystemTreeEmitter {} impl FileSystemTreeEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> FileSystemTreeEmitter { + FileSystemTreeEmitter { + qobject: self.qobject.clone(), + path_changed: self.path_changed, + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const FileSystemTreeQObject = null(); self.qobject.store(n as *mut FileSystemTreeQObject, Ordering::SeqCst); } - pub fn path_changed(&self) { + pub fn path_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.path_changed)(ptr); } } - pub fn new_data_ready(&self, item: Option) { + pub fn new_data_ready(&mut self, item: Option) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr, item.into()); @@ -594,52 +639,52 @@ impl FileSystemTreeEmitter { #[derive(Clone)] pub struct FileSystemTreeTree { - qobject: *const FileSystemTreeQObject, - layout_about_to_be_changed: fn(*const FileSystemTreeQObject), - layout_changed: fn(*const FileSystemTreeQObject), - data_changed: fn(*const FileSystemTreeQObject, usize, usize), - begin_reset_model: fn(*const FileSystemTreeQObject), - end_reset_model: fn(*const FileSystemTreeQObject), - begin_insert_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize), - end_insert_rows: fn(*const FileSystemTreeQObject), - begin_move_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize, dest: COption, usize), - end_move_rows: fn(*const FileSystemTreeQObject), - begin_remove_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize), - end_remove_rows: fn(*const FileSystemTreeQObject), + qobject: *mut FileSystemTreeQObject, + layout_about_to_be_changed: fn(*mut FileSystemTreeQObject), + layout_changed: fn(*mut FileSystemTreeQObject), + data_changed: fn(*mut FileSystemTreeQObject, usize, usize), + begin_reset_model: fn(*mut FileSystemTreeQObject), + end_reset_model: fn(*mut FileSystemTreeQObject), + begin_insert_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize), + end_insert_rows: fn(*mut FileSystemTreeQObject), + begin_move_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize, dest: COption, usize), + end_move_rows: fn(*mut FileSystemTreeQObject), + begin_remove_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize), + end_remove_rows: fn(*mut FileSystemTreeQObject), } impl FileSystemTreeTree { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, index: Option, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, index: Option, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, index.into(), first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, index: Option, first: usize, last: usize, dest: Option, destination: usize) { + pub fn begin_move_rows(&mut self, index: Option, first: usize, last: usize, dest: Option, destination: usize) { (self.begin_move_rows)(self.qobject, index.into(), first, last, dest.into(), destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, index: Option, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, index: Option, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, index.into(), first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -670,19 +715,19 @@ pub trait FileSystemTreeTrait { #[no_mangle] pub extern "C" fn file_system_tree_new( file_system_tree: *mut FileSystemTreeQObject, - file_system_tree_path_changed: fn(*const FileSystemTreeQObject), - file_system_tree_new_data_ready: fn(*const FileSystemTreeQObject, index: COption), - file_system_tree_layout_about_to_be_changed: fn(*const FileSystemTreeQObject), - file_system_tree_layout_changed: fn(*const FileSystemTreeQObject), - file_system_tree_data_changed: fn(*const FileSystemTreeQObject, usize, usize), - file_system_tree_begin_reset_model: fn(*const FileSystemTreeQObject), - file_system_tree_end_reset_model: fn(*const FileSystemTreeQObject), - file_system_tree_begin_insert_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize), - file_system_tree_end_insert_rows: fn(*const FileSystemTreeQObject), - file_system_tree_begin_move_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize, index: COption, usize), - file_system_tree_end_move_rows: fn(*const FileSystemTreeQObject), - file_system_tree_begin_remove_rows: fn(*const FileSystemTreeQObject, index: COption, usize, usize), - file_system_tree_end_remove_rows: fn(*const FileSystemTreeQObject), + file_system_tree_path_changed: fn(*mut FileSystemTreeQObject), + file_system_tree_new_data_ready: fn(*mut FileSystemTreeQObject, index: COption), + file_system_tree_layout_about_to_be_changed: fn(*mut FileSystemTreeQObject), + file_system_tree_layout_changed: fn(*mut FileSystemTreeQObject), + file_system_tree_data_changed: fn(*mut FileSystemTreeQObject, usize, usize), + file_system_tree_begin_reset_model: fn(*mut FileSystemTreeQObject), + file_system_tree_end_reset_model: fn(*mut FileSystemTreeQObject), + file_system_tree_begin_insert_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize), + file_system_tree_end_insert_rows: fn(*mut FileSystemTreeQObject), + file_system_tree_begin_move_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize, index: COption, usize), + file_system_tree_end_move_rows: fn(*mut FileSystemTreeQObject), + file_system_tree_begin_remove_rows: fn(*mut FileSystemTreeQObject, index: COption, usize, usize), + file_system_tree_end_remove_rows: fn(*mut FileSystemTreeQObject), ) -> *mut FileSystemTree { let file_system_tree_emit = FileSystemTreeEmitter { qobject: Arc::new(AtomicPtr::new(file_system_tree)), @@ -859,27 +904,39 @@ pub unsafe extern "C" fn file_system_tree_data_file_type(ptr: *const FileSystemT pub struct ProcessesQObject {} -#[derive(Clone)] pub struct ProcessesEmitter { qobject: Arc>, - active_changed: fn(*const ProcessesQObject), - new_data_ready: fn(*const ProcessesQObject, index: COption), + active_changed: fn(*mut ProcessesQObject), + new_data_ready: fn(*mut ProcessesQObject, index: COption), } unsafe impl Send for ProcessesEmitter {} impl ProcessesEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> ProcessesEmitter { + ProcessesEmitter { + qobject: self.qobject.clone(), + active_changed: self.active_changed, + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const ProcessesQObject = null(); self.qobject.store(n as *mut ProcessesQObject, Ordering::SeqCst); } - pub fn active_changed(&self) { + pub fn active_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.active_changed)(ptr); } } - pub fn new_data_ready(&self, item: Option) { + pub fn new_data_ready(&mut self, item: Option) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr, item.into()); @@ -889,52 +946,52 @@ impl ProcessesEmitter { #[derive(Clone)] pub struct ProcessesTree { - qobject: *const ProcessesQObject, - layout_about_to_be_changed: fn(*const ProcessesQObject), - layout_changed: fn(*const ProcessesQObject), - data_changed: fn(*const ProcessesQObject, usize, usize), - begin_reset_model: fn(*const ProcessesQObject), - end_reset_model: fn(*const ProcessesQObject), - begin_insert_rows: fn(*const ProcessesQObject, index: COption, usize, usize), - end_insert_rows: fn(*const ProcessesQObject), - begin_move_rows: fn(*const ProcessesQObject, index: COption, usize, usize, dest: COption, usize), - end_move_rows: fn(*const ProcessesQObject), - begin_remove_rows: fn(*const ProcessesQObject, index: COption, usize, usize), - end_remove_rows: fn(*const ProcessesQObject), + qobject: *mut ProcessesQObject, + layout_about_to_be_changed: fn(*mut ProcessesQObject), + layout_changed: fn(*mut ProcessesQObject), + data_changed: fn(*mut ProcessesQObject, usize, usize), + begin_reset_model: fn(*mut ProcessesQObject), + end_reset_model: fn(*mut ProcessesQObject), + begin_insert_rows: fn(*mut ProcessesQObject, index: COption, usize, usize), + end_insert_rows: fn(*mut ProcessesQObject), + begin_move_rows: fn(*mut ProcessesQObject, index: COption, usize, usize, dest: COption, usize), + end_move_rows: fn(*mut ProcessesQObject), + begin_remove_rows: fn(*mut ProcessesQObject, index: COption, usize, usize), + end_remove_rows: fn(*mut ProcessesQObject), } impl ProcessesTree { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, index: Option, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, index: Option, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, index.into(), first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, index: Option, first: usize, last: usize, dest: Option, destination: usize) { + pub fn begin_move_rows(&mut self, index: Option, first: usize, last: usize, dest: Option, destination: usize) { (self.begin_move_rows)(self.qobject, index.into(), first, last, dest.into(), destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, index: Option, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, index: Option, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, index.into(), first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -966,19 +1023,19 @@ pub trait ProcessesTrait { #[no_mangle] pub extern "C" fn processes_new( processes: *mut ProcessesQObject, - processes_active_changed: fn(*const ProcessesQObject), - processes_new_data_ready: fn(*const ProcessesQObject, index: COption), - processes_layout_about_to_be_changed: fn(*const ProcessesQObject), - processes_layout_changed: fn(*const ProcessesQObject), - processes_data_changed: fn(*const ProcessesQObject, usize, usize), - processes_begin_reset_model: fn(*const ProcessesQObject), - processes_end_reset_model: fn(*const ProcessesQObject), - processes_begin_insert_rows: fn(*const ProcessesQObject, index: COption, usize, usize), - processes_end_insert_rows: fn(*const ProcessesQObject), - processes_begin_move_rows: fn(*const ProcessesQObject, index: COption, usize, usize, index: COption, usize), - processes_end_move_rows: fn(*const ProcessesQObject), - processes_begin_remove_rows: fn(*const ProcessesQObject, index: COption, usize, usize), - processes_end_remove_rows: fn(*const ProcessesQObject), + processes_active_changed: fn(*mut ProcessesQObject), + processes_new_data_ready: fn(*mut ProcessesQObject, index: COption), + processes_layout_about_to_be_changed: fn(*mut ProcessesQObject), + processes_layout_changed: fn(*mut ProcessesQObject), + processes_data_changed: fn(*mut ProcessesQObject, usize, usize), + processes_begin_reset_model: fn(*mut ProcessesQObject), + processes_end_reset_model: fn(*mut ProcessesQObject), + processes_begin_insert_rows: fn(*mut ProcessesQObject, index: COption, usize, usize), + processes_end_insert_rows: fn(*mut ProcessesQObject), + processes_begin_move_rows: fn(*mut ProcessesQObject, index: COption, usize, usize, index: COption, usize), + processes_end_move_rows: fn(*mut ProcessesQObject), + processes_begin_remove_rows: fn(*mut ProcessesQObject, index: COption, usize, usize), + processes_end_remove_rows: fn(*mut ProcessesQObject), ) -> *mut Processes { let processes_emit = ProcessesEmitter { qobject: Arc::new(AtomicPtr::new(processes)), @@ -1135,20 +1192,31 @@ pub unsafe extern "C" fn processes_data_uid(ptr: *const Processes, index: usize) pub struct TimeSeriesQObject {} -#[derive(Clone)] pub struct TimeSeriesEmitter { qobject: Arc>, - new_data_ready: fn(*const TimeSeriesQObject), + new_data_ready: fn(*mut TimeSeriesQObject), } unsafe impl Send for TimeSeriesEmitter {} impl TimeSeriesEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> TimeSeriesEmitter { + TimeSeriesEmitter { + qobject: self.qobject.clone(), + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const TimeSeriesQObject = null(); self.qobject.store(n as *mut TimeSeriesQObject, Ordering::SeqCst); } - pub fn new_data_ready(&self) { + pub fn new_data_ready(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr); @@ -1158,52 +1226,52 @@ impl TimeSeriesEmitter { #[derive(Clone)] pub struct TimeSeriesList { - qobject: *const TimeSeriesQObject, - layout_about_to_be_changed: fn(*const TimeSeriesQObject), - layout_changed: fn(*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_move_rows: fn(*const TimeSeriesQObject, usize, usize, usize), - end_move_rows: fn(*const TimeSeriesQObject), - begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), - end_remove_rows: fn(*const TimeSeriesQObject), + qobject: *mut TimeSeriesQObject, + layout_about_to_be_changed: fn(*mut TimeSeriesQObject), + layout_changed: fn(*mut TimeSeriesQObject), + data_changed: fn(*mut TimeSeriesQObject, usize, usize), + begin_reset_model: fn(*mut TimeSeriesQObject), + end_reset_model: fn(*mut TimeSeriesQObject), + begin_insert_rows: fn(*mut TimeSeriesQObject, usize, usize), + end_insert_rows: fn(*mut TimeSeriesQObject), + begin_move_rows: fn(*mut TimeSeriesQObject, usize, usize, usize), + end_move_rows: fn(*mut TimeSeriesQObject), + begin_remove_rows: fn(*mut TimeSeriesQObject, usize, usize), + end_remove_rows: fn(*mut TimeSeriesQObject), } impl TimeSeriesList { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, first: usize, last: usize, destination: usize) { + pub fn begin_move_rows(&mut self, first: usize, last: usize, destination: usize) { (self.begin_move_rows)(self.qobject, first, last, destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -1230,18 +1298,18 @@ pub trait TimeSeriesTrait { #[no_mangle] pub extern "C" fn time_series_new( time_series: *mut TimeSeriesQObject, - time_series_new_data_ready: fn(*const TimeSeriesQObject), - time_series_layout_about_to_be_changed: fn(*const TimeSeriesQObject), - time_series_layout_changed: 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_move_rows: fn(*const TimeSeriesQObject, usize, usize, usize), - time_series_end_move_rows: fn(*const TimeSeriesQObject), - time_series_begin_remove_rows: fn(*const TimeSeriesQObject, usize, usize), - time_series_end_remove_rows: fn(*const TimeSeriesQObject), + time_series_new_data_ready: fn(*mut TimeSeriesQObject), + time_series_layout_about_to_be_changed: fn(*mut TimeSeriesQObject), + time_series_layout_changed: fn(*mut TimeSeriesQObject), + time_series_data_changed: fn(*mut TimeSeriesQObject, usize, usize), + time_series_begin_reset_model: fn(*mut TimeSeriesQObject), + time_series_end_reset_model: fn(*mut TimeSeriesQObject), + time_series_begin_insert_rows: fn(*mut TimeSeriesQObject, usize, usize), + time_series_end_insert_rows: fn(*mut TimeSeriesQObject), + time_series_begin_move_rows: fn(*mut TimeSeriesQObject, usize, usize, usize), + time_series_end_move_rows: fn(*mut TimeSeriesQObject), + time_series_begin_remove_rows: fn(*mut TimeSeriesQObject, usize, usize), + time_series_end_remove_rows: fn(*mut TimeSeriesQObject), ) -> *mut TimeSeries { let time_series_emit = TimeSeriesEmitter { qobject: Arc::new(AtomicPtr::new(time_series)), diff --git a/examples/todos/rust/src/interface.rs b/examples/todos/rust/src/interface.rs index da15b89..56aa27a 100644 --- a/examples/todos/rust/src/interface.rs +++ b/examples/todos/rust/src/interface.rs @@ -1,11 +1,10 @@ /* 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::sync::Arc; +use std::sync::atomic::{AtomicPtr, Ordering}; use std::ptr::null; use implementation::*; @@ -17,6 +16,17 @@ pub struct COption { some: bool, } +impl COption { + #![allow(dead_code)] + fn into(self) -> Option { + if self.some { + Some(self.data) + } else { + None + } + } +} + impl From> for COption where T: Default, @@ -42,7 +52,6 @@ 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); @@ -82,71 +91,102 @@ fn to_c_int(n: usize) -> 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), + qobject: Arc>, + active_count_changed: fn(*mut TodosQObject), + count_changed: fn(*mut TodosQObject), + new_data_ready: fn(*mut TodosQObject), } unsafe impl Send for TodosEmitter {} impl TodosEmitter { - fn clear(&self) { - *self.qobject.lock().unwrap() = null(); + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> TodosEmitter { + TodosEmitter { + qobject: self.qobject.clone(), + active_count_changed: self.active_count_changed, + count_changed: self.count_changed, + new_data_ready: self.new_data_ready, + } } - pub fn active_count_changed(&self) { - let ptr = *self.qobject.lock().unwrap(); + fn clear(&self) { + let n: *const TodosQObject = null(); + self.qobject.store(n as *mut TodosQObject, Ordering::SeqCst); + } + pub fn active_count_changed(&mut self) { + let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.active_count_changed)(ptr); } } - pub fn count_changed(&self) { - let ptr = *self.qobject.lock().unwrap(); + pub fn count_changed(&mut self) { + let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.count_changed)(ptr); } } - pub fn new_data_ready(&self) { - let ptr = *self.qobject.lock().unwrap(); + pub fn new_data_ready(&mut self) { + let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr); } } } +#[derive(Clone)] 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), + qobject: *mut TodosQObject, + layout_about_to_be_changed: fn(*mut TodosQObject), + layout_changed: fn(*mut TodosQObject), + data_changed: fn(*mut TodosQObject, usize, usize), + begin_reset_model: fn(*mut TodosQObject), + end_reset_model: fn(*mut TodosQObject), + begin_insert_rows: fn(*mut TodosQObject, usize, usize), + end_insert_rows: fn(*mut TodosQObject), + begin_move_rows: fn(*mut TodosQObject, usize, usize, usize), + end_move_rows: fn(*mut TodosQObject), + begin_remove_rows: fn(*mut TodosQObject, usize, usize), + end_remove_rows: fn(*mut TodosQObject), } impl TodosList { - pub fn data_changed(&self, first: usize, last: usize) { + pub fn layout_about_to_be_changed(&mut self) { + (self.layout_about_to_be_changed)(self.qobject); + } + pub fn layout_changed(&mut self) { + (self.layout_changed)(self.qobject); + } + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_remove_rows(&self, first: usize, last: usize) { + pub fn begin_move_rows(&mut self, first: usize, last: usize, destination: usize) { + (self.begin_move_rows)(self.qobject, first, last, destination); + } + pub fn end_move_rows(&mut self) { + (self.end_move_rows)(self.qobject); + } + pub fn begin_remove_rows(&mut self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -177,30 +217,38 @@ pub trait TodosTrait { #[no_mangle] pub extern "C" fn todos_new( todos: *mut TodosQObject, - todos_active_count_changed: fn(*const TodosQObject), - todos_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), + todos_active_count_changed: fn(*mut TodosQObject), + todos_count_changed: fn(*mut TodosQObject), + todos_new_data_ready: fn(*mut TodosQObject), + todos_layout_about_to_be_changed: fn(*mut TodosQObject), + todos_layout_changed: fn(*mut TodosQObject), + todos_data_changed: fn(*mut TodosQObject, usize, usize), + todos_begin_reset_model: fn(*mut TodosQObject), + todos_end_reset_model: fn(*mut TodosQObject), + todos_begin_insert_rows: fn(*mut TodosQObject, usize, usize), + todos_end_insert_rows: fn(*mut TodosQObject), + todos_begin_move_rows: fn(*mut TodosQObject, usize, usize, usize), + todos_end_move_rows: fn(*mut TodosQObject), + todos_begin_remove_rows: fn(*mut TodosQObject, usize, usize), + todos_end_remove_rows: fn(*mut TodosQObject), ) -> *mut Todos { let todos_emit = TodosEmitter { - qobject: Arc::new(Mutex::new(todos)), + qobject: Arc::new(AtomicPtr::new(todos)), active_count_changed: todos_active_count_changed, count_changed: todos_count_changed, new_data_ready: todos_new_data_ready, }; let model = TodosList { qobject: todos, + layout_about_to_be_changed: todos_layout_about_to_be_changed, + layout_changed: todos_layout_changed, 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_move_rows: todos_begin_move_rows, + end_move_rows: todos_end_move_rows, begin_remove_rows: todos_begin_remove_rows, end_remove_rows: todos_end_remove_rows, }; @@ -224,31 +272,31 @@ pub unsafe extern "C" fn todos_count_get(ptr: *const Todos) -> u64 { } #[no_mangle] -pub extern "C" fn todos_add(ptr: *mut Todos, description_str: *const c_ushort, description_len: c_int) -> () { +pub unsafe 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 o = &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 }; +pub unsafe extern "C" fn todos_clear_completed(ptr: *mut Todos) -> () { + let o = &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 }; +pub unsafe extern "C" fn todos_remove(ptr: *mut Todos, index: u64) -> bool { + let o = &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 }; +pub unsafe extern "C" fn todos_set_all(ptr: *mut Todos, completed: bool) -> () { + let o = &mut *ptr; let r = o.set_all(completed); r } @@ -283,8 +331,8 @@ pub unsafe extern "C" fn todos_sort( } #[no_mangle] -pub extern "C" fn todos_data_completed(ptr: *const Todos, row: c_int) -> bool { - let o = unsafe { &*ptr }; +pub unsafe extern "C" fn todos_data_completed(ptr: *const Todos, row: c_int) -> bool { + let o = &*ptr; o.completed(to_usize(row)).into() } @@ -297,23 +345,23 @@ pub unsafe extern "C" fn todos_set_data_completed( } #[no_mangle] -pub extern "C" fn todos_data_description( +pub unsafe 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 o = &*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( +pub unsafe 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 o = &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/examples/todos/src/Bindings.cpp b/examples/todos/src/Bindings.cpp index 2ca8fc1..e27779b 100644 --- a/examples/todos/src/Bindings.cpp +++ b/examples/todos/src/Bindings.cpp @@ -30,11 +30,11 @@ namespace { } inline void todosActiveCountChanged(Todos* o) { - emit o->activeCountChanged(); + Q_EMIT o->activeCountChanged(); } inline void todosCountChanged(Todos* o) { - emit o->countChanged(); + Q_EMIT o->countChanged(); } } extern "C" { @@ -99,6 +99,7 @@ void Todos::fetchMore(const QModelIndex &parent) todos_fetch_more(m_d); } } +void Todos::updatePersistentIndexes() {} void Todos::sort(int column, Qt::SortOrder order) { @@ -124,7 +125,7 @@ bool Todos::setCompleted(int row, bool value) set = todos_set_data_completed(m_d, row, value); if (set) { QModelIndex index = createIndex(row, 0, row); - emit dataChanged(index, index); + Q_EMIT dataChanged(index, index); } return set; } @@ -142,7 +143,7 @@ bool Todos::setDescription(int row, const QString& value) set = todos_set_data_description(m_d, row, value.utf16(), value.length()); if (set) { QModelIndex index = createIndex(row, 0, row); - emit dataChanged(index, index); + Q_EMIT dataChanged(index, index); } return set; } @@ -158,6 +159,7 @@ QVariant Todos::data(const QModelIndex &index, int role) const case Qt::UserRole + 1: return QVariant::fromValue(description(index.row())); } + break; } return QVariant(); } @@ -216,11 +218,15 @@ bool Todos::setData(const QModelIndex &index, const QVariant &value, int role) extern "C" { Todos::Private* todos_new(Todos*, void (*)(Todos*), void (*)(Todos*), void (*)(const Todos*), + void (*)(Todos*), + void (*)(Todos*), void (*)(Todos*, quintptr, quintptr), void (*)(Todos*), void (*)(Todos*), void (*)(Todos*, int, int), void (*)(Todos*), + void (*)(Todos*, int, int, int), + void (*)(Todos*), void (*)(Todos*, int, int), void (*)(Todos*)); void todos_free(Todos::Private*); @@ -234,7 +240,7 @@ extern "C" { Todos::Todos(bool /*owned*/, QObject *parent): QAbstractItemModel(parent), - m_d(0), + m_d(nullptr), m_ownsPrivate(false) { initHeaderData(); @@ -246,7 +252,14 @@ Todos::Todos(QObject *parent): todosActiveCountChanged, todosCountChanged, [](const Todos* o) { - emit o->newDataReady(QModelIndex()); + Q_EMIT o->newDataReady(QModelIndex()); + }, + [](Todos* o) { + Q_EMIT o->layoutAboutToBeChanged(); + }, + [](Todos* o) { + o->updatePersistentIndexes(); + Q_EMIT o->layoutChanged(); }, [](Todos* o, quintptr first, quintptr last) { o->dataChanged(o->createIndex(first, 0, first), @@ -264,6 +277,12 @@ Todos::Todos(QObject *parent): [](Todos* o) { o->endInsertRows(); }, + [](Todos* o, int first, int last, int destination) { + o->beginMoveRows(QModelIndex(), first, last, QModelIndex(), destination); + }, + [](Todos* o) { + o->endMoveRows(); + }, [](Todos* o, int first, int last) { o->beginRemoveRows(QModelIndex(), first, last); }, diff --git a/examples/todos/src/Bindings.h b/examples/todos/src/Bindings.h index cbd6028..b10e08c 100644 --- a/examples/todos/src/Bindings.h +++ b/examples/todos/src/Bindings.h @@ -2,8 +2,8 @@ #ifndef BINDINGS_H #define BINDINGS_H -#include -#include +#include +#include class Todos; @@ -50,13 +50,14 @@ public: Q_INVOKABLE QString description(int row) const; Q_INVOKABLE bool setDescription(int row, const QString& value); -signals: +Q_SIGNALS: // new data is ready to be made available to the model with fetchMore() void newDataReady(const QModelIndex &parent) const; private: QHash, QVariant> m_headerData; void initHeaderData(); -signals: + void updatePersistentIndexes(); +Q_SIGNALS: void activeCountChanged(); void countChanged(); }; diff --git a/templates/qt_quick/rust/src/interface.rs b/templates/qt_quick/rust/src/interface.rs index 7040580..0a8f8c3 100644 --- a/templates/qt_quick/rust/src/interface.rs +++ b/templates/qt_quick/rust/src/interface.rs @@ -1,11 +1,10 @@ /* 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::sync::Arc; +use std::sync::atomic::{AtomicPtr, Ordering}; use std::ptr::null; use implementation::*; @@ -16,7 +15,6 @@ 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); @@ -42,20 +40,32 @@ fn to_c_int(n: usize) -> c_int { pub struct SimpleQObject {} -#[derive(Clone)] pub struct SimpleEmitter { - qobject: Arc>, - message_changed: fn(*const SimpleQObject), + qobject: Arc>, + message_changed: fn(*mut SimpleQObject), } unsafe impl Send for SimpleEmitter {} impl SimpleEmitter { - fn clear(&self) { - *self.qobject.lock().unwrap() = null(); + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> SimpleEmitter { + SimpleEmitter { + qobject: self.qobject.clone(), + message_changed: self.message_changed, + } } - pub fn message_changed(&self) { - let ptr = *self.qobject.lock().unwrap(); + fn clear(&self) { + let n: *const SimpleQObject = null(); + self.qobject.store(n as *mut SimpleQObject, Ordering::SeqCst); + } + pub fn message_changed(&mut self) { + let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.message_changed)(ptr); } @@ -72,10 +82,10 @@ pub trait SimpleTrait { #[no_mangle] pub extern "C" fn simple_new( simple: *mut SimpleQObject, - simple_message_changed: fn(*const SimpleQObject), + simple_message_changed: fn(*mut SimpleQObject), ) -> *mut Simple { let simple_emit = SimpleEmitter { - qobject: Arc::new(Mutex::new(simple)), + qobject: Arc::new(AtomicPtr::new(simple)), message_changed: simple_message_changed, }; let d_simple = Simple::new(simple_emit); @@ -88,20 +98,20 @@ pub unsafe extern "C" fn simple_free(ptr: *mut Simple) { } #[no_mangle] -pub extern "C" fn simple_message_get( +pub unsafe extern "C" fn simple_message_get( ptr: *const Simple, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { - let o = unsafe { &*ptr }; + let o = &*ptr; let v = o.message(); 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 simple_message_set(ptr: *mut Simple, v: *const c_ushort, len: c_int) { - let o = unsafe { &mut *ptr }; +pub unsafe extern "C" fn simple_message_set(ptr: *mut Simple, v: *const c_ushort, len: c_int) { + let o = &mut *ptr; let mut s = String::new(); set_string_from_utf16(&mut s, v, len); o.set_message(s); diff --git a/templates/qt_quick/src/Bindings.cpp b/templates/qt_quick/src/Bindings.cpp index 8178774..2ffb312 100644 --- a/templates/qt_quick/src/Bindings.cpp +++ b/templates/qt_quick/src/Bindings.cpp @@ -9,7 +9,7 @@ namespace { } inline void simpleMessageChanged(Simple* o) { - emit o->messageChanged(); + Q_EMIT o->messageChanged(); } } extern "C" { @@ -21,7 +21,7 @@ extern "C" { Simple::Simple(bool /*owned*/, QObject *parent): QObject(parent), - m_d(0), + m_d(nullptr), m_ownsPrivate(false) { } diff --git a/templates/qt_quick/src/Bindings.h b/templates/qt_quick/src/Bindings.h index 6d2c6df..1461ede 100644 --- a/templates/qt_quick/src/Bindings.h +++ b/templates/qt_quick/src/Bindings.h @@ -2,8 +2,8 @@ #ifndef BINDINGS_H #define BINDINGS_H -#include -#include +#include +#include class Simple; @@ -22,7 +22,7 @@ public: ~Simple(); QString message() const; void setMessage(const QString& v); -signals: +Q_SIGNALS: void messageChanged(); }; #endif // BINDINGS_H diff --git a/templates/qt_widgets/rust/src/interface.rs b/templates/qt_widgets/rust/src/interface.rs index 7040580..0a8f8c3 100644 --- a/templates/qt_widgets/rust/src/interface.rs +++ b/templates/qt_widgets/rust/src/interface.rs @@ -1,11 +1,10 @@ /* 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::sync::Arc; +use std::sync::atomic::{AtomicPtr, Ordering}; use std::ptr::null; use implementation::*; @@ -16,7 +15,6 @@ 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); @@ -42,20 +40,32 @@ fn to_c_int(n: usize) -> c_int { pub struct SimpleQObject {} -#[derive(Clone)] pub struct SimpleEmitter { - qobject: Arc>, - message_changed: fn(*const SimpleQObject), + qobject: Arc>, + message_changed: fn(*mut SimpleQObject), } unsafe impl Send for SimpleEmitter {} impl SimpleEmitter { - fn clear(&self) { - *self.qobject.lock().unwrap() = null(); + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> SimpleEmitter { + SimpleEmitter { + qobject: self.qobject.clone(), + message_changed: self.message_changed, + } } - pub fn message_changed(&self) { - let ptr = *self.qobject.lock().unwrap(); + fn clear(&self) { + let n: *const SimpleQObject = null(); + self.qobject.store(n as *mut SimpleQObject, Ordering::SeqCst); + } + pub fn message_changed(&mut self) { + let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.message_changed)(ptr); } @@ -72,10 +82,10 @@ pub trait SimpleTrait { #[no_mangle] pub extern "C" fn simple_new( simple: *mut SimpleQObject, - simple_message_changed: fn(*const SimpleQObject), + simple_message_changed: fn(*mut SimpleQObject), ) -> *mut Simple { let simple_emit = SimpleEmitter { - qobject: Arc::new(Mutex::new(simple)), + qobject: Arc::new(AtomicPtr::new(simple)), message_changed: simple_message_changed, }; let d_simple = Simple::new(simple_emit); @@ -88,20 +98,20 @@ pub unsafe extern "C" fn simple_free(ptr: *mut Simple) { } #[no_mangle] -pub extern "C" fn simple_message_get( +pub unsafe extern "C" fn simple_message_get( ptr: *const Simple, p: *mut QString, set: fn(*mut QString, *const c_char, c_int), ) { - let o = unsafe { &*ptr }; + let o = &*ptr; let v = o.message(); 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 simple_message_set(ptr: *mut Simple, v: *const c_ushort, len: c_int) { - let o = unsafe { &mut *ptr }; +pub unsafe extern "C" fn simple_message_set(ptr: *mut Simple, v: *const c_ushort, len: c_int) { + let o = &mut *ptr; let mut s = String::new(); set_string_from_utf16(&mut s, v, len); o.set_message(s); diff --git a/templates/qt_widgets/src/Bindings.cpp b/templates/qt_widgets/src/Bindings.cpp index 8178774..2ffb312 100644 --- a/templates/qt_widgets/src/Bindings.cpp +++ b/templates/qt_widgets/src/Bindings.cpp @@ -9,7 +9,7 @@ namespace { } inline void simpleMessageChanged(Simple* o) { - emit o->messageChanged(); + Q_EMIT o->messageChanged(); } } extern "C" { @@ -21,7 +21,7 @@ extern "C" { Simple::Simple(bool /*owned*/, QObject *parent): QObject(parent), - m_d(0), + m_d(nullptr), m_ownsPrivate(false) { } diff --git a/templates/qt_widgets/src/Bindings.h b/templates/qt_widgets/src/Bindings.h index 6d2c6df..1461ede 100644 --- a/templates/qt_widgets/src/Bindings.h +++ b/templates/qt_widgets/src/Bindings.h @@ -2,8 +2,8 @@ #ifndef BINDINGS_H #define BINDINGS_H -#include -#include +#include +#include class Simple; @@ -22,7 +22,7 @@ public: ~Simple(); QString message() const; void setMessage(const QString& v); -signals: +Q_SIGNALS: void messageChanged(); }; #endif // BINDINGS_H diff --git a/tests/rust_functions/src/interface.rs b/tests/rust_functions/src/interface.rs index 5d46e80..86b3694 100644 --- a/tests/rust_functions/src/interface.rs +++ b/tests/rust_functions/src/interface.rs @@ -43,20 +43,31 @@ fn to_c_int(n: usize) -> c_int { pub struct PersonQObject {} -#[derive(Clone)] pub struct PersonEmitter { qobject: Arc>, - user_name_changed: fn(*const PersonQObject), + user_name_changed: fn(*mut PersonQObject), } unsafe impl Send for PersonEmitter {} impl PersonEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> PersonEmitter { + PersonEmitter { + qobject: self.qobject.clone(), + user_name_changed: self.user_name_changed, + } + } fn clear(&self) { let n: *const PersonQObject = null(); self.qobject.store(n as *mut PersonQObject, Ordering::SeqCst); } - pub fn user_name_changed(&self) { + pub fn user_name_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.user_name_changed)(ptr); @@ -80,7 +91,7 @@ pub trait PersonTrait { #[no_mangle] pub extern "C" fn person_new( person: *mut PersonQObject, - person_user_name_changed: fn(*const PersonQObject), + person_user_name_changed: fn(*mut PersonQObject), ) -> *mut Person { let person_emit = PersonEmitter { qobject: Arc::new(AtomicPtr::new(person)), diff --git a/tests/rust_list/src/interface.rs b/tests/rust_list/src/interface.rs index a71ba68..df1e90c 100644 --- a/tests/rust_list/src/interface.rs +++ b/tests/rust_list/src/interface.rs @@ -91,20 +91,31 @@ fn to_c_int(n: usize) -> c_int { pub struct NoRoleQObject {} -#[derive(Clone)] pub struct NoRoleEmitter { qobject: Arc>, - new_data_ready: fn(*const NoRoleQObject), + new_data_ready: fn(*mut NoRoleQObject), } unsafe impl Send for NoRoleEmitter {} impl NoRoleEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> NoRoleEmitter { + NoRoleEmitter { + qobject: self.qobject.clone(), + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const NoRoleQObject = null(); self.qobject.store(n as *mut NoRoleQObject, Ordering::SeqCst); } - pub fn new_data_ready(&self) { + pub fn new_data_ready(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr); @@ -114,52 +125,52 @@ impl NoRoleEmitter { #[derive(Clone)] pub struct NoRoleList { - qobject: *const NoRoleQObject, - layout_about_to_be_changed: fn(*const NoRoleQObject), - layout_changed: fn(*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_move_rows: fn(*const NoRoleQObject, usize, usize, usize), - end_move_rows: fn(*const NoRoleQObject), - begin_remove_rows: fn(*const NoRoleQObject, usize, usize), - end_remove_rows: fn(*const NoRoleQObject), + qobject: *mut NoRoleQObject, + layout_about_to_be_changed: fn(*mut NoRoleQObject), + layout_changed: fn(*mut NoRoleQObject), + data_changed: fn(*mut NoRoleQObject, usize, usize), + begin_reset_model: fn(*mut NoRoleQObject), + end_reset_model: fn(*mut NoRoleQObject), + begin_insert_rows: fn(*mut NoRoleQObject, usize, usize), + end_insert_rows: fn(*mut NoRoleQObject), + begin_move_rows: fn(*mut NoRoleQObject, usize, usize, usize), + end_move_rows: fn(*mut NoRoleQObject), + begin_remove_rows: fn(*mut NoRoleQObject, usize, usize), + end_remove_rows: fn(*mut NoRoleQObject), } impl NoRoleList { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, first: usize, last: usize, destination: usize) { + pub fn begin_move_rows(&mut self, first: usize, last: usize, destination: usize) { (self.begin_move_rows)(self.qobject, first, last, destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -184,18 +195,18 @@ pub trait NoRoleTrait { #[no_mangle] pub extern "C" fn no_role_new( no_role: *mut NoRoleQObject, - no_role_new_data_ready: fn(*const NoRoleQObject), - no_role_layout_about_to_be_changed: fn(*const NoRoleQObject), - no_role_layout_changed: 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_move_rows: fn(*const NoRoleQObject, usize, usize, usize), - no_role_end_move_rows: fn(*const NoRoleQObject), - no_role_begin_remove_rows: fn(*const NoRoleQObject, usize, usize), - no_role_end_remove_rows: fn(*const NoRoleQObject), + no_role_new_data_ready: fn(*mut NoRoleQObject), + no_role_layout_about_to_be_changed: fn(*mut NoRoleQObject), + no_role_layout_changed: fn(*mut NoRoleQObject), + no_role_data_changed: fn(*mut NoRoleQObject, usize, usize), + no_role_begin_reset_model: fn(*mut NoRoleQObject), + no_role_end_reset_model: fn(*mut NoRoleQObject), + no_role_begin_insert_rows: fn(*mut NoRoleQObject, usize, usize), + no_role_end_insert_rows: fn(*mut NoRoleQObject), + no_role_begin_move_rows: fn(*mut NoRoleQObject, usize, usize, usize), + no_role_end_move_rows: fn(*mut NoRoleQObject), + no_role_begin_remove_rows: fn(*mut NoRoleQObject, usize, usize), + no_role_end_remove_rows: fn(*mut NoRoleQObject), ) -> *mut NoRole { let no_role_emit = NoRoleEmitter { qobject: Arc::new(AtomicPtr::new(no_role)), @@ -292,20 +303,31 @@ pub unsafe extern "C" fn no_role_set_data_user_name( pub struct PersonsQObject {} -#[derive(Clone)] pub struct PersonsEmitter { qobject: Arc>, - new_data_ready: fn(*const PersonsQObject), + new_data_ready: fn(*mut PersonsQObject), } unsafe impl Send for PersonsEmitter {} impl PersonsEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> PersonsEmitter { + PersonsEmitter { + qobject: self.qobject.clone(), + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const PersonsQObject = null(); self.qobject.store(n as *mut PersonsQObject, Ordering::SeqCst); } - pub fn new_data_ready(&self) { + pub fn new_data_ready(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr); @@ -315,52 +337,52 @@ impl PersonsEmitter { #[derive(Clone)] pub struct PersonsList { - qobject: *const PersonsQObject, - layout_about_to_be_changed: fn(*const PersonsQObject), - layout_changed: fn(*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_move_rows: fn(*const PersonsQObject, usize, usize, usize), - end_move_rows: fn(*const PersonsQObject), - begin_remove_rows: fn(*const PersonsQObject, usize, usize), - end_remove_rows: fn(*const PersonsQObject), + qobject: *mut PersonsQObject, + layout_about_to_be_changed: fn(*mut PersonsQObject), + layout_changed: fn(*mut PersonsQObject), + data_changed: fn(*mut PersonsQObject, usize, usize), + begin_reset_model: fn(*mut PersonsQObject), + end_reset_model: fn(*mut PersonsQObject), + begin_insert_rows: fn(*mut PersonsQObject, usize, usize), + end_insert_rows: fn(*mut PersonsQObject), + begin_move_rows: fn(*mut PersonsQObject, usize, usize, usize), + end_move_rows: fn(*mut PersonsQObject), + begin_remove_rows: fn(*mut PersonsQObject, usize, usize), + end_remove_rows: fn(*mut PersonsQObject), } impl PersonsList { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, first: usize, last: usize, destination: usize) { + pub fn begin_move_rows(&mut self, first: usize, last: usize, destination: usize) { (self.begin_move_rows)(self.qobject, first, last, destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -383,18 +405,18 @@ pub trait PersonsTrait { #[no_mangle] pub extern "C" fn persons_new( persons: *mut PersonsQObject, - persons_new_data_ready: fn(*const PersonsQObject), - persons_layout_about_to_be_changed: fn(*const PersonsQObject), - persons_layout_changed: 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_move_rows: fn(*const PersonsQObject, usize, usize, usize), - persons_end_move_rows: fn(*const PersonsQObject), - persons_begin_remove_rows: fn(*const PersonsQObject, usize, usize), - persons_end_remove_rows: fn(*const PersonsQObject), + persons_new_data_ready: fn(*mut PersonsQObject), + persons_layout_about_to_be_changed: fn(*mut PersonsQObject), + persons_layout_changed: fn(*mut PersonsQObject), + persons_data_changed: fn(*mut PersonsQObject, usize, usize), + persons_begin_reset_model: fn(*mut PersonsQObject), + persons_end_reset_model: fn(*mut PersonsQObject), + persons_begin_insert_rows: fn(*mut PersonsQObject, usize, usize), + persons_end_insert_rows: fn(*mut PersonsQObject), + persons_begin_move_rows: fn(*mut PersonsQObject, usize, usize, usize), + persons_end_move_rows: fn(*mut PersonsQObject), + persons_begin_remove_rows: fn(*mut PersonsQObject, usize, usize), + persons_end_remove_rows: fn(*mut PersonsQObject), ) -> *mut Persons { let persons_emit = PersonsEmitter { qobject: Arc::new(AtomicPtr::new(persons)), diff --git a/tests/rust_list_types/src/interface.rs b/tests/rust_list_types/src/interface.rs index cd91c84..c8be099 100644 --- a/tests/rust_list_types/src/interface.rs +++ b/tests/rust_list_types/src/interface.rs @@ -94,20 +94,31 @@ fn to_c_int(n: usize) -> c_int { pub struct ListQObject {} -#[derive(Clone)] pub struct ListEmitter { qobject: Arc>, - new_data_ready: fn(*const ListQObject), + new_data_ready: fn(*mut ListQObject), } unsafe impl Send for ListEmitter {} impl ListEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> ListEmitter { + ListEmitter { + qobject: self.qobject.clone(), + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const ListQObject = null(); self.qobject.store(n as *mut ListQObject, Ordering::SeqCst); } - pub fn new_data_ready(&self) { + pub fn new_data_ready(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr); @@ -117,52 +128,52 @@ impl ListEmitter { #[derive(Clone)] pub struct ListList { - qobject: *const ListQObject, - layout_about_to_be_changed: fn(*const ListQObject), - layout_changed: fn(*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_move_rows: fn(*const ListQObject, usize, usize, usize), - end_move_rows: fn(*const ListQObject), - begin_remove_rows: fn(*const ListQObject, usize, usize), - end_remove_rows: fn(*const ListQObject), + qobject: *mut ListQObject, + layout_about_to_be_changed: fn(*mut ListQObject), + layout_changed: fn(*mut ListQObject), + data_changed: fn(*mut ListQObject, usize, usize), + begin_reset_model: fn(*mut ListQObject), + end_reset_model: fn(*mut ListQObject), + begin_insert_rows: fn(*mut ListQObject, usize, usize), + end_insert_rows: fn(*mut ListQObject), + begin_move_rows: fn(*mut ListQObject, usize, usize, usize), + end_move_rows: fn(*mut ListQObject), + begin_remove_rows: fn(*mut ListQObject, usize, usize), + end_remove_rows: fn(*mut ListQObject), } impl ListList { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, first: usize, last: usize, destination: usize) { + pub fn begin_move_rows(&mut self, first: usize, last: usize, destination: usize) { (self.begin_move_rows)(self.qobject, first, last, destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -215,18 +226,18 @@ pub trait ListTrait { #[no_mangle] pub extern "C" fn list_new( list: *mut ListQObject, - list_new_data_ready: fn(*const ListQObject), - list_layout_about_to_be_changed: fn(*const ListQObject), - list_layout_changed: 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_move_rows: fn(*const ListQObject, usize, usize, usize), - list_end_move_rows: fn(*const ListQObject), - list_begin_remove_rows: fn(*const ListQObject, usize, usize), - list_end_remove_rows: fn(*const ListQObject), + list_new_data_ready: fn(*mut ListQObject), + list_layout_about_to_be_changed: fn(*mut ListQObject), + list_layout_changed: fn(*mut ListQObject), + list_data_changed: fn(*mut ListQObject, usize, usize), + list_begin_reset_model: fn(*mut ListQObject), + list_end_reset_model: fn(*mut ListQObject), + list_begin_insert_rows: fn(*mut ListQObject, usize, usize), + list_end_insert_rows: fn(*mut ListQObject), + list_begin_move_rows: fn(*mut ListQObject, usize, usize, usize), + list_end_move_rows: fn(*mut ListQObject), + list_begin_remove_rows: fn(*mut ListQObject, usize, usize), + list_end_remove_rows: fn(*mut ListQObject), ) -> *mut List { let list_emit = ListEmitter { qobject: Arc::new(AtomicPtr::new(list)), diff --git a/tests/rust_object/src/interface.rs b/tests/rust_object/src/interface.rs index 7cfe940..583c151 100644 --- a/tests/rust_object/src/interface.rs +++ b/tests/rust_object/src/interface.rs @@ -40,20 +40,31 @@ fn to_c_int(n: usize) -> c_int { pub struct PersonQObject {} -#[derive(Clone)] pub struct PersonEmitter { qobject: Arc>, - user_name_changed: fn(*const PersonQObject), + user_name_changed: fn(*mut PersonQObject), } unsafe impl Send for PersonEmitter {} impl PersonEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> PersonEmitter { + PersonEmitter { + qobject: self.qobject.clone(), + user_name_changed: self.user_name_changed, + } + } fn clear(&self) { let n: *const PersonQObject = null(); self.qobject.store(n as *mut PersonQObject, Ordering::SeqCst); } - pub fn user_name_changed(&self) { + pub fn user_name_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.user_name_changed)(ptr); @@ -71,7 +82,7 @@ pub trait PersonTrait { #[no_mangle] pub extern "C" fn person_new( person: *mut PersonQObject, - person_user_name_changed: fn(*const PersonQObject), + person_user_name_changed: fn(*mut PersonQObject), ) -> *mut Person { let person_emit = PersonEmitter { qobject: Arc::new(AtomicPtr::new(person)), diff --git a/tests/rust_object_types/src/interface.rs b/tests/rust_object_types/src/interface.rs index 751c354..659f572 100644 --- a/tests/rust_object_types/src/interface.rs +++ b/tests/rust_object_types/src/interface.rs @@ -80,139 +80,167 @@ fn to_c_int(n: usize) -> c_int { pub struct ObjectQObject {} -#[derive(Clone)] pub struct ObjectEmitter { qobject: Arc>, - boolean_changed: fn(*const ObjectQObject), - bytearray_changed: fn(*const ObjectQObject), - f32_changed: fn(*const ObjectQObject), - f64_changed: fn(*const ObjectQObject), - i16_changed: fn(*const ObjectQObject), - i32_changed: fn(*const ObjectQObject), - i64_changed: fn(*const ObjectQObject), - i8_changed: fn(*const ObjectQObject), - optional_boolean_changed: fn(*const ObjectQObject), - optional_bytearray_changed: fn(*const ObjectQObject), - optional_string_changed: fn(*const ObjectQObject), - optional_u64_changed: fn(*const ObjectQObject), - string_changed: fn(*const ObjectQObject), - string_by_function_changed: fn(*const ObjectQObject), - u16_changed: fn(*const ObjectQObject), - u32_changed: fn(*const ObjectQObject), - u64_changed: fn(*const ObjectQObject), - u8_changed: fn(*const ObjectQObject), + boolean_changed: fn(*mut ObjectQObject), + bytearray_changed: fn(*mut ObjectQObject), + f32_changed: fn(*mut ObjectQObject), + f64_changed: fn(*mut ObjectQObject), + i16_changed: fn(*mut ObjectQObject), + i32_changed: fn(*mut ObjectQObject), + i64_changed: fn(*mut ObjectQObject), + i8_changed: fn(*mut ObjectQObject), + optional_boolean_changed: fn(*mut ObjectQObject), + optional_bytearray_changed: fn(*mut ObjectQObject), + optional_string_changed: fn(*mut ObjectQObject), + optional_u64_changed: fn(*mut ObjectQObject), + string_changed: fn(*mut ObjectQObject), + string_by_function_changed: fn(*mut ObjectQObject), + u16_changed: fn(*mut ObjectQObject), + u32_changed: fn(*mut ObjectQObject), + u64_changed: fn(*mut ObjectQObject), + u8_changed: fn(*mut ObjectQObject), } unsafe impl Send for ObjectEmitter {} impl ObjectEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> ObjectEmitter { + ObjectEmitter { + qobject: self.qobject.clone(), + boolean_changed: self.boolean_changed, + bytearray_changed: self.bytearray_changed, + f32_changed: self.f32_changed, + f64_changed: self.f64_changed, + i16_changed: self.i16_changed, + i32_changed: self.i32_changed, + i64_changed: self.i64_changed, + i8_changed: self.i8_changed, + optional_boolean_changed: self.optional_boolean_changed, + optional_bytearray_changed: self.optional_bytearray_changed, + optional_string_changed: self.optional_string_changed, + optional_u64_changed: self.optional_u64_changed, + string_changed: self.string_changed, + string_by_function_changed: self.string_by_function_changed, + u16_changed: self.u16_changed, + u32_changed: self.u32_changed, + u64_changed: self.u64_changed, + u8_changed: self.u8_changed, + } + } fn clear(&self) { let n: *const ObjectQObject = null(); self.qobject.store(n as *mut ObjectQObject, Ordering::SeqCst); } - pub fn boolean_changed(&self) { + pub fn boolean_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.boolean_changed)(ptr); } } - pub fn bytearray_changed(&self) { + pub fn bytearray_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.bytearray_changed)(ptr); } } - pub fn f32_changed(&self) { + pub fn f32_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.f32_changed)(ptr); } } - pub fn f64_changed(&self) { + pub fn f64_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.f64_changed)(ptr); } } - pub fn i16_changed(&self) { + pub fn i16_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.i16_changed)(ptr); } } - pub fn i32_changed(&self) { + pub fn i32_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.i32_changed)(ptr); } } - pub fn i64_changed(&self) { + pub fn i64_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.i64_changed)(ptr); } } - pub fn i8_changed(&self) { + pub fn i8_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.i8_changed)(ptr); } } - pub fn optional_boolean_changed(&self) { + pub fn optional_boolean_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.optional_boolean_changed)(ptr); } } - pub fn optional_bytearray_changed(&self) { + pub fn optional_bytearray_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.optional_bytearray_changed)(ptr); } } - pub fn optional_string_changed(&self) { + pub fn optional_string_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.optional_string_changed)(ptr); } } - pub fn optional_u64_changed(&self) { + pub fn optional_u64_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.optional_u64_changed)(ptr); } } - pub fn string_changed(&self) { + pub fn string_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.string_changed)(ptr); } } - pub fn string_by_function_changed(&self) { + pub fn string_by_function_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.string_by_function_changed)(ptr); } } - pub fn u16_changed(&self) { + pub fn u16_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.u16_changed)(ptr); } } - pub fn u32_changed(&self) { + pub fn u32_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.u32_changed)(ptr); } } - pub fn u64_changed(&self) { + pub fn u64_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.u64_changed)(ptr); } } - pub fn u8_changed(&self) { + pub fn u8_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.u8_changed)(ptr); @@ -263,24 +291,24 @@ pub trait ObjectTrait { #[no_mangle] pub extern "C" fn object_new( object: *mut ObjectQObject, - object_boolean_changed: fn(*const ObjectQObject), - object_bytearray_changed: fn(*const ObjectQObject), - object_f32_changed: fn(*const ObjectQObject), - object_f64_changed: fn(*const ObjectQObject), - object_i16_changed: fn(*const ObjectQObject), - object_i32_changed: fn(*const ObjectQObject), - object_i64_changed: fn(*const ObjectQObject), - object_i8_changed: fn(*const ObjectQObject), - object_optional_boolean_changed: fn(*const ObjectQObject), - object_optional_bytearray_changed: fn(*const ObjectQObject), - object_optional_string_changed: fn(*const ObjectQObject), - object_optional_u64_changed: fn(*const ObjectQObject), - object_string_changed: fn(*const ObjectQObject), - object_string_by_function_changed: fn(*const ObjectQObject), - object_u16_changed: fn(*const ObjectQObject), - object_u32_changed: fn(*const ObjectQObject), - object_u64_changed: fn(*const ObjectQObject), - object_u8_changed: fn(*const ObjectQObject), + object_boolean_changed: fn(*mut ObjectQObject), + object_bytearray_changed: fn(*mut ObjectQObject), + object_f32_changed: fn(*mut ObjectQObject), + object_f64_changed: fn(*mut ObjectQObject), + object_i16_changed: fn(*mut ObjectQObject), + object_i32_changed: fn(*mut ObjectQObject), + object_i64_changed: fn(*mut ObjectQObject), + object_i8_changed: fn(*mut ObjectQObject), + object_optional_boolean_changed: fn(*mut ObjectQObject), + object_optional_bytearray_changed: fn(*mut ObjectQObject), + object_optional_string_changed: fn(*mut ObjectQObject), + object_optional_u64_changed: fn(*mut ObjectQObject), + object_string_changed: fn(*mut ObjectQObject), + object_string_by_function_changed: fn(*mut ObjectQObject), + object_u16_changed: fn(*mut ObjectQObject), + object_u32_changed: fn(*mut ObjectQObject), + object_u64_changed: fn(*mut ObjectQObject), + object_u8_changed: fn(*mut ObjectQObject), ) -> *mut Object { let object_emit = ObjectEmitter { qobject: Arc::new(AtomicPtr::new(object)), diff --git a/tests/rust_objects/src/interface.rs b/tests/rust_objects/src/interface.rs index 8c445c8..f3f9efe 100644 --- a/tests/rust_objects/src/interface.rs +++ b/tests/rust_objects/src/interface.rs @@ -40,7 +40,6 @@ fn to_c_int(n: usize) -> c_int { pub struct GroupQObject {} -#[derive(Clone)] pub struct GroupEmitter { qobject: Arc>, } @@ -48,6 +47,17 @@ pub struct GroupEmitter { unsafe impl Send for GroupEmitter {} impl GroupEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> GroupEmitter { + GroupEmitter { + qobject: self.qobject.clone(), + } + } fn clear(&self) { let n: *const GroupQObject = null(); self.qobject.store(n as *mut GroupQObject, Ordering::SeqCst); @@ -67,7 +77,7 @@ pub extern "C" fn group_new( group: *mut GroupQObject, person: *mut PersonQObject, object: *mut InnerObjectQObject, - object_description_changed: fn(*const InnerObjectQObject), + object_description_changed: fn(*mut InnerObjectQObject), ) -> *mut Group { let object_emit = InnerObjectEmitter { qobject: Arc::new(AtomicPtr::new(object)), @@ -99,20 +109,31 @@ pub unsafe extern "C" fn group_person_get(ptr: *mut Group) -> *mut Person { pub struct InnerObjectQObject {} -#[derive(Clone)] pub struct InnerObjectEmitter { qobject: Arc>, - description_changed: fn(*const InnerObjectQObject), + description_changed: fn(*mut InnerObjectQObject), } unsafe impl Send for InnerObjectEmitter {} impl InnerObjectEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> InnerObjectEmitter { + InnerObjectEmitter { + qobject: self.qobject.clone(), + description_changed: self.description_changed, + } + } fn clear(&self) { let n: *const InnerObjectQObject = null(); self.qobject.store(n as *mut InnerObjectQObject, Ordering::SeqCst); } - pub fn description_changed(&self) { + pub fn description_changed(&mut self) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.description_changed)(ptr); @@ -130,7 +151,7 @@ pub trait InnerObjectTrait { #[no_mangle] pub extern "C" fn inner_object_new( inner_object: *mut InnerObjectQObject, - inner_object_description_changed: fn(*const InnerObjectQObject), + inner_object_description_changed: fn(*mut InnerObjectQObject), ) -> *mut InnerObject { let inner_object_emit = InnerObjectEmitter { qobject: Arc::new(AtomicPtr::new(inner_object)), @@ -167,7 +188,6 @@ pub unsafe extern "C" fn inner_object_description_set(ptr: *mut InnerObject, v: pub struct PersonQObject {} -#[derive(Clone)] pub struct PersonEmitter { qobject: Arc>, } @@ -175,6 +195,17 @@ pub struct PersonEmitter { unsafe impl Send for PersonEmitter {} impl PersonEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> PersonEmitter { + PersonEmitter { + qobject: self.qobject.clone(), + } + } fn clear(&self) { let n: *const PersonQObject = null(); self.qobject.store(n as *mut PersonQObject, Ordering::SeqCst); @@ -193,7 +224,7 @@ pub trait PersonTrait { pub extern "C" fn person_new( person: *mut PersonQObject, object: *mut InnerObjectQObject, - object_description_changed: fn(*const InnerObjectQObject), + object_description_changed: fn(*mut InnerObjectQObject), ) -> *mut Person { let object_emit = InnerObjectEmitter { qobject: Arc::new(AtomicPtr::new(object)), diff --git a/tests/rust_tree/src/interface.rs b/tests/rust_tree/src/interface.rs index af227ca..79b18a4 100644 --- a/tests/rust_tree/src/interface.rs +++ b/tests/rust_tree/src/interface.rs @@ -91,20 +91,31 @@ fn to_c_int(n: usize) -> c_int { pub struct PersonsQObject {} -#[derive(Clone)] pub struct PersonsEmitter { qobject: Arc>, - new_data_ready: fn(*const PersonsQObject, index: COption), + new_data_ready: fn(*mut PersonsQObject, index: COption), } unsafe impl Send for PersonsEmitter {} impl PersonsEmitter { + /// Clone the emitter + /// + /// The emitter can only be cloned when it is mutable. The emitter calls + /// into C++ code which may call into Rust again. If emmitting is possible + /// from immutable structures, that might lead to access to a mutable + /// reference. That is undefined behaviour and forbidden. + pub fn clone(&mut self) -> PersonsEmitter { + PersonsEmitter { + qobject: self.qobject.clone(), + new_data_ready: self.new_data_ready, + } + } fn clear(&self) { let n: *const PersonsQObject = null(); self.qobject.store(n as *mut PersonsQObject, Ordering::SeqCst); } - pub fn new_data_ready(&self, item: Option) { + pub fn new_data_ready(&mut self, item: Option) { let ptr = self.qobject.load(Ordering::SeqCst); if !ptr.is_null() { (self.new_data_ready)(ptr, item.into()); @@ -114,52 +125,52 @@ impl PersonsEmitter { #[derive(Clone)] pub struct PersonsTree { - qobject: *const PersonsQObject, - layout_about_to_be_changed: fn(*const PersonsQObject), - layout_changed: fn(*const PersonsQObject), - data_changed: fn(*const PersonsQObject, usize, usize), - begin_reset_model: fn(*const PersonsQObject), - end_reset_model: fn(*const PersonsQObject), - begin_insert_rows: fn(*const PersonsQObject, index: COption, usize, usize), - end_insert_rows: fn(*const PersonsQObject), - begin_move_rows: fn(*const PersonsQObject, index: COption, usize, usize, dest: COption, usize), - end_move_rows: fn(*const PersonsQObject), - begin_remove_rows: fn(*const PersonsQObject, index: COption, usize, usize), - end_remove_rows: fn(*const PersonsQObject), + qobject: *mut PersonsQObject, + layout_about_to_be_changed: fn(*mut PersonsQObject), + layout_changed: fn(*mut PersonsQObject), + data_changed: fn(*mut PersonsQObject, usize, usize), + begin_reset_model: fn(*mut PersonsQObject), + end_reset_model: fn(*mut PersonsQObject), + begin_insert_rows: fn(*mut PersonsQObject, index: COption, usize, usize), + end_insert_rows: fn(*mut PersonsQObject), + begin_move_rows: fn(*mut PersonsQObject, index: COption, usize, usize, dest: COption, usize), + end_move_rows: fn(*mut PersonsQObject), + begin_remove_rows: fn(*mut PersonsQObject, index: COption, usize, usize), + end_remove_rows: fn(*mut PersonsQObject), } impl PersonsTree { - pub fn layout_about_to_be_changed(&self) { + pub fn layout_about_to_be_changed(&mut self) { (self.layout_about_to_be_changed)(self.qobject); } - pub fn layout_changed(&self) { + pub fn layout_changed(&mut self) { (self.layout_changed)(self.qobject); } - pub fn data_changed(&self, first: usize, last: usize) { + pub fn data_changed(&mut self, first: usize, last: usize) { (self.data_changed)(self.qobject, first, last); } - pub fn begin_reset_model(&self) { + pub fn begin_reset_model(&mut self) { (self.begin_reset_model)(self.qobject); } - pub fn end_reset_model(&self) { + pub fn end_reset_model(&mut self) { (self.end_reset_model)(self.qobject); } - pub fn begin_insert_rows(&self, index: Option, first: usize, last: usize) { + pub fn begin_insert_rows(&mut self, index: Option, first: usize, last: usize) { (self.begin_insert_rows)(self.qobject, index.into(), first, last); } - pub fn end_insert_rows(&self) { + pub fn end_insert_rows(&mut self) { (self.end_insert_rows)(self.qobject); } - pub fn begin_move_rows(&self, index: Option, first: usize, last: usize, dest: Option, destination: usize) { + pub fn begin_move_rows(&mut self, index: Option, first: usize, last: usize, dest: Option, destination: usize) { (self.begin_move_rows)(self.qobject, index.into(), first, last, dest.into(), destination); } - pub fn end_move_rows(&self) { + pub fn end_move_rows(&mut self) { (self.end_move_rows)(self.qobject); } - pub fn begin_remove_rows(&self, index: Option, first: usize, last: usize) { + pub fn begin_remove_rows(&mut self, index: Option, first: usize, last: usize) { (self.begin_remove_rows)(self.qobject, index.into(), first, last); } - pub fn end_remove_rows(&self) { + pub fn end_remove_rows(&mut self) { (self.end_remove_rows)(self.qobject); } } @@ -184,18 +195,18 @@ pub trait PersonsTrait { #[no_mangle] pub extern "C" fn persons_new( persons: *mut PersonsQObject, - persons_new_data_ready: fn(*const PersonsQObject, index: COption), - persons_layout_about_to_be_changed: fn(*const PersonsQObject), - persons_layout_changed: 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, index: COption, usize, usize), - persons_end_insert_rows: fn(*const PersonsQObject), - persons_begin_move_rows: fn(*const PersonsQObject, index: COption, usize, usize, index: COption, usize), - persons_end_move_rows: fn(*const PersonsQObject), - persons_begin_remove_rows: fn(*const PersonsQObject, index: COption, usize, usize), - persons_end_remove_rows: fn(*const PersonsQObject), + persons_new_data_ready: fn(*mut PersonsQObject, index: COption), + persons_layout_about_to_be_changed: fn(*mut PersonsQObject), + persons_layout_changed: fn(*mut PersonsQObject), + persons_data_changed: fn(*mut PersonsQObject, usize, usize), + persons_begin_reset_model: fn(*mut PersonsQObject), + persons_end_reset_model: fn(*mut PersonsQObject), + persons_begin_insert_rows: fn(*mut PersonsQObject, index: COption, usize, usize), + persons_end_insert_rows: fn(*mut PersonsQObject), + persons_begin_move_rows: fn(*mut PersonsQObject, index: COption, usize, usize, index: COption, usize), + persons_end_move_rows: fn(*mut PersonsQObject), + persons_begin_remove_rows: fn(*mut PersonsQObject, index: COption, usize, usize), + persons_end_remove_rows: fn(*mut PersonsQObject), ) -> *mut Persons { let persons_emit = PersonsEmitter { qobject: Arc::new(AtomicPtr::new(persons)),