Skip to content

Commit

Permalink
shorten paths: factor out extra double-colons to imports
Browse files Browse the repository at this point in the history
* Most noticeably, shortened std::ffi::CStr[ing] and ::std::os::raw::c_void.
* (qmetaobject/src/scenegraph.rs):
  - Removed probably misplaced #[cfg(qt_5.8)] attribute before use super::*;
  - Fixed indentation in screnegraph, because cargo fmt could be wrong when
    it comes to  macros.
  • Loading branch information
ratijas committed Apr 27, 2021
1 parent ad28e6c commit 0624965
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 108 deletions.
8 changes: 4 additions & 4 deletions examples/graph/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#![allow(non_snake_case)]
#![allow(unused_variables)]
#[macro_use]
extern crate qmetaobject;
use qmetaobject::scenegraph::*;
use qmetaobject::*;

#[macro_use]
extern crate cstr;
#[macro_use]
extern crate cpp;

use qmetaobject::scenegraph::*;
use qmetaobject::*;

mod nodes;

#[derive(Default, QObject)]
Expand Down
2 changes: 1 addition & 1 deletion examples/graph/src/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use qmetaobject::scenegraph::SGNode;
use qmetaobject::{QColor, QQuickItem, QRectF};
use qmetaobject::{qrc, QColor, QQuickItem, QRectF};

