Run rustfmt

master
Jos van den Oever 2017-08-08 20:33:46 +02:00
parent 91962f3f2b
commit 6b22802ec6
3 changed files with 100 additions and 77 deletions

View File

@ -1,6 +1,6 @@
use interface::*; use interface::*;
use types::*; use types::*;
use libc::{c_int}; use libc::c_int;
use std::fs::read_dir; use std::fs::read_dir;
use std::path::PathBuf; use std::path::PathBuf;
use std::ffi::OsString; use std::ffi::OsString;
@ -16,7 +16,7 @@ impl HelloTrait for Hello {
fn create(emit: HelloEmitter) -> Self { fn create(emit: HelloEmitter) -> Self {
Hello { Hello {
emit: emit, emit: emit,
hello: String::new() hello: String::new(),
} }
} }
fn get_hello(&self) -> &String { fn get_hello(&self) -> &String {
@ -29,8 +29,7 @@ impl HelloTrait for Hello {
} }
impl Drop for Hello { impl Drop for Hello {
fn drop(&mut self) { fn drop(&mut self) {}
}
} }
pub struct DirEntry { pub struct DirEntry {
@ -39,28 +38,24 @@ pub struct DirEntry {
impl DirEntry { impl DirEntry {
pub fn create(name: &str) -> DirEntry { pub fn create(name: &str) -> DirEntry {
DirEntry { DirEntry { name: OsString::from(name) }
name: OsString::from(name)
}
} }
} }
impl Item for DirEntry { impl Item for DirEntry {
fn data(&self, role: c_int) -> Variant { fn data(&self, role: c_int) -> Variant {
if role != 0 { if role != 0 {
return Variant::None return Variant::None;
} }
let str = self.name.to_string_lossy().to_string(); let str = self.name.to_string_lossy().to_string();
Variant::from(str) Variant::from(str)
} }
fn retrieve(&self, parents: Vec<&DirEntry>) -> Vec<DirEntry> { fn retrieve(&self, parents: Vec<&DirEntry>) -> Vec<DirEntry> {
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(); let mut v = Vec::new();
if let Ok(it) = read_dir(path) { if let Ok(it) = read_dir(path) {
for i in it.filter_map(|v|v.ok()) { for i in it.filter_map(|v| v.ok()) {
let de = DirEntry { let de = DirEntry { name: i.file_name() };
name: i.file_name(),
};
v.push(de); v.push(de);
} }
} }
@ -71,9 +66,7 @@ impl Item for DirEntry {
impl Default for DirEntry { impl Default for DirEntry {
fn default() -> DirEntry { fn default() -> DirEntry {
DirEntry { DirEntry { name: OsString::new() }
name: OsString::new()
}
} }
} }
@ -88,12 +81,12 @@ struct Entry<T: Item> {
parent: usize, parent: usize,
row: usize, row: usize,
children: Option<Vec<usize>>, children: Option<Vec<usize>>,
data: T data: T,
} }
pub struct RGeneralItemModel<T: Item> { pub struct RGeneralItemModel<T: Item> {
emit: RItemModelEmitter, emit: RItemModelEmitter,
entries: Vec<Entry<T>> entries: Vec<Entry<T>>,
} }
impl<T: Item> RGeneralItemModel<T> { impl<T: Item> RGeneralItemModel<T> {
@ -107,9 +100,7 @@ impl<T: Item> RGeneralItemModel<T> {
if self.entries[p].children.is_none() { if self.entries[p].children.is_none() {
self.retrieve(p); self.retrieve(p);
let emit = self.emit.clone(); let emit = self.emit.clone();
thread::spawn(move || { thread::spawn(move || { emit.new_data_ready(); });
emit.new_data_ready();
});
} }
p p
} }
@ -124,7 +115,7 @@ impl<T: Item> RGeneralItemModel<T> {
parent: id, parent: id,
row: row, row: row,
children: None, children: None,
data: d data: d,
}; };
children.push(self.entries.len() + row); children.push(self.entries.len() + row);
new_entries.push(e); new_entries.push(e);
@ -146,11 +137,21 @@ impl<T: Item> RGeneralItemModel<T> {
impl<T: Item> RItemModelTrait<T> for RGeneralItemModel<T> { impl<T: Item> RItemModelTrait<T> for RGeneralItemModel<T> {
fn create(emit: RItemModelEmitter, root: T) -> Self { fn create(emit: RItemModelEmitter, root: T) -> Self {
let none = Entry { parent: 0, row: 0, children: None, data: T::default() }; let none = Entry {
let root = Entry { parent: 0, row: 0, children: None, data: root }; parent: 0,
row: 0,
children: None,
data: T::default(),
};
let root = Entry {
parent: 0,
row: 0,
children: None,
data: root,
};
RGeneralItemModel { RGeneralItemModel {
emit: emit, emit: emit,
entries: vec![none, root] entries: vec![none, root],
} }
} }
fn emit(&self) -> &RItemModelEmitter { fn emit(&self) -> &RItemModelEmitter {

View File

@ -12,7 +12,7 @@ pub struct HelloQObject {}
pub struct HelloEmitter { pub struct HelloEmitter {
qobject: *const HelloQObject, qobject: *const HelloQObject,
hello_changed: fn (*const HelloQObject) hello_changed: fn(*const HelloQObject),
} }
impl HelloEmitter { impl HelloEmitter {
@ -28,29 +28,33 @@ pub trait HelloTrait {
} }
#[no_mangle] #[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 { let emit = HelloEmitter {
qobject: qobject, qobject: qobject,
hello_changed: changed hello_changed: changed,
}; };
let hello = Hello::create(emit); let hello = Hello::create(emit);
Box::into_raw(Box::new(hello)) Box::into_raw(Box::new(hello))
} }
#[no_mangle] #[no_mangle]
pub unsafe extern fn hello_free(ptr: *mut Hello) { pub unsafe extern "C" fn hello_free(ptr: *mut Hello) {
if ptr.is_null() { return } if ptr.is_null() {
return;
}
Box::from_raw(ptr); Box::from_raw(ptr);
} }
#[no_mangle] #[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); let data = slice::from_raw_parts(s, len as usize);
(&mut *ptr).set_hello(String::from_utf16_lossy(data)); (&mut *ptr).set_hello(String::from_utf16_lossy(data));
} }
#[no_mangle] #[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()) QString::from((&*ptr).get_hello())
} }
@ -60,7 +64,7 @@ pub struct RItemModelQObject {}
#[derive (Clone)] #[derive (Clone)]
pub struct RItemModelEmitter { pub struct RItemModelEmitter {
qobject: Arc<Mutex<*const RItemModelQObject>>, qobject: Arc<Mutex<*const RItemModelQObject>>,
new_data_ready: fn (*const RItemModelQObject) new_data_ready: fn(*const RItemModelQObject),
} }
unsafe impl Send for RItemModelEmitter {} unsafe impl Send for RItemModelEmitter {}
@ -88,42 +92,54 @@ pub trait RItemModelTrait<T> {
} }
#[no_mangle] #[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 { let emit = RItemModelEmitter {
qobject: Arc::new(Mutex::new(qobject)), 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("/")); let ritemmodel = RItemModel::create(emit, DirEntry::create("/"));
Box::into_raw(Box::new(ritemmodel)) Box::into_raw(Box::new(ritemmodel))
} }
#[no_mangle] #[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(); Box::from_raw(ptr).emit().clear();
} }
#[no_mangle] #[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) (&mut *ptr).column_count(parent)
} }
#[no_mangle] #[no_mangle]
pub unsafe extern fn ritemmodel_row_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 { pub unsafe extern "C" fn ritemmodel_row_count(ptr: *mut RItemModel, parent: QModelIndex) -> i32 {
(&mut*ptr).row_count(parent) (&mut *ptr).row_count(parent)
} }
#[no_mangle] #[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) (&mut *ptr).index(row, column, parent)
} }
#[no_mangle] #[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) (&*ptr).parent(index)
} }
#[no_mangle] #[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); let data = (&mut *ptr).data(index, role);
set(d, &QVariant::from(&data)); set(d, &QVariant::from(&data));
} }

