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, std::ffi::CStr[ing] and ::std::os::raw::c_void.
  • Loading branch information
ratijas committed Apr 27, 2021
1 parent ad28e6c commit 3e993d3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 49 deletions.
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
12 changes: 7 additions & 5 deletions qmetaobject/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,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 +441,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 +544,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 @@ -750,13 +751,14 @@ macro_rules! qt_signal {
/// ```
/// # #[macro_use] extern crate qmetaobject;
/// # use qmetaobject::qtdeclarative::QQmlExtensionPlugin;
/// # 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
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
41 changes: 20 additions & 21 deletions qmetaobject/src/qmetatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
use super::*;

fn register_metatype_common<T: QMetaType>(
name: *const std::os::raw::c_char,
name: *const c_char,
gadget_metaobject: *const QMetaObject,
) -> i32 {
use std::any::TypeId;
use std::collections::{HashMap, HashSet};
use std::ffi::{CStr, CString};
use std::sync::Mutex;

lazy_static! {
Expand Down Expand Up @@ -176,7 +175,7 @@ pub trait QMetaType: Clone + Default + 'static {
/// See the Qt documentation of qRegisterMetaType()
///
/// The default implementation should work for most types
fn register(name: Option<&std::ffi::CStr>) -> i32 {
fn register(name: Option<&CStr>) -> i32 {
register_metatype_common::<Self>(
name.map_or(std::ptr::null(), |x| x.as_ptr()),
std::ptr::null(),
Expand Down Expand Up @@ -240,7 +239,7 @@ impl QMetaType for String {
macro_rules! qdeclare_builtin_metatype {
($name:ty => $value:expr) => {
impl QMetaType for $name {
fn register(_name: Option<&std::ffi::CStr>) -> i32 {
fn register(_name: Option<&CStr>) -> i32 {
$value
}
}
Expand Down Expand Up @@ -275,7 +274,7 @@ qdeclare_builtin_metatype! {QSizeF => 22}
qdeclare_builtin_metatype! {QPoint => 25}
qdeclare_builtin_metatype! {QPointF => 26}
impl QMetaType for QVariant {
fn register(_name: Option<&std::ffi::CStr>) -> i32 {
fn register(_name: Option<&CStr>) -> i32 {
41
}
fn to_qvariant(&self) -> QVariant {
Expand Down Expand Up @@ -305,7 +304,7 @@ impl QMetaType for QJSValue {}
///
/// Don't implement this trait, implement the QMetaType trait.
pub trait PropertyType {
fn register_type(name: &std::ffi::CStr) -> i32;
fn register_type(name: &CStr) -> i32;
// Note: this is &mut self because of the lazy initialization of the QObject* for the QObject impl
unsafe fn pass_to_qt(&mut self, a: *mut c_void);
unsafe fn read_from_qt(a: *const c_void) -> Self;
Expand All @@ -315,7 +314,7 @@ impl<T: QMetaType> PropertyType for T
where
T: QMetaType,
{
fn register_type(name: &std::ffi::CStr) -> i32 {
fn register_type(name: &CStr) -> i32 {
<T as QMetaType>::register(Some(name))
}

Expand All @@ -332,21 +331,21 @@ where
}
}

impl<T> PropertyType for ::std::cell::RefCell<T>
impl<T> PropertyType for RefCell<T>
where
T: QObject,
{
fn register_type(_name: &::std::ffi::CStr) -> i32 {
fn register_type(_name: &CStr) -> i32 {
register_metatype_qobject::<T>()
}

unsafe fn pass_to_qt(&mut self, a: *mut ::std::os::raw::c_void) {
unsafe fn pass_to_qt(&mut self, a: *mut c_void) {
let pinned = QObjectPinned::new(self);
let r = a as *mut *const ::std::os::raw::c_void;
let r = a as *mut *const c_void;
*r = pinned.get_or_create_cpp_object()
}

unsafe fn read_from_qt(_a: *const ::std::os::raw::c_void) -> Self {
unsafe fn read_from_qt(_a: *const c_void) -> Self {
panic!("Cannot write into an Object property");
}
}
Expand All @@ -355,21 +354,21 @@ impl<T> PropertyType for QPointer<T>
where
T: QObject,
{
fn register_type(_name: &::std::ffi::CStr) -> i32 {
fn register_type(_name: &CStr) -> i32 {
register_metatype_qobject::<T>()
}

unsafe fn pass_to_qt(&mut self, a: *mut ::std::os::raw::c_void) {
unsafe fn pass_to_qt(&mut self, a: *mut c_void) {
let pinned = self.as_pinned();
let r = a as *mut *const ::std::os::raw::c_void;
let r = a as *mut *const c_void;
match pinned {
Some(pinned) => *r = pinned.get_or_create_cpp_object(),
None => *r = std::ptr::null(),
}
}

unsafe fn read_from_qt(a: *const ::std::os::raw::c_void) -> Self {
let r = a as *const *mut ::std::os::raw::c_void;
unsafe fn read_from_qt(a: *const c_void) -> Self {
let r = a as *const *mut c_void;
if a.is_null() || (*r).is_null() {
Self::default()
} else {
Expand All @@ -387,7 +386,7 @@ fn test_qmetatype() {
}
impl QMetaType for MyInt {}

assert_eq!(MyInt::register(Some(&std::ffi::CString::new("MyInt").unwrap())), MyInt::id());
assert_eq!(MyInt::register(Some(&CString::new("MyInt").unwrap())), MyInt::id());
let m42 = MyInt { x: 42 };
let m43 = MyInt { x: 43 };

Expand All @@ -406,7 +405,7 @@ fn test_qmetatype_register_wrong_type1() {
struct MyType {}
impl QMetaType for MyType {}
// registering with the name of an existing type should panic
MyType::register(Some(&std::ffi::CString::new("QString").unwrap()));
MyType::register(Some(&CString::new("QString").unwrap()));
}

#[test]
Expand All @@ -415,9 +414,9 @@ fn test_qmetatype_register_wrong_type2() {
#[derive(Default, Clone, Debug, Eq, PartialEq)]
struct MyType {}
impl QMetaType for MyType {}
String::register(Some(&std::ffi::CString::new("String").unwrap()));
String::register(Some(&CString::new("String").unwrap()));
// registering with the name of an existing type should panic
MyType::register(Some(&std::ffi::CString::new("String").unwrap()));
MyType::register(Some(&CString::new("String").unwrap()));
}

#[test]
Expand Down
31 changes: 15 additions & 16 deletions qmetaobject/src/qtdeclarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FO
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
use super::scenegraph::*;
use super::*;
use crate::*;
use crate::scenegraph::*;

/// Qt is not thread safe, and the engine can only be created once and in one thread.
/// So this is a guard that will be used to panic if the engine is created twice
Expand Down Expand Up @@ -80,8 +80,6 @@ cpp_class!(
impl QmlEngine {
/// Create a new QmlEngine
pub fn new() -> QmlEngine {
use std::ffi::CString;

let mut arguments: Vec<*mut c_char> = std::env::args()
.map(|arg| CString::new(arg.into_bytes()).expect("argument contains invalid c-string!"))
.map(|arg| arg.into_raw())
Expand Down Expand Up @@ -387,10 +385,10 @@ impl QmlComponent {
///
/// Refer to the Qt documentation for qmlRegisterType.
pub fn qml_register_type<T: QObject + Default + Sized>(
uri: &std::ffi::CStr,
uri: &CStr,
version_major: u32,
version_minor: u32,
qml_name: &std::ffi::CStr,
qml_name: &CStr,
) {
let uri_ptr = uri.as_ptr();
let qml_name_ptr = qml_name.as_ptr();
Expand Down Expand Up @@ -517,10 +515,10 @@ pub trait QSingletonInit {
///
/// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType-3
pub fn qml_register_singleton_type<T: QObject + QSingletonInit + Sized + Default>(
uri: &std::ffi::CStr,
uri: &CStr,
version_major: u32,
version_minor: u32,
qml_name: &std::ffi::CStr,
qml_name: &CStr,
) {
let uri_ptr = uri.as_ptr();
let qml_name_ptr = qml_name.as_ptr();
Expand Down Expand Up @@ -615,10 +613,10 @@ pub fn qml_register_singleton_type<T: QObject + QSingletonInit + Sized + Default
// XXX: replace link with real documentation, when it will be generated.
#[cfg(qt_5_14)]
pub fn qml_register_singleton_instance<T: QObject + Sized + Default>(
uri: &std::ffi::CStr,
uri: &CStr,
version_major: u32,
version_minor: u32,
type_name: &std::ffi::CStr,
type_name: &CStr,
obj: T,
) {
let uri_ptr = uri.as_ptr();
Expand Down Expand Up @@ -654,10 +652,10 @@ pub fn qml_register_singleton_instance<T: QObject + Sized + Default>(
/// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterUncreatableMetaObject
#[cfg(qt_5_8)]
pub fn qml_register_enum<T: QEnum>(
uri: &std::ffi::CStr,
uri: &CStr,
version_major: u32,
version_minor: u32,
qml_name: &std::ffi::CStr,
qml_name: &CStr,
) {
let uri_ptr = uri.as_ptr();
let qml_name_ptr = qml_name.as_ptr();
Expand Down Expand Up @@ -975,14 +973,15 @@ mod qjsvalue_tests {
///
/// ```
/// # extern crate qmetaobject; use qmetaobject::*;
/// # use std::ffi::CStr;
/// #[derive(Default, QObject)]
/// struct QExampleQmlPlugin {
/// base: qt_base_class!(trait QQmlExtensionPlugin),
/// plugin: qt_plugin!("org.qt-project.Qt.QQmlExtensionInterface/1.0"),
/// }
///
/// impl QQmlExtensionPlugin for QExampleQmlPlugin {
/// fn register_types(&mut self, uri: &std::ffi::CStr) {
/// fn register_types(&mut self, uri: &CStr) {
/// // call `qml_register_type` here
/// }
/// }
Expand All @@ -1002,7 +1001,7 @@ pub trait QQmlExtensionPlugin: QObject {
}

/// Refer to the Qt documentation of QQmlExtensionPlugin::registerTypes
fn register_types(&mut self, uri: &std::ffi::CStr);
fn register_types(&mut self, uri: &CStr);
}

cpp! {{
Expand All @@ -1013,9 +1012,9 @@ cpp! {{
void registerTypes(const char *uri) override {
rust!(Rust_QQmlExtensionPlugin_registerTypes[
rust_object: QObjectPinned<dyn QQmlExtensionPlugin> as "TraitObject",
uri: *const std::os::raw::c_char as "const char *"
uri: *const c_char as "const char *"
] {
rust_object.borrow_mut().register_types(unsafe { std::ffi::CStr::from_ptr(uri) });
rust_object.borrow_mut().register_types(unsafe { CStr::from_ptr(uri) });
});
}
};
Expand Down

0 comments on commit 3e993d3

Please sign in to comment.