qrc! {
init_resource,
Expand Down
5 changes: 3 additions & 2 deletions qmetaobject/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,9 @@ impl<Args> Signal<Args> {
/// # Example
///
/// ```
/// # #[macro_use] extern crate cpp;
/// # use qmetaobject::*;
/// #[macro_use] extern crate cpp;
/// use qmetaobject::*;
///
/// fn object_name_changed() -> Signal<fn(QString)> {
/// unsafe {
/// Signal::new(cpp!([] -> SignalInner as "SignalInner" {
Expand Down
7 changes: 4 additions & 3 deletions qmetaobject/src/future.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::future::Future;
use std::mem::replace;
use std::os::raw::c_void;
use std::pin::Pin;
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
Expand Down Expand Up @@ -90,7 +91,7 @@ cpp! {{

~Waker() {
rust!(QtDestroyFuture [future: *mut dyn Future<Output = ()> as "TraitObject"] {
std::mem::drop(Box::from_raw(future))
drop(Box::from_raw(future));
});
}
};
Expand Down Expand Up @@ -160,7 +161,7 @@ pub unsafe fn wait_on_signal<Args: SignalArgArrayToTuple>(
type Output = <Args as SignalArgArrayToTuple>::Tuple;
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
let state = &mut self.0;
*state = match std::mem::replace(state, ConnectionFutureState::Invalid) {
*state = match replace(state, ConnectionFutureState::Invalid) {
ConnectionFutureState::Finished { result } => {
return Poll::Ready(result);
}
Expand All @@ -181,7 +182,7 @@ pub unsafe fn wait_on_signal<Args: SignalArgArrayToTuple>(
for *mut ConnectionFutureState<Args>
{
unsafe fn apply(&mut self, a: *const *const c_void) {
if let ConnectionFutureState::Started { mut handle, waker } = std::mem::replace(
if let ConnectionFutureState::Started { mut handle, waker } = replace(
&mut **self,
ConnectionFutureState::Finished { result: Args::args_array_to_tuple(a) },
) {
Expand Down
64 changes: 34 additions & 30 deletions qmetaobject/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```
#[macro_use] extern crate cstr;
extern crate qmetaobject;
use qmetaobject::*;
Expand Down Expand Up @@ -88,7 +87,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
All the method are provided so you can just implement the QMetaType like this:
```rust
# use qmetaobject::QMetaType;
use qmetaobject::QMetaType;
#[derive(Default, Clone)]
struct MyPoint(u32, u32);
Expand All @@ -115,19 +115,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This can be done like so:
```
# extern crate qmetaobject;
# use qmetaobject::*;
#[derive(QObject,Default)]
use qmetaobject::*;
# use std::cell::RefCell;
#[derive(QObject, Default)]
struct MyAsyncObject {
base: qt_base_class!(trait QObject),
result: qt_property!(QString; NOTIFY result_changed),
result_changed: qt_signal!(),
recompute_result: qt_method!(fn recompute_result(&self, name: String) {
let qptr = QPointer::from(&*self);
let set_value = qmetaobject::queued_callback(move |val: QString| {
qptr.as_pinned().map(|self_| {
self_.borrow_mut().result = val;
self_.borrow().result_changed();
let set_value = queued_callback(move |val: QString| {
qptr.as_pinned().map(|this| {
this.borrow_mut().result = val;
this.borrow().result_changed();
});
});
std::thread::spawn(move || {
Expand All @@ -137,9 +138,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}).join();
})
}
# let obj = std::cell::RefCell::new(MyAsyncObject::default());
# let obj = RefCell::new(MyAsyncObject::default());
# let mut engine = QmlEngine::new();
# unsafe { qmetaobject::connect(
# unsafe { connect(
# QObject::cpp_construct(&obj),
# obj.borrow().result_changed.to_cpp_representation(&*obj.borrow()),
# || engine.quit()
Expand All @@ -157,9 +158,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#[macro_use]
extern crate cpp;

#[allow(unused_imports)]
#[macro_use]
extern crate qmetaobject_impl;
#[doc(hidden)]
pub use qmetaobject_impl::*;

Expand All @@ -173,7 +171,8 @@ pub use lazy_static::*;
#[macro_export]
macro_rules! qmetaobject_lazy_static { ($($t:tt)*) => { lazy_static!($($t)*) } }

use std::cell::RefCell;
use std::cell::{RefCell, RefMut};
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_void};

pub use qttypes;
Expand Down Expand Up @@ -440,11 +439,11 @@ impl<T: QObject + ?Sized> Clone for QPointer<T> {
/// Same as std::cell::RefMut, but does not allow to move from
pub struct QObjectRefMut<'b, T: QObject + ?Sized + 'b> {
old_value: *mut c_void,
inner: std::cell::RefMut<'b, T>,
inner: RefMut<'b, T>,
}

impl<'b, T: QObject + ?Sized> std::ops::Deref for QObjectRefMut<'b, T> {
type Target = std::cell::RefMut<'b, T>;
type Target = RefMut<'b, T>;

#[inline]
fn deref(&self) -> &Self::Target {
Expand Down Expand Up @@ -543,7 +542,7 @@ impl<T: QObject + ?Sized> QObjectBox<T> {
pub fn into_leaked_cpp_ptr<T: QObject>(obj: T) -> *mut c_void {
let b = Box::new(RefCell::new(obj));
let obj_ptr = unsafe { QObject::cpp_construct(&b) };
std::boxed::Box::into_raw(b);
Box::into_raw(b);
obj_ptr
}

Expand Down Expand Up @@ -640,7 +639,8 @@ unsafe impl Send for QMetaObject {}
/// The trait needs to be like the QObject trait, see the documentation of the QObject trait.
///
/// ```
/// # #[macro_use] extern crate qmetaobject; use qmetaobject::QObject;
/// use qmetaobject::*;
///
/// #[derive(QObject)]
/// struct Foo {
/// base : qt_base_class!(trait QObject),
Expand Down Expand Up @@ -670,7 +670,8 @@ macro_rules! qt_base_class {
/// `ALIAS` followed by an identifier allow to give a different name than the actual field name.
///
/// ```
/// # #[macro_use] extern crate qmetaobject; use qmetaobject::QObject;
/// use qmetaobject::*;
///
/// #[derive(QObject)]
/// struct Foo {
/// base: qt_base_class!(trait QObject),
Expand All @@ -696,7 +697,8 @@ macro_rules! qt_property {
/// Can be used within a struct that derives from QObject or QGadget
///
/// ```
/// # #[macro_use] extern crate qmetaobject; use qmetaobject::QObject;
/// use qmetaobject::*;
///
/// #[derive(QObject)]
/// struct Foo {
/// base: qt_base_class!(trait QObject),
Expand Down Expand Up @@ -726,7 +728,8 @@ macro_rules! qt_method {
/// To be used within a struct that derives from QObject
///
/// ```
/// # #[macro_use] extern crate qmetaobject; use qmetaobject::QObject;
/// use qmetaobject::*;
///
/// #[derive(QObject)]
/// struct Foo {
/// base: qt_base_class!(trait QObject),
Expand All @@ -748,15 +751,16 @@ macro_rules! qt_signal {
/// the IID
///
/// ```
/// # #[macro_use] extern crate qmetaobject;
/// # use qmetaobject::qtdeclarative::QQmlExtensionPlugin;
/// use qmetaobject::*;
/// # use std::ffi::CStr;
///
/// #[derive(Default, QObject)]
/// struct MyPlugin {
/// base: qt_base_class!(trait QQmlExtensionPlugin),
/// plugin: qt_plugin!("org.qt-project.Qt.QQmlExtensionInterface/1.0")
/// }
/// # impl QQmlExtensionPlugin for MyPlugin {
/// # fn register_types(&mut self, uri: &std::ffi::CStr) {}
/// # fn register_types(&mut self, uri: &CStr) {}
/// # }
/// ```
#[macro_export]
Expand Down Expand Up @@ -841,8 +845,8 @@ where
/// callback will not be recieved.
///
/// ```
/// # extern crate qmetaobject;
/// # use qmetaobject::queued_callback;
/// use qmetaobject::queued_callback;
///
/// let callback = queued_callback(|()| println!("hello from main thread"));
/// std::thread::spawn(move || {callback(());}).join();
/// ```
Expand Down Expand Up @@ -989,10 +993,9 @@ pub const USER_ROLE: i32 = 0x0100;
/// ```
/// then the following Rust code:
/// ```
/// # extern crate qmetaobject;
/// # use qmetaobject::qrc;
/// use qmetaobject::qrc;
/// # // For maintainers: this is actually tested against real files.
/// // private fn, and base directory shortcut
/// // private fn, base directory shortcut
/// qrc!(my_resource_1,
/// "tests/qml" as "foo1" {
/// "main.qml",
Expand All @@ -1016,6 +1019,7 @@ pub const USER_ROLE: i32 = 0x0100;
/// # fn use_resource(_r: &str) {
/// # // at the time of writing, it is the only way to test the existence of a resource.
/// # use qmetaobject::*;
/// #
/// # let mut engine = QmlEngine::new();
/// # let mut c = QmlComponent::new(&engine);
/// # c.load_url(QUrl::from(QString::from("qrc:/foo2/baz/Foo.qml")), CompilationMode::PreferSynchronous);
Expand Down
2 changes: 1 addition & 1 deletion qmetaobject/src/listmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ where
QVariant::default()
}
}
fn role_names(&self) -> std::collections::HashMap<i32, QByteArray> {
fn role_names(&self) -> HashMap<i32, QByteArray> {
T::names().iter().enumerate().map(|(i, x)| (i as i32 + USER_ROLE, x.clone())).collect()
}
}
Expand Down
7 changes: 4 additions & 3 deletions qmetaobject/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Logging facilities and forwarding

use std::ffi::CStr;
use std::os::raw::c_char;

#[cfg(feature = "log")]
Expand Down Expand Up @@ -33,7 +34,7 @@ impl QMessageLogContext {
if x.is_null() {
return "";
}
std::ffi::CStr::from_ptr(x).to_str().unwrap()
CStr::from_ptr(x).to_str().unwrap()
}
}

Expand All @@ -46,7 +47,7 @@ impl QMessageLogContext {
if x.is_null() {
return "";
}
std::ffi::CStr::from_ptr(x).to_str().unwrap()
CStr::from_ptr(x).to_str().unwrap()
}
}

Expand All @@ -59,7 +60,7 @@ impl QMessageLogContext {
if x.is_null() {
return "";
}
std::ffi::CStr::from_ptr(x).to_str().unwrap()
CStr::from_ptr(x).to_str().unwrap()
}
}
}
Expand Down
Loading

0 comments on commit 0624965

Please sign in to comment.