View File

@ -6,20 +6,18 @@ use std::marker::PhantomData;
#[repr(C)] #[repr(C)]
pub struct QString { pub struct QString {
data: *const uint8_t, data: *const uint8_t,
len: c_int len: c_int,
} }
#[repr(C)] #[repr(C)]
pub struct QStringIn { pub struct QStringIn {
data: *const uint16_t, data: *const uint16_t,
len: c_int len: c_int,
} }
impl QStringIn { impl QStringIn {
pub fn convert(&self) -> String { pub fn convert(&self) -> String {
let data = unsafe { let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) };
slice::from_raw_parts(self.data, self.len as usize)
};
String::from_utf16_lossy(data) String::from_utf16_lossy(data)
} }
} }
@ -28,7 +26,7 @@ impl<'a> From<&'a String> for QString {
fn from(string: &'a String) -> QString { fn from(string: &'a String) -> QString {
QString { QString {
len: string.len() as c_int, len: string.len() as c_int,
data: string.as_ptr() data: string.as_ptr(),
} }
} }
} }
@ -37,7 +35,7 @@ pub enum Variant {
None, None,
Bool(bool), Bool(bool),
String(String), String(String),
ByteArray(Vec<u8>) ByteArray(Vec<u8>),
} }
impl From<bool> for Variant { impl From<bool> for Variant {
@ -67,7 +65,7 @@ pub struct QVariant<'a> {
type_: c_uint, type_: c_uint,
len: c_int, len: c_int,
data: *const uint8_t, data: *const uint8_t,
phantom: PhantomData<&'a u8> phantom: PhantomData<&'a u8>,
} }
impl<'a> QVariant<'a> { impl<'a> QVariant<'a> {
@ -83,29 +81,37 @@ impl<'a> QVariant<'a> {
impl<'a> From<&'a Variant> for QVariant<'a> { impl<'a> From<&'a Variant> for QVariant<'a> {
fn from(variant: &'a Variant) -> QVariant { fn from(variant: &'a Variant) -> QVariant {
match *variant { match *variant {
Variant::None => QVariant { Variant::None => {
data: null(), QVariant {
len: 0, data: null(),
type_: VariantType::Invalid as c_uint, len: 0,
phantom: PhantomData type_: VariantType::Invalid as c_uint,
}, phantom: PhantomData,
Variant::Bool(v) => QVariant { }
data: null(), }
len: v as c_int, Variant::Bool(v) => {
type_: VariantType::Bool as c_uint, QVariant {
phantom: PhantomData data: null(),
}, len: v as c_int,
Variant::String(ref v) => QVariant { type_: VariantType::Bool as c_uint,
data: v.as_ptr(), phantom: PhantomData,
len: v.len() as c_int, }
type_: VariantType::String as c_uint, }
phantom: PhantomData Variant::String(ref v) => {
}, QVariant {
Variant::ByteArray(ref v) => QVariant { data: v.as_ptr(),
data: v.as_ptr(), len: v.len() as c_int,
len: v.len() as c_int, type_: VariantType::String as c_uint,
type_: VariantType::ByteArray as c_uint, phantom: PhantomData,
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 { pub struct QModelIndex {
row: c_int, row: c_int,
column: c_int, column: c_int,
internal_id: usize internal_id: usize,
} }
impl QModelIndex { impl QModelIndex {
@ -123,21 +129,21 @@ impl QModelIndex {
QModelIndex { QModelIndex {
row: -1, row: -1,
column: -1, column: -1,
internal_id: 0 internal_id: 0,
} }
} }
pub fn create(row: c_int, column: c_int, id: usize) -> QModelIndex { pub fn create(row: c_int, column: c_int, id: usize) -> QModelIndex {
QModelIndex { QModelIndex {
row: row, row: row,
column: column, column: column,
internal_id: id internal_id: id,
} }
} }
pub fn flat(row: c_int, column: c_int) -> QModelIndex { pub fn flat(row: c_int, column: c_int) -> QModelIndex {
QModelIndex { QModelIndex {
row: row, row: row,
column: column, column: column,
internal_id: 1 internal_id: 1,
} }
} }
pub fn is_valid(&self) -> bool { pub fn is_valid(&self) -> bool {