Skip to content

Commit 3e993d3

Browse files
committed
shorten paths: factor out extra double-colons to imports
Most noticeably, std::ffi::CStr[ing] and ::std::os::raw::c_void.
1 parent ad28e6c commit 3e993d3

File tree

6 files changed

+51
-49
lines changed

6 files changed

+51
-49
lines changed

qmetaobject/src/future.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::future::Future;
2+
use std::mem::replace;
23
use std::os::raw::c_void;
34
use std::pin::Pin;
45
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
@@ -90,7 +91,7 @@ cpp! {{
9091

9192
~Waker() {
9293
rust!(QtDestroyFuture [future: *mut dyn Future<Output = ()> as "TraitObject"] {
93-
std::mem::drop(Box::from_raw(future))
94+
drop(Box::from_raw(future));
9495
});
9596
}
9697
};
@@ -160,7 +161,7 @@ pub unsafe fn wait_on_signal<Args: SignalArgArrayToTuple>(
160161
type Output = <Args as SignalArgArrayToTuple>::Tuple;
161162
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
162163
let state = &mut self.0;
163-
*state = match std::mem::replace(state, ConnectionFutureState::Invalid) {
164+
*state = match replace(state, ConnectionFutureState::Invalid) {
164165
ConnectionFutureState::Finished { result } => {
165166
return Poll::Ready(result);
166167
}
@@ -181,7 +182,7 @@ pub unsafe fn wait_on_signal<Args: SignalArgArrayToTuple>(
181182
for *mut ConnectionFutureState<Args>
182183
{
183184
unsafe fn apply(&mut self, a: *const *const c_void) {
184-
if let ConnectionFutureState::Started { mut handle, waker } = std::mem::replace(
185+
if let ConnectionFutureState::Started { mut handle, waker } = replace(
185186
&mut **self,
186187
ConnectionFutureState::Finished { result: Args::args_array_to_tuple(a) },
187188
) {

qmetaobject/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ pub use lazy_static::*;
173173
#[macro_export]
174174
macro_rules! qmetaobject_lazy_static { ($($t:tt)*) => { lazy_static!($($t)*) } }
175175

176-
use std::cell::RefCell;
176+
use std::cell::{RefCell, RefMut};
177+
use std::ffi::{CStr, CString};
177178
use std::os::raw::{c_char, c_void};
178179

179180
pub use qttypes;
@@ -440,11 +441,11 @@ impl<T: QObject + ?Sized> Clone for QPointer<T> {
440441
/// Same as std::cell::RefMut, but does not allow to move from
441442
pub struct QObjectRefMut<'b, T: QObject + ?Sized + 'b> {
442443
old_value: *mut c_void,
443-
inner: std::cell::RefMut<'b, T>,
444+
inner: RefMut<'b, T>,
444445
}
445446

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

449450
#[inline]
450451
fn deref(&self) -> &Self::Target {
@@ -543,7 +544,7 @@ impl<T: QObject + ?Sized> QObjectBox<T> {
543544
pub fn into_leaked_cpp_ptr<T: QObject>(obj: T) -> *mut c_void {
544545
let b = Box::new(RefCell::new(obj));
545546
let obj_ptr = unsafe { QObject::cpp_construct(&b) };
546-
std::boxed::Box::into_raw(b);
547+
Box::into_raw(b);
547548
obj_ptr
548549
}
549550

@@ -750,13 +751,14 @@ macro_rules! qt_signal {
750751
/// ```
751752
/// # #[macro_use] extern crate qmetaobject;
752753
/// # use qmetaobject::qtdeclarative::QQmlExtensionPlugin;
754+
/// # use std::ffi::CStr;
753755
/// #[derive(Default, QObject)]
754756
/// struct MyPlugin {
755757
/// base: qt_base_class!(trait QQmlExtensionPlugin),
756758
/// plugin: qt_plugin!("org.qt-project.Qt.QQmlExtensionInterface/1.0")
757759
/// }
758760
/// # impl QQmlExtensionPlugin for MyPlugin {
759-
/// # fn register_types(&mut self, uri: &std::ffi::CStr) {}
761+
/// # fn register_types(&mut self, uri: &CStr) {}
760762
/// # }
761763
/// ```
762764
#[macro_export]

qmetaobject/src/listmodel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ where
223223
QVariant::default()
224224
}
225225
}
226-
fn role_names(&self) -> std::collections::HashMap<i32, QByteArray> {
226+
fn role_names(&self) -> HashMap<i32, QByteArray> {
227227
T::names().iter().enumerate().map(|(i, x)| (i as i32 + USER_ROLE, x.clone())).collect()
228228
}
229229
}

qmetaobject/src/log.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Logging facilities and forwarding
22
3+
use std::ffi::CStr;
34
use std::os::raw::c_char;
45

56
#[cfg(feature = "log")]
@@ -33,7 +34,7 @@ impl QMessageLogContext {
3334
if x.is_null() {
3435
return "";
3536
}
36-
std::ffi::CStr::from_ptr(x).to_str().unwrap()
37+
CStr::from_ptr(x).to_str().unwrap()
3738
}
3839
}
3940

@@ -46,7 +47,7 @@ impl QMessageLogContext {
4647
if x.is_null() {
4748
return "";
4849
}
49-
std::ffi::CStr::from_ptr(x).to_str().unwrap()
50+
CStr::from_ptr(x).to_str().unwrap()
5051
}
5152
}
5253

@@ -59,7 +60,7 @@ impl QMessageLogContext {
5960
if x.is_null() {
6061
return "";
6162
}
62-
std::ffi::CStr::from_ptr(x).to_str().unwrap()
63+
CStr::from_ptr(x).to_str().unwrap()
6364
}
6465
}
6566
}

qmetaobject/src/qmetatype.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1818
use super::*;
1919

2020
fn register_metatype_common<T: QMetaType>(
21-
name: *const std::os::raw::c_char,
21+
name: *const c_char,
2222
gadget_metaobject: *const QMetaObject,
2323
) -> i32 {
2424
use std::any::TypeId;
2525
use std::collections::{HashMap, HashSet};
26-
use std::ffi::{CStr, CString};
2726
use std::sync::Mutex;
2827

2928
lazy_static! {
@@ -176,7 +175,7 @@ pub trait QMetaType: Clone + Default + 'static {
176175
/// See the Qt documentation of qRegisterMetaType()
177176
///
178177
/// The default implementation should work for most types
179-
fn register(name: Option<&std::ffi::CStr>) -> i32 {
178+
fn register(name: Option<&CStr>) -> i32 {
180179
register_metatype_common::<Self>(
181180
name.map_or(std::ptr::null(), |x| x.as_ptr()),
182181
std::ptr::null(),
@@ -240,7 +239,7 @@ impl QMetaType for String {
240239
macro_rules! qdeclare_builtin_metatype {
241240
($name:ty => $value:expr) => {
242241
impl QMetaType for $name {
243-
fn register(_name: Option<&std::ffi::CStr>) -> i32 {
242+
fn register(_name: Option<&CStr>) -> i32 {
244243
$value
245244
}
246245
}
@@ -275,7 +274,7 @@ qdeclare_builtin_metatype! {QSizeF => 22}
275274
qdeclare_builtin_metatype! {QPoint => 25}
276275
qdeclare_builtin_metatype! {QPointF => 26}
277276
impl QMetaType for QVariant {
278-
fn register(_name: Option<&std::ffi::CStr>) -> i32 {
277+
fn register(_name: Option<&CStr>) -> i32 {
279278
41
280279
}
281280
fn to_qvariant(&self) -> QVariant {
@@ -305,7 +304,7 @@ impl QMetaType for QJSValue {}
305304
///
306305
/// Don't implement this trait, implement the QMetaType trait.
307306
pub trait PropertyType {
308-
fn register_type(name: &std::ffi::CStr) -> i32;
307+
fn register_type(name: &CStr) -> i32;
309308
// Note: this is &mut self because of the lazy initialization of the QObject* for the QObject impl
310309
unsafe fn pass_to_qt(&mut self, a: *mut c_void);
311310
unsafe fn read_from_qt(a: *const c_void) -> Self;
@@ -315,7 +314,7 @@ impl<T: QMetaType> PropertyType for T
315314
where
316315
T: QMetaType,
317316
{
318-
fn register_type(name: &std::ffi::CStr) -> i32 {
317+
fn register_type(name: &CStr) -> i32 {
319318
<T as QMetaType>::register(Some(name))
320319
}
321320

@@ -332,21 +331,21 @@ where
332331
}
333332
}
334333

335-
impl<T> PropertyType for ::std::cell::RefCell<T>
334+
impl<T> PropertyType for RefCell<T>
336335
where
337336
T: QObject,
338337
{
339-
fn register_type(_name: &::std::ffi::CStr) -> i32 {
338+
fn register_type(_name: &CStr) -> i32 {
340339
register_metatype_qobject::<T>()
341340
}
342341

343-
unsafe fn pass_to_qt(&mut self, a: *mut ::std::os::raw::c_void) {
342+
unsafe fn pass_to_qt(&mut self, a: *mut c_void) {
344343
let pinned = QObjectPinned::new(self);
345-
let r = a as *mut *const ::std::os::raw::c_void;
344+
let r = a as *mut *const c_void;
346345
*r = pinned.get_or_create_cpp_object()
347346
}
348347

349-
unsafe fn read_from_qt(_a: *const ::std::os::raw::c_void) -> Self {
348+
unsafe fn read_from_qt(_a: *const c_void) -> Self {
350349
panic!("Cannot write into an Object property");
351350
}
352351
}
@@ -355,21 +354,21 @@ impl<T> PropertyType for QPointer<T>
355354
where
356355
T: QObject,
357356
{
358-
fn register_type(_name: &::std::ffi::CStr) -> i32 {
357+
fn register_type(_name: &CStr) -> i32 {
359358
register_metatype_qobject::<T>()
360359
}
361360

362-
unsafe fn pass_to_qt(&mut self, a: *mut ::std::os::raw::c_void) {
361+
unsafe fn pass_to_qt(&mut self, a: *mut c_void) {
363362
let pinned = self.as_pinned();
364-
let r = a as *mut *const ::std::os::raw::c_void;
363+
let r = a as *mut *const c_void;
365364
match pinned {
366365
Some(pinned) => *r = pinned.get_or_create_cpp_object(),
367366
None => *r = std::ptr::null(),
368367
}
369368
}
370369

371-
unsafe fn read_from_qt(a: *const ::std::os::raw::c_void) -> Self {
372-
let r = a as *const *mut ::std::os::raw::c_void;
370+
unsafe fn read_from_qt(a: *const c_void) -> Self {
371+
let r = a as *const *mut c_void;
373372
if a.is_null() || (*r).is_null() {
374373
Self::default()
375374
} else {
@@ -387,7 +386,7 @@ fn test_qmetatype() {
387386
}
388387
impl QMetaType for MyInt {}
389388

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

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

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

423422
#[test]

qmetaobject/src/qtdeclarative.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FO
1515
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1616
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1717
*/
18-
use super::scenegraph::*;
19-
use super::*;
18+
use crate::*;
19+
use crate::scenegraph::*;
2020

2121
/// Qt is not thread safe, and the engine can only be created once and in one thread.
2222
/// So this is a guard that will be used to panic if the engine is created twice
@@ -80,8 +80,6 @@ cpp_class!(
8080
impl QmlEngine {
8181
/// Create a new QmlEngine
8282
pub fn new() -> QmlEngine {
83-
use std::ffi::CString;
84-
8583
let mut arguments: Vec<*mut c_char> = std::env::args()
8684
.map(|arg| CString::new(arg.into_bytes()).expect("argument contains invalid c-string!"))
8785
.map(|arg| arg.into_raw())
@@ -387,10 +385,10 @@ impl QmlComponent {
387385
///
388386
/// Refer to the Qt documentation for qmlRegisterType.
389387
pub fn qml_register_type<T: QObject + Default + Sized>(
390-
uri: &std::ffi::CStr,
388+
uri: &CStr,
391389
version_major: u32,
392390
version_minor: u32,
393-
qml_name: &std::ffi::CStr,
391+
qml_name: &CStr,
394392
) {
395393
let uri_ptr = uri.as_ptr();
396394
let qml_name_ptr = qml_name.as_ptr();
@@ -517,10 +515,10 @@ pub trait QSingletonInit {
517515
///
518516
/// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType-3
519517
pub fn qml_register_singleton_type<T: QObject + QSingletonInit + Sized + Default>(
520-
uri: &std::ffi::CStr,
518+
uri: &CStr,
521519
version_major: u32,
522520
version_minor: u32,
523-
qml_name: &std::ffi::CStr,
521+
qml_name: &CStr,
524522
) {
525523
let uri_ptr = uri.as_ptr();
526524
let qml_name_ptr = qml_name.as_ptr();
@@ -615,10 +613,10 @@ pub fn qml_register_singleton_type<T: QObject + QSingletonInit + Sized + Default
615613
// XXX: replace link with real documentation, when it will be generated.
616614
#[cfg(qt_5_14)]
617615
pub fn qml_register_singleton_instance<T: QObject + Sized + Default>(
618-
uri: &std::ffi::CStr,
616+
uri: &CStr,
619617
version_major: u32,
620618
version_minor: u32,
621-
type_name: &std::ffi::CStr,
619+
type_name: &CStr,
622620
obj: T,
623621
) {
624622
let uri_ptr = uri.as_ptr();
@@ -654,10 +652,10 @@ pub fn qml_register_singleton_instance<T: QObject + Sized + Default>(
654652
/// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterUncreatableMetaObject
655653
#[cfg(qt_5_8)]
656654
pub fn qml_register_enum<T: QEnum>(
657-
uri: &std::ffi::CStr,
655+
uri: &CStr,
658656
version_major: u32,
659657
version_minor: u32,
660-
qml_name: &std::ffi::CStr,
658+
qml_name: &CStr,
661659
) {
662660
let uri_ptr = uri.as_ptr();
663661
let qml_name_ptr = qml_name.as_ptr();
@@ -975,14 +973,15 @@ mod qjsvalue_tests {
975973
///
976974
/// ```
977975
/// # extern crate qmetaobject; use qmetaobject::*;
976+
/// # use std::ffi::CStr;
978977
/// #[derive(Default, QObject)]
979978
/// struct QExampleQmlPlugin {
980979
/// base: qt_base_class!(trait QQmlExtensionPlugin),
981980
/// plugin: qt_plugin!("org.qt-project.Qt.QQmlExtensionInterface/1.0"),
982981
/// }
983982
///
984983
/// impl QQmlExtensionPlugin for QExampleQmlPlugin {
985-
/// fn register_types(&mut self, uri: &std::ffi::CStr) {
984+
/// fn register_types(&mut self, uri: &CStr) {
986985
/// // call `qml_register_type` here
987986
/// }
988987
/// }
@@ -1002,7 +1001,7 @@ pub trait QQmlExtensionPlugin: QObject {
10021001
}
10031002

10041003
/// Refer to the Qt documentation of QQmlExtensionPlugin::registerTypes
1005-
fn register_types(&mut self, uri: &std::ffi::CStr);
1004+
fn register_types(&mut self, uri: &CStr);
10061005
}
10071006

10081007
cpp! {{
@@ -1013,9 +1012,9 @@ cpp! {{
10131012
void registerTypes(const char *uri) override {
10141013
rust!(Rust_QQmlExtensionPlugin_registerTypes[
10151014
rust_object: QObjectPinned<dyn QQmlExtensionPlugin> as "TraitObject",
1016-
uri: *const std::os::raw::c_char as "const char *"
1015+
uri: *const c_char as "const char *"
10171016
] {
1018-
rust_object.borrow_mut().register_types(unsafe { std::ffi::CStr::from_ptr(uri) });
1017+
rust_object.borrow_mut().register_types(unsafe { CStr::from_ptr(uri) });
10191018
});
10201019
}
10211020
};

0 commit comments

Comments
 (0)