diff --git a/common-rust/src/implementation.rs b/common-rust/src/implementation.rs index 008e750..3765996 100644 --- a/common-rust/src/implementation.rs +++ b/common-rust/src/implementation.rs @@ -1,6 +1,6 @@ use interface::*; use types::*; -use libc::{c_int}; +use libc::c_int; use std::fs::read_dir; use std::path::PathBuf; use std::ffi::OsString; @@ -16,7 +16,7 @@ impl HelloTrait for Hello { fn create(emit: HelloEmitter) -> Self { Hello { emit: emit, - hello: String::new() + hello: String::new(), } } fn get_hello(&self) -> &String { @@ -29,8 +29,7 @@ impl HelloTrait for Hello { } impl Drop for Hello { - fn drop(&mut self) { - } + fn drop(&mut self) {} } pub struct DirEntry { @@ -39,28 +38,24 @@ pub struct DirEntry { impl DirEntry { pub fn create(name: &str) -> DirEntry { - DirEntry { - name: OsString::from(name) - } + DirEntry { name: OsString::from(name) } } } impl Item for DirEntry { fn data(&self, role: c_int) -> Variant { if role != 0 { - return Variant::None + return Variant::None; } let str = self.name.to_string_lossy().to_string(); Variant::from(str) } fn retrieve(&self, parents: Vec<&DirEntry>) -> Vec { - let path: PathBuf = parents.into_iter().map(|e|&e.name).collect(); + let path: PathBuf = parents.into_iter().map(|e| &e.name).collect(); let mut v = Vec::new(); if let Ok(it) = read_dir(path) { - for i in it.filter_map(|v|v.ok()) { - let de = DirEntry { - name: i.file_name(), - }; + for i in it.filter_map(|v| v.ok()) { + let de = DirEntry { name: i.file_name() }; v.push(de); } } @@ -71,9 +66,7 @@ impl Item for DirEntry { impl Default for DirEntry { fn default() -> DirEntry { - DirEntry { - name: OsString::new() - } + DirEntry { name: OsString::new() } } } @@ -88,12 +81,12 @@ struct Entry { parent: usize, row: usize, children: Option>, - data: T + data: T, } pub struct RGeneralItemModel { emit: RItemModelEmitter, - entries: Vec> + entries: Vec>, } impl RGeneralItemModel { @@ -107,9 +100,7 @@ impl RGeneralItemModel { if self.entries[p].children.is_none() { self.retrieve(p); let emit = self.emit.clone(); - thread::spawn(move || { - emit.new_data_ready(); - }); + thread::spawn(move || { emit.new_data_ready(); }); } p } @@ -124,7 +115,7 @@ impl RGeneralItemModel { parent: id, row: row, children: None, - data: d + data: d, }; children.push(self.entries.len() + row); new_entries.push(e); @@ -146,11 +137,21 @@ impl RGeneralItemModel { impl RItemModelTrait for RGeneralItemModel { fn create(emit: RItemModelEmitter, root: T) -> Self { - let none = Entry { parent: 0, row: 0, children: None, data: T::default() }; - let root = Entry { parent: 0, row: 0, children: None, data: root }; + let none = Entry { + parent: 0, + row: 0, + children: None, + data: T::default(), + }; + let root = Entry { + parent: 0, + row: 0, + children: None, + data: root, + }; RGeneralItemModel { emit: emit, - entries: vec![none, root] + entries: vec![none, root], } } fn emit(&self) -> &RItemModelEmitter { diff --git a/common-rust/src/interface.rs b/common-rust/src/interface.rs index dc74a2d..26d9759 100644 --- a/common-rust/src/interface.rs +++ b/common-rust/src/interface.rs @@ -12,7 +12,7 @@ pub struct HelloQObject {} pub struct HelloEmitter { qobject: *const HelloQObject, - hello_changed: fn (*const HelloQObject) + hello_changed: fn(*const HelloQObject), } impl HelloEmitter { @@ -28,29 +28,33 @@ pub trait HelloTrait { } #[no_mangle] -pub extern fn hello_new(qobject: *const HelloQObject, changed: fn(*const HelloQObject)) -> *mut Hello { +pub extern "C" fn hello_new(qobject: *const HelloQObject, + changed: fn(*const HelloQObject)) + -> *mut Hello { let emit = HelloEmitter { qobject: qobject, - hello_changed: changed + hello_changed: changed, }; let hello = Hello::create(emit); Box::into_raw(Box::new(hello)) } #[no_mangle] -pub unsafe extern fn hello_free(ptr: *mut Hello) { - if ptr.is_null() { return } +pub unsafe extern "C" fn hello_free(ptr: *mut Hello) { + if ptr.is_null() { + return; + } Box::from_raw(ptr); } #[no_mangle] -pub unsafe extern fn hello_set(ptr: *mut Hello, s: *const uint16_t, len: size_t) { +pub unsafe extern "C" fn hello_set(ptr: *mut Hello, s: *const uint16_t, len: size_t) { let data = slice::from_raw_parts(s, len as usize); (&mut *ptr).set_hello(String::from_utf16_lossy(data)); } #[no_mangle] -pub unsafe extern fn hello_get(ptr: *const Hello) -> QString { +pub unsafe extern "C" fn hello_get(ptr: *const Hello) -> QString { QString::from((&*ptr).get_hello()) } @@ -60,7 +64,7 @@ pub struct RItemModelQObject {} #[derive (Clone)] pub struct RItemModelEmitter { qobject: Arc>, - new_data_ready: fn (*const RItemModelQObject) + new_data_ready: fn(*const RItemModelQObject), } unsafe impl Send for RItemModelEmitter {} @@ -88,42 +92,54 @@ pub trait RItemModelTrait { } #[no_mangle] -pub extern fn ritemmodel_new(qobject: *const RItemModelQObject, new_data_ready: fn(*const RItemModelQObject)) -> *mut RItemModel { +pub extern "C" fn ritemmodel_new(qobject: *const RItemModelQObject, + new_data_ready: fn(*const RItemModelQObject)) + -> *mut RItemModel { let emit = RItemModelEmitter { qobject: Arc::new(Mutex::new(qobject)), - new_data_ready: new_data_ready + new_data_ready: new_data_ready, }; let ritemmodel = RItemModel::create(emit, DirEntry::create("/")); Box::into_raw(Box::new(ritemmodel)) } #[no_mangle] -pub unsafe extern fn ritemmodel_free(ptr: *mut RItemModel) { +pub unsafe extern "C" fn ritemmodel_free(ptr: *mut RItemModel) { Box::from_raw(ptr).emit().clear(); } #[no_mangle] -pub unsafe extern fn ritemmodel_column_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 { +pub unsafe extern "C" fn ritemmodel_column_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 { (&mut *ptr).column_count(parent) } #[no_mangle] -pub unsafe extern fn ritemmodel_row_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 { - (&mut*ptr).row_count(parent) +pub unsafe extern "C" fn ritemmodel_row_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 { + (&mut *ptr).row_count(parent) } #[no_mangle] -pub unsafe extern fn ritemmodel_index(ptr: *mut RItemModel, row: i32, column: i32, parent: QModelIndex) -> QModelIndex { +pub unsafe extern "C" fn ritemmodel_index(ptr: *mut RItemModel, + row: i32, + column: i32, + parent: QModelIndex) + -> QModelIndex { (&mut *ptr).index(row, column, parent) } #[no_mangle] -pub unsafe extern fn ritemmodel_parent(ptr: *const RItemModel, index: QModelIndex) -> QModelIndex { +pub unsafe extern "C" fn ritemmodel_parent(ptr: *const RItemModel, + index: QModelIndex) + -> QModelIndex { (&*ptr).parent(index) } #[no_mangle] -pub unsafe extern fn ritemmodel_data(ptr: *mut RItemModel, index: QModelIndex, role: c_int, d: *mut c_void, set: fn (*mut c_void, &QVariant)) { +pub unsafe extern "C" fn ritemmodel_data(ptr: *mut RItemModel, + index: QModelIndex, + role: c_int, + d: *mut c_void, + set: fn(*mut c_void, &QVariant)) { let data = (&mut *ptr).data(index, role); set(d, &QVariant::from(&data)); } diff --git a/common-rust/src/types.rs b/common-rust/src/types.rs index 9646155..87d10a8 100644 --- a/common-rust/src/types.rs +++ b/common-rust/src/types.rs @@ -6,20 +6,18 @@ use std::marker::PhantomData; #[repr(C)] pub struct QString { data: *const uint8_t, - len: c_int + len: c_int, } #[repr(C)] pub struct QStringIn { data: *const uint16_t, - len: c_int + len: c_int, } impl QStringIn { pub fn convert(&self) -> String { - let data = unsafe { - slice::from_raw_parts(self.data, self.len as usize) - }; + let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) }; String::from_utf16_lossy(data) } } @@ -28,7 +26,7 @@ impl<'a> From<&'a String> for QString { fn from(string: &'a String) -> QString { QString { len: string.len() as c_int, - data: string.as_ptr() + data: string.as_ptr(), } } } @@ -37,7 +35,7 @@ pub enum Variant { None, Bool(bool), String(String), - ByteArray(Vec) + ByteArray(Vec), } impl From for Variant { @@ -67,7 +65,7 @@ pub struct QVariant<'a> { type_: c_uint, len: c_int, data: *const uint8_t, - phantom: PhantomData<&'a u8> + phantom: PhantomData<&'a u8>, } impl<'a> QVariant<'a> { @@ -83,29 +81,37 @@ impl<'a> QVariant<'a> { impl<'a> From<&'a Variant> for QVariant<'a> { fn from(variant: &'a Variant) -> QVariant { match *variant { - Variant::None => QVariant { - data: null(), - len: 0, - type_: VariantType::Invalid as c_uint, - phantom: PhantomData - }, - Variant::Bool(v) => QVariant { - data: null(), - len: v as c_int, - type_: VariantType::Bool as c_uint, - phantom: PhantomData - }, - Variant::String(ref v) => QVariant { - data: v.as_ptr(), - len: v.len() as c_int, - type_: VariantType::String as c_uint, - phantom: PhantomData - }, - Variant::ByteArray(ref v) => QVariant { - data: v.as_ptr(), - len: v.len() as c_int, - type_: VariantType::ByteArray as c_uint, - phantom: PhantomData + Variant::None => { + QVariant { + data: null(), + len: 0, + type_: VariantType::Invalid as c_uint, + phantom: PhantomData, + } + } + Variant::Bool(v) => { + QVariant { + data: null(), + len: v as c_int, + type_: VariantType::Bool as c_uint, + phantom: PhantomData, + } + } + Variant::String(ref v) => { + QVariant { + data: v.as_ptr(), + len: v.len() as c_int, + type_: VariantType::String as c_uint, + phantom: PhantomData, + } + } + Variant::ByteArray(ref v) => { + QVariant { + data: v.as_ptr(), + len: v.len() as c_int, + type_: VariantType::ByteArray as c_uint, + phantom: PhantomData, + } } } } @@ -115,7 +121,7 @@ impl<'a> From<&'a Variant> for QVariant<'a> { pub struct QModelIndex { row: c_int, column: c_int, - internal_id: usize + internal_id: usize, } impl QModelIndex { @@ -123,21 +129,21 @@ impl QModelIndex { QModelIndex { row: -1, column: -1, - internal_id: 0 + internal_id: 0, } } pub fn create(row: c_int, column: c_int, id: usize) -> QModelIndex { QModelIndex { row: row, column: column, - internal_id: id + internal_id: id, } } pub fn flat(row: c_int, column: c_int) -> QModelIndex { QModelIndex { row: row, column: column, - internal_id: 1 + internal_id: 1, } } pub fn is_valid(&self) -> bool {