Warning, /sdk/rust-qt-binding-generator/tests/rust_tree/src/interface.rs is written in an unsupported language. File is not indexed.
0001 /* generated by rust_qt_binding_generator */
0002 use libc::{c_char, c_ushort, c_int};
0003 use std::slice;
0004 use std::char::decode_utf16;
0005
0006 use std::sync::Arc;
0007 use std::sync::atomic::{AtomicPtr, Ordering};
0008 use std::ptr::null;
0009
0010 use implementation::*;
0011
0012
0013 #[repr(C)]
0014 pub struct COption<T> {
0015 data: T,
0016 some: bool,
0017 }
0018
0019 impl<T> COption<T> {
0020 #![allow(dead_code)]
0021 fn into(self) -> Option<T> {
0022 if self.some {
0023 Some(self.data)
0024 } else {
0025 None
0026 }
0027 }
0028 }
0029
0030 impl<T> From<Option<T>> for COption<T>
0031 where
0032 T: Default,
0033 {
0034 fn from(t: Option<T>) -> COption<T> {
0035 if let Some(v) = t {
0036 COption {
0037 data: v,
0038 some: true,
0039 }
0040 } else {
0041 COption {
0042 data: T::default(),
0043 some: false,
0044 }
0045 }
0046 }
0047 }
0048
0049
0050 pub enum QString {}
0051
0052 fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) {
0053 let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) };
0054 let characters = decode_utf16(utf16.iter().cloned())
0055 .map(|r| r.unwrap());
0056 s.clear();
0057 s.extend(characters);
0058 }
0059
0060
0061
0062 #[repr(C)]
0063 #[derive(PartialEq, Eq, Debug)]
0064 pub enum SortOrder {
0065 Ascending = 0,
0066 Descending = 1,
0067 }
0068
0069 #[repr(C)]
0070 pub struct QModelIndex {
0071 row: c_int,
0072 internal_id: usize,
0073 }
0074
0075
0076 fn to_usize(n: c_int) -> usize {
0077 if n < 0 {
0078 panic!("Cannot cast {} to usize", n);
0079 }
0080 n as usize
0081 }
0082
0083
0084 fn to_c_int(n: usize) -> c_int {
0085 if n > c_int::max_value() as usize {
0086 panic!("Cannot cast {} to c_int", n);
0087 }
0088 n as c_int
0089 }
0090
0091
0092 pub struct PersonsQObject {}
0093
0094 pub struct PersonsEmitter {
0095 qobject: Arc<AtomicPtr<PersonsQObject>>,
0096 new_data_ready: extern fn(*mut PersonsQObject, index: COption<usize>),
0097 }
0098
0099 unsafe impl Send for PersonsEmitter {}
0100
0101 impl PersonsEmitter {
0102 /// Clone the emitter
0103 ///
0104 /// The emitter can only be cloned when it is mutable. The emitter calls
0105 /// into C++ code which may call into Rust again. If emmitting is possible
0106 /// from immutable structures, that might lead to access to a mutable
0107 /// reference. That is undefined behaviour and forbidden.
0108 pub fn clone(&mut self) -> PersonsEmitter {
0109 PersonsEmitter {
0110 qobject: self.qobject.clone(),
0111 new_data_ready: self.new_data_ready,
0112 }
0113 }
0114 fn clear(&self) {
0115 let n: *const PersonsQObject = null();
0116 self.qobject.store(n as *mut PersonsQObject, Ordering::SeqCst);
0117 }
0118 pub fn new_data_ready(&mut self, item: Option<usize>) {
0119 let ptr = self.qobject.load(Ordering::SeqCst);
0120 if !ptr.is_null() {
0121 (self.new_data_ready)(ptr, item.into());
0122 }
0123 }
0124 }
0125
0126 #[derive(Clone)]
0127 pub struct PersonsTree {
0128 qobject: *mut PersonsQObject,
0129 layout_about_to_be_changed: extern fn(*mut PersonsQObject),
0130 layout_changed: extern fn(*mut PersonsQObject),
0131 data_changed: extern fn(*mut PersonsQObject, usize, usize),
0132 begin_reset_model: extern fn(*mut PersonsQObject),
0133 end_reset_model: extern fn(*mut PersonsQObject),
0134 begin_insert_rows: extern fn(*mut PersonsQObject, index: COption<usize>, usize, usize),
0135 end_insert_rows: extern fn(*mut PersonsQObject),
0136 begin_move_rows: extern fn(*mut PersonsQObject, index: COption<usize>, usize, usize, dest: COption<usize>, usize),
0137 end_move_rows: extern fn(*mut PersonsQObject),
0138 begin_remove_rows: extern fn(*mut PersonsQObject, index: COption<usize>, usize, usize),
0139 end_remove_rows: extern fn(*mut PersonsQObject),
0140 }
0141
0142 impl PersonsTree {
0143 pub fn layout_about_to_be_changed(&mut self) {
0144 (self.layout_about_to_be_changed)(self.qobject);
0145 }
0146 pub fn layout_changed(&mut self) {
0147 (self.layout_changed)(self.qobject);
0148 }
0149 pub fn data_changed(&mut self, first: usize, last: usize) {
0150 (self.data_changed)(self.qobject, first, last);
0151 }
0152 pub fn begin_reset_model(&mut self) {
0153 (self.begin_reset_model)(self.qobject);
0154 }
0155 pub fn end_reset_model(&mut self) {
0156 (self.end_reset_model)(self.qobject);
0157 }
0158 pub fn begin_insert_rows(&mut self, index: Option<usize>, first: usize, last: usize) {
0159 (self.begin_insert_rows)(self.qobject, index.into(), first, last);
0160 }
0161 pub fn end_insert_rows(&mut self) {
0162 (self.end_insert_rows)(self.qobject);
0163 }
0164 pub fn begin_move_rows(&mut self, index: Option<usize>, first: usize, last: usize, dest: Option<usize>, destination: usize) {
0165 (self.begin_move_rows)(self.qobject, index.into(), first, last, dest.into(), destination);
0166 }
0167 pub fn end_move_rows(&mut self) {
0168 (self.end_move_rows)(self.qobject);
0169 }
0170 pub fn begin_remove_rows(&mut self, index: Option<usize>, first: usize, last: usize) {
0171 (self.begin_remove_rows)(self.qobject, index.into(), first, last);
0172 }
0173 pub fn end_remove_rows(&mut self) {
0174 (self.end_remove_rows)(self.qobject);
0175 }
0176 }
0177
0178 pub trait PersonsTrait {
0179 fn new(emit: PersonsEmitter, model: PersonsTree) -> Self;
0180 fn emit(&mut self) -> &mut PersonsEmitter;
0181 fn row_count(&self, _: Option<usize>) -> usize;
0182 fn can_fetch_more(&self, _: Option<usize>) -> bool {
0183 false
0184 }
0185 fn fetch_more(&mut self, _: Option<usize>) {}
0186 fn sort(&mut self, _: u8, _: SortOrder) {}
0187 fn check_row(&self, index: usize, row: usize) -> Option<usize>;
0188 fn index(&self, item: Option<usize>, row: usize) -> usize;
0189 fn parent(&self, index: usize) -> Option<usize>;
0190 fn row(&self, index: usize) -> usize;
0191 fn user_name(&self, index: usize) -> &str;
0192 fn set_user_name(&mut self, index: usize, _: String) -> bool;
0193 }
0194
0195 #[no_mangle]
0196 pub extern "C" fn persons_new(
0197 persons: *mut PersonsQObject,
0198 persons_new_data_ready: extern fn(*mut PersonsQObject, index: COption<usize>),
0199 persons_layout_about_to_be_changed: extern fn(*mut PersonsQObject),
0200 persons_layout_changed: extern fn(*mut PersonsQObject),
0201 persons_data_changed: extern fn(*mut PersonsQObject, usize, usize),
0202 persons_begin_reset_model: extern fn(*mut PersonsQObject),
0203 persons_end_reset_model: extern fn(*mut PersonsQObject),
0204 persons_begin_insert_rows: extern fn(*mut PersonsQObject, index: COption<usize>, usize, usize),
0205 persons_end_insert_rows: extern fn(*mut PersonsQObject),
0206 persons_begin_move_rows: extern fn(*mut PersonsQObject, index: COption<usize>, usize, usize, index: COption<usize>, usize),
0207 persons_end_move_rows: extern fn(*mut PersonsQObject),
0208 persons_begin_remove_rows: extern fn(*mut PersonsQObject, index: COption<usize>, usize, usize),
0209 persons_end_remove_rows: extern fn(*mut PersonsQObject),
0210 ) -> *mut Persons {
0211 let persons_emit = PersonsEmitter {
0212 qobject: Arc::new(AtomicPtr::new(persons)),
0213 new_data_ready: persons_new_data_ready,
0214 };
0215 let model = PersonsTree {
0216 qobject: persons,
0217 layout_about_to_be_changed: persons_layout_about_to_be_changed,
0218 layout_changed: persons_layout_changed,
0219 data_changed: persons_data_changed,
0220 begin_reset_model: persons_begin_reset_model,
0221 end_reset_model: persons_end_reset_model,
0222 begin_insert_rows: persons_begin_insert_rows,
0223 end_insert_rows: persons_end_insert_rows,
0224 begin_move_rows: persons_begin_move_rows,
0225 end_move_rows: persons_end_move_rows,
0226 begin_remove_rows: persons_begin_remove_rows,
0227 end_remove_rows: persons_end_remove_rows,
0228 };
0229 let d_persons = Persons::new(persons_emit, model);
0230 Box::into_raw(Box::new(d_persons))
0231 }
0232
0233 #[no_mangle]
0234 pub unsafe extern "C" fn persons_free(ptr: *mut Persons) {
0235 Box::from_raw(ptr).emit().clear();
0236 }
0237
0238 #[no_mangle]
0239 pub unsafe extern "C" fn persons_row_count(
0240 ptr: *const Persons,
0241 index: COption<usize>,
0242 ) -> c_int {
0243 to_c_int((&*ptr).row_count(index.into()))
0244 }
0245 #[no_mangle]
0246 pub unsafe extern "C" fn persons_can_fetch_more(
0247 ptr: *const Persons,
0248 index: COption<usize>,
0249 ) -> bool {
0250 (&*ptr).can_fetch_more(index.into())
0251 }
0252 #[no_mangle]
0253 pub unsafe extern "C" fn persons_fetch_more(ptr: *mut Persons, index: COption<usize>) {
0254 (&mut *ptr).fetch_more(index.into())
0255 }
0256 #[no_mangle]
0257 pub unsafe extern "C" fn persons_sort(
0258 ptr: *mut Persons,
0259 column: u8,
0260 order: SortOrder
0261 ) {
0262 (&mut *ptr).sort(column, order)
0263 }
0264 #[no_mangle]
0265 pub unsafe extern "C" fn persons_check_row(
0266 ptr: *const Persons,
0267 index: usize,
0268 row: c_int,
0269 ) -> COption<usize> {
0270 (&*ptr).check_row(index, to_usize(row)).into()
0271 }
0272 #[no_mangle]
0273 pub unsafe extern "C" fn persons_index(
0274 ptr: *const Persons,
0275 index: COption<usize>,
0276 row: c_int,
0277 ) -> usize {
0278 (&*ptr).index(index.into(), to_usize(row))
0279 }
0280 #[no_mangle]
0281 pub unsafe extern "C" fn persons_parent(ptr: *const Persons, index: usize) -> QModelIndex {
0282 if let Some(parent) = (&*ptr).parent(index) {
0283 QModelIndex {
0284 row: to_c_int((&*ptr).row(parent)),
0285 internal_id: parent,
0286 }
0287 } else {
0288 QModelIndex {
0289 row: -1,
0290 internal_id: 0,
0291 }
0292 }
0293 }
0294 #[no_mangle]
0295 pub unsafe extern "C" fn persons_row(ptr: *const Persons, index: usize) -> c_int {
0296 to_c_int((&*ptr).row(index))
0297 }
0298
0299 #[no_mangle]
0300 pub unsafe extern "C" fn persons_data_user_name(
0301 ptr: *const Persons, index: usize,
0302 d: *mut QString,
0303 set: extern fn(*mut QString, *const c_char, len: c_int),
0304 ) {
0305 let o = &*ptr;
0306 let data = o.user_name(index);
0307 let s: *const c_char = data.as_ptr() as *const c_char;
0308 set(d, s, to_c_int(data.len()));
0309 }
0310
0311 #[no_mangle]
0312 pub unsafe extern "C" fn persons_set_data_user_name(
0313 ptr: *mut Persons, index: usize,
0314 s: *const c_ushort, len: c_int,
0315 ) -> bool {
0316 let o = &mut *ptr;
0317 let mut v = String::new();
0318 set_string_from_utf16(&mut v, s, len);
0319 o.set_user_name(index, v)
0320 }