diff --git a/README.md b/README.md index 94b345a3..5294f387 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,11 @@ Presentation Blog Post: https://woboq.com/blog/qmetaobject-from-rust.html ## Overview ```rust -#[macro_use] extern crate cstr; -extern crate qmetaobject; - +use cstr::cstr; use qmetaobject::*; // The `QObject` custom derive macro allows to expose a class to Qt and QML -#[derive(QObject,Default)] +#[derive(QObject, Default)] struct Greeter { // Specify the base class with the qt_base_class macro base: qt_base_class!(trait QObject), diff --git a/examples/graph/build.rs b/examples/graph/build.rs index dfa24caf..7e265e3d 100644 --- a/examples/graph/build.rs +++ b/examples/graph/build.rs @@ -15,7 +15,6 @@ 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. */ -extern crate cpp_build; fn main() { let qt_include_path = std::env::var("DEP_QT_INCLUDE_PATH").unwrap(); diff --git a/examples/graph/src/main.rs b/examples/graph/src/main.rs index 7b1ff0a3..1fcb28b9 100644 --- a/examples/graph/src/main.rs +++ b/examples/graph/src/main.rs @@ -1,13 +1,11 @@ #![allow(non_snake_case)] #![allow(unused_variables)] -#[macro_use] -extern crate qmetaobject; + +use cpp::cpp; +use cstr::cstr; + use qmetaobject::scenegraph::*; use qmetaobject::*; -#[macro_use] -extern crate cstr; -#[macro_use] -extern crate cpp; mod nodes; diff --git a/examples/graph/src/nodes.rs b/examples/graph/src/nodes.rs index 0dc80ad7..0c08c544 100644 --- a/examples/graph/src/nodes.rs +++ b/examples/graph/src/nodes.rs @@ -1,5 +1,8 @@ +use cpp::cpp; + use qmetaobject::scenegraph::SGNode; -use qmetaobject::{QColor, QQuickItem, QRectF}; +use qmetaobject::{qrc, QQuickItem}; +use qttypes::{QColor, QRectF}; qrc! { init_resource, diff --git a/examples/qmlextensionplugins/Cargo.toml b/examples/qmlextensionplugins/Cargo.toml index a03c71e4..4bdc4575 100644 --- a/examples/qmlextensionplugins/Cargo.toml +++ b/examples/qmlextensionplugins/Cargo.toml @@ -9,5 +9,6 @@ name = "qmlqtimeexampleplugin" crate-type = ["cdylib"] [dependencies] -qmetaobject = { path = "../../qmetaobject"} +qmetaobject = { path = "../../qmetaobject" } chrono = "^0.4" +cstr = "0.2" diff --git a/examples/qmlextensionplugins/src/lib.rs b/examples/qmlextensionplugins/src/lib.rs index c4d6f33a..ca91c958 100644 --- a/examples/qmlextensionplugins/src/lib.rs +++ b/examples/qmlextensionplugins/src/lib.rs @@ -1,10 +1,12 @@ -extern crate qmetaobject; -use qmetaobject::*; -extern crate chrono; -use chrono::Timelike; +use std::ffi::CStr; use std::sync::{Arc, Condvar, Mutex}; use std::thread::JoinHandle; +use chrono::Timelike; +use cstr::cstr; + +use qmetaobject::*; + #[derive(Default)] struct AbortCondVar { is_aborted: Mutex, @@ -75,13 +77,8 @@ struct QExampleQmlPlugin { } impl QQmlExtensionPlugin for QExampleQmlPlugin { - fn register_types(&mut self, uri: &std::ffi::CStr) { - //assert_eq!(uri, std::ffi::CStr::from_bytes_with_nul(b"TimeExample\0")); - qml_register_type::( - uri, - 1, - 0, - std::ffi::CStr::from_bytes_with_nul(b"Time\0").unwrap(), - ); + fn register_types(&mut self, uri: &CStr) { + //assert_eq!(uri, cstr!("TimeExample")); + qml_register_type::(uri, 1, 0, cstr!("Time")); } } diff --git a/examples/todos/Cargo.toml b/examples/todos/Cargo.toml index d7992108..7c876b65 100644 --- a/examples/todos/Cargo.toml +++ b/examples/todos/Cargo.toml @@ -5,4 +5,5 @@ edition = "2018" authors = ["Olivier Goffart "] [dependencies] -qmetaobject = { path = "../../qmetaobject"} +qmetaobject = { path = "../../qmetaobject" } +cstr = "0.2" diff --git a/examples/todos/src/implementation.rs b/examples/todos/src/implementation.rs index e017fa18..6af70933 100644 --- a/examples/todos/src/implementation.rs +++ b/examples/todos/src/implementation.rs @@ -18,9 +18,10 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -use qmetaobject::*; use std::collections::HashMap; +use qmetaobject::*; + #[derive(Default, Clone)] struct TodosItem { completed: bool, diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index c7fa32b8..1de20310 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -1,7 +1,6 @@ -extern crate qmetaobject; -use qmetaobject::*; +use cstr::cstr; -use std::ffi::CStr; +use qmetaobject::*; mod implementation; @@ -13,12 +12,7 @@ qrc!(my_resource, fn main() { my_resource(); - qml_register_type::( - CStr::from_bytes_with_nul(b"RustCode\0").unwrap(), - 1, - 0, - CStr::from_bytes_with_nul(b"Todos\0").unwrap(), - ); + qml_register_type::(cstr!("RustCode"), 1, 0, cstr!("Todos")); let mut engine = QmlEngine::new(); engine.load_file("qrc:/todos/qml/main.qml".into()); engine.exec(); diff --git a/examples/webengine/src/main.rs b/examples/webengine/src/main.rs index e897a98e..887cb501 100644 --- a/examples/webengine/src/main.rs +++ b/examples/webengine/src/main.rs @@ -1,4 +1,3 @@ -extern crate qmetaobject; use qmetaobject::*; qrc!(my_resource, diff --git a/qmetaobject/src/connections.rs b/qmetaobject/src/connections.rs index 6fd157f8..a058db5e 100644 --- a/qmetaobject/src/connections.rs +++ b/qmetaobject/src/connections.rs @@ -84,6 +84,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #![deny(missing_docs)] use std::os::raw::c_void; +use cpp::{cpp, cpp_class}; + use super::*; /// Inner functor type of a `QRustClosureSlotObject` class. @@ -282,8 +284,9 @@ impl Signal { /// # Example /// /// ``` - /// # #[macro_use] extern crate cpp; - /// # use qmetaobject::*; + /// use cpp::cpp; + /// use qmetaobject::*; + /// /// fn object_name_changed() -> Signal { /// unsafe { /// Signal::new(cpp!([] -> SignalInner as "SignalInner" { diff --git a/qmetaobject/src/future.rs b/qmetaobject/src/future.rs index c68d7e89..88ab6243 100644 --- a/qmetaobject/src/future.rs +++ b/qmetaobject/src/future.rs @@ -1,8 +1,11 @@ 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}; +use cpp::cpp; + use crate::connections::SignalArgArrayToTuple; static QT_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new( @@ -90,7 +93,7 @@ cpp! {{ ~Waker() { rust!(QtDestroyFuture [future: *mut dyn Future as "TraitObject"] { - std::mem::drop(Box::from_raw(future)) + drop(Box::from_raw(future)); }); } }; @@ -160,7 +163,7 @@ pub unsafe fn wait_on_signal( type Output = ::Tuple; fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll { 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); } @@ -181,7 +184,7 @@ pub unsafe fn wait_on_signal( for *mut ConnectionFutureState { 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) }, ) { diff --git a/qmetaobject/src/itemmodel.rs b/qmetaobject/src/itemmodel.rs index a2deede9..10391e11 100644 --- a/qmetaobject/src/itemmodel.rs +++ b/qmetaobject/src/itemmodel.rs @@ -15,9 +15,10 @@ 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 std::collections::HashMap; +use cpp::cpp; + use crate::*; /// This trait allow to override a Qt QAbstractItemModel @@ -58,6 +59,7 @@ pub trait QAbstractItemModel: QObject { fn role_names(&self) -> HashMap { HashMap::new() } + /// Refer to the Qt documentation of QAbstractListModel::beginInsertRows fn begin_insert_rows(&self, parent: QModelIndex, first: i32, last: i32) { let obj = self.get_cpp_object(); diff --git a/qmetaobject/src/lib.rs b/qmetaobject/src/lib.rs index 90c706eb..e0073069 100644 --- a/qmetaobject/src/lib.rs +++ b/qmetaobject/src/lib.rs @@ -21,9 +21,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # Example: ``` - #[macro_use] extern crate cstr; - extern crate qmetaobject; - + use cstr::cstr; use qmetaobject::*; // The `QObject` custom derive macro allows to expose a class to Qt and QML @@ -88,7 +86,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); @@ -115,19 +114,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 || { @@ -137,9 +137,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() @@ -154,28 +154,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #![cfg_attr(feature = "cargo-clippy", allow(clippy::needless_pass_by_value))] // Too many of that for qt types. (FIXME) #![cfg_attr(feature = "cargo-clippy", allow(clippy::cognitive_complexity))] -#[macro_use] -extern crate cpp; - -#[allow(unused_imports)] -#[macro_use] -extern crate qmetaobject_impl; #[doc(hidden)] pub use qmetaobject_impl::*; // In order to be able to use the lazy_static macro from the QObject custom derive, we re-export // it under a new name qmetaobject_lazy_static. -extern crate lazy_static; #[allow(unused_imports)] #[doc(hidden)] -pub use lazy_static::*; +pub use lazy_static::lazy_static; #[doc(hidden)] #[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}; +use cpp::{cpp, cpp_class}; + pub use qttypes; pub use crate::log::*; @@ -440,11 +436,11 @@ impl Clone for QPointer { /// 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 { @@ -543,7 +539,7 @@ impl QObjectBox { pub fn into_leaked_cpp_ptr(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 } @@ -640,7 +636,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), @@ -670,7 +667,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), @@ -696,7 +694,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), @@ -726,7 +725,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), @@ -748,15 +748,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] @@ -841,8 +842,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(); /// ``` @@ -989,10 +990,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", @@ -1016,6 +1016,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); diff --git a/qmetaobject/src/listmodel.rs b/qmetaobject/src/listmodel.rs index 97191ee6..99209fde 100644 --- a/qmetaobject/src/listmodel.rs +++ b/qmetaobject/src/listmodel.rs @@ -16,11 +16,14 @@ OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -use super::*; use std::collections::HashMap; use std::iter::FromIterator; use std::ops::Index; +use cpp::cpp; + +use super::*; + /// This trait allow to override a Qt QAbstractListModel pub trait QAbstractListModel: QObject { /// Required for the implementation detail of the QObject custom derive @@ -223,7 +226,7 @@ where QVariant::default() } } - fn role_names(&self) -> std::collections::HashMap { + fn role_names(&self) -> HashMap { T::names().iter().enumerate().map(|(i, x)| (i as i32 + USER_ROLE, x.clone())).collect() } } diff --git a/qmetaobject/src/log.rs b/qmetaobject/src/log.rs index 8085518f..b92c846f 100644 --- a/qmetaobject/src/log.rs +++ b/qmetaobject/src/log.rs @@ -1,7 +1,10 @@ //! Logging facilities and forwarding +use std::ffi::CStr; use std::os::raw::c_char; +use cpp::{cpp, cpp_class}; + #[cfg(feature = "log")] use log::{logger, Level, Record, RecordBuilder}; @@ -33,7 +36,7 @@ impl QMessageLogContext { if x.is_null() { return ""; } - std::ffi::CStr::from_ptr(x).to_str().unwrap() + CStr::from_ptr(x).to_str().unwrap() } } @@ -46,7 +49,7 @@ impl QMessageLogContext { if x.is_null() { return ""; } - std::ffi::CStr::from_ptr(x).to_str().unwrap() + CStr::from_ptr(x).to_str().unwrap() } } @@ -59,7 +62,7 @@ impl QMessageLogContext { if x.is_null() { return ""; } - std::ffi::CStr::from_ptr(x).to_str().unwrap() + CStr::from_ptr(x).to_str().unwrap() } } } diff --git a/qmetaobject/src/qmetatype.rs b/qmetaobject/src/qmetatype.rs index 707a266b..695592c7 100644 --- a/qmetaobject/src/qmetatype.rs +++ b/qmetaobject/src/qmetatype.rs @@ -15,15 +15,16 @@ 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 cpp::cpp; + use super::*; fn register_metatype_common( - 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! { @@ -164,7 +165,8 @@ fn register_metatype_qobject() -> i32 { /// as a parameter of a qt_method! /// /// ``` -/// # use ::qmetaobject::QMetaType; +/// use qmetaobject::QMetaType; +/// /// #[derive(Default, Clone)] /// struct MyStruct(u32, String); /// @@ -176,7 +178,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::( name.map_or(std::ptr::null(), |x| x.as_ptr()), std::ptr::null(), @@ -240,7 +242,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 } } @@ -275,7 +277,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 { @@ -305,7 +307,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; @@ -315,7 +317,7 @@ impl PropertyType for T where T: QMetaType, { - fn register_type(name: &std::ffi::CStr) -> i32 { + fn register_type(name: &CStr) -> i32 { ::register(Some(name)) } @@ -332,21 +334,21 @@ where } } -impl PropertyType for ::std::cell::RefCell +impl PropertyType for RefCell where T: QObject, { - fn register_type(_name: &::std::ffi::CStr) -> i32 { + fn register_type(_name: &CStr) -> i32 { register_metatype_qobject::() } - 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"); } } @@ -355,21 +357,21 @@ impl PropertyType for QPointer where T: QObject, { - fn register_type(_name: &::std::ffi::CStr) -> i32 { + fn register_type(_name: &CStr) -> i32 { register_metatype_qobject::() } - 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 { @@ -387,7 +389,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 }; @@ -406,7 +408,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] @@ -415,9 +417,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] diff --git a/qmetaobject/src/qrc.rs b/qmetaobject/src/qrc.rs index 82ca67eb..0126afd7 100644 --- a/qmetaobject/src/qrc.rs +++ b/qmetaobject/src/qrc.rs @@ -15,6 +15,7 @@ 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 cpp::cpp; cpp! {{ Q_CORE_EXPORT bool qRegisterResourceData(int, const unsigned char *, diff --git a/qmetaobject/src/qtdeclarative.rs b/qmetaobject/src/qtdeclarative.rs index 1e2f49b9..a14492af 100644 --- a/qmetaobject/src/qtdeclarative.rs +++ b/qmetaobject/src/qtdeclarative.rs @@ -15,8 +15,10 @@ 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 cpp::{cpp, cpp_class}; + +use crate::scenegraph::*; +use crate::*; /// 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 @@ -80,8 +82,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()) @@ -387,10 +387,10 @@ impl QmlComponent { /// /// Refer to the Qt documentation for qmlRegisterType. pub fn qml_register_type( - 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(); @@ -517,10 +517,10 @@ pub trait QSingletonInit { /// /// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType-3 pub fn qml_register_singleton_type( - 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(); @@ -615,10 +615,10 @@ pub fn qml_register_singleton_type( - 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(); @@ -654,10 +654,10 @@ pub fn qml_register_singleton_instance( /// [qt]: https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterUncreatableMetaObject #[cfg(qt_5_8)] pub fn qml_register_enum( - 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(); @@ -974,7 +974,9 @@ mod qjsvalue_tests { /// See also the 'qmlextensionplugins' example. /// /// ``` -/// # extern crate qmetaobject; use qmetaobject::*; +/// use qmetaobject::*; +/// use std::ffi::CStr; +/// /// #[derive(Default, QObject)] /// struct QExampleQmlPlugin { /// base: qt_base_class!(trait QQmlExtensionPlugin), @@ -982,12 +984,11 @@ mod qjsvalue_tests { /// } /// /// 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 /// } /// } /// ``` - pub trait QQmlExtensionPlugin: QObject { #[doc(hidden)] // implementation detail for the QObject custom derive fn get_object_description() -> &'static QObjectDescription @@ -1002,7 +1003,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! {{ @@ -1013,9 +1014,9 @@ cpp! {{ void registerTypes(const char *uri) override { rust!(Rust_QQmlExtensionPlugin_registerTypes[ rust_object: QObjectPinned 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) }); }); } }; diff --git a/qmetaobject/src/scenegraph.rs b/qmetaobject/src/scenegraph.rs index ecd45985..f99ec78f 100644 --- a/qmetaobject/src/scenegraph.rs +++ b/qmetaobject/src/scenegraph.rs @@ -15,10 +15,9 @@ 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 cpp::cpp; -#[cfg(qt_5_8)] use super::*; -use std::os::raw::c_void; /// A typed node in the scene graph /// @@ -120,12 +119,14 @@ impl SGNode { /// call reset() if you want to change the size. /// /// ``` - /// # use qmetaobject::{QRectF, QObject, qtdeclarative::QQuickItem}; - /// # use qmetaobject::scenegraph::{SGNode, ContainerNode, RectangleNode}; + /// # use qmetaobject::{QObject, qtdeclarative::QQuickItem}; + /// use qmetaobject::scenegraph::{SGNode, ContainerNode, RectangleNode}; + /// use qttypes::QRectF; + /// /// # struct Dummy { items : Vec, _phantom :T } /// # impl QQuickItem for Dummy where Dummy : QObject { /// // in the reimplementation of QQuickItem::update_paint_node - /// fn update_paint_node(&mut self, mut node : SGNode ) -> SGNode { + /// fn update_paint_node(&mut self, mut node: SGNode) -> SGNode { /// let items : &Vec = &self.items; /// node.update_dynamic(items.iter(), /// |i, mut n| -> SGNode { n.create(self); n.set_rect(*i); n }); @@ -180,8 +181,10 @@ impl SGNode { /// In this example, the node has two children node /// /// ``` - /// # use qmetaobject::{QRectF, QObject, qtdeclarative::QQuickItem}; - /// # use qmetaobject::scenegraph::{SGNode, ContainerNode, RectangleNode}; + /// # use qmetaobject::{QObject, qtdeclarative::QQuickItem}; + /// use qmetaobject::scenegraph::{SGNode, ContainerNode, RectangleNode}; + /// use qttypes::QRectF; + /// /// # struct Dummy { items : Vec, _phantom :T } /// # impl QQuickItem for Dummy where Dummy : QObject { /// // in the reimplementation of QQuickItem::update_paint_node @@ -339,9 +342,10 @@ impl SGNode { pub enum RectangleNode {} cpp! {{ -#if QT_VERSION < QT_VERSION_CHECK(5, 8, 0) + // Just a stub for compatibility + #if QT_VERSION < QT_VERSION_CHECK(5, 8, 0) struct QSGRectangleNode{}; -#endif + #endif }} #[cfg(qt_5_8)] @@ -349,18 +353,18 @@ impl SGNode { pub fn set_color(&mut self, color: QColor) { let raw = self.raw; cpp!(unsafe [raw as "QSGRectangleNode*", color as "QColor"] { - #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) - if(raw) raw->setColor(color); - #endif - }); + #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) + if(raw) raw->setColor(color); + #endif + }); } pub fn set_rect(&mut self, rect: QRectF) { let raw = self.raw; cpp!(unsafe [raw as "QSGRectangleNode*", rect as "QRectF"] { - #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) - if (raw) raw->setRect(rect); - #endif - }); + #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) + if (raw) raw->setRect(rect); + #endif + }); } pub fn create(&mut self, item: &dyn QQuickItem) { @@ -369,13 +373,13 @@ impl SGNode { } let item = item.get_cpp_object(); self.raw = cpp!(unsafe [item as "QQuickItem*"] -> *mut c_void as "void*" { - if (!item) return nullptr; - #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) - if (auto window = item->window()) - return window->createRectangleNode(); - #endif - return nullptr; - }); + #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) + if (!item) return nullptr; + if (auto window = item->window()) + return window->createRectangleNode(); + #endif + return nullptr; + }); } } diff --git a/qmetaobject/src/tablemodel.rs b/qmetaobject/src/tablemodel.rs index a6fb7cea..a808c0e0 100644 --- a/qmetaobject/src/tablemodel.rs +++ b/qmetaobject/src/tablemodel.rs @@ -1,6 +1,9 @@ -use super::*; use std::collections::HashMap; +use cpp::cpp; + +use super::*; + pub trait QAbstractTableModel: QObject { fn get_object_description() -> &'static QObjectDescription where diff --git a/qmetaobject/src/webengine.rs b/qmetaobject/src/webengine.rs index b3df0397..bc5c3df9 100644 --- a/qmetaobject/src/webengine.rs +++ b/qmetaobject/src/webengine.rs @@ -1,3 +1,5 @@ +use cpp::cpp; + cpp! {{ #include }} diff --git a/qmetaobject/tests/common/mod.rs b/qmetaobject/tests/common/mod.rs index bcbd1d2b..19e4ce63 100644 --- a/qmetaobject/tests/common/mod.rs +++ b/qmetaobject/tests/common/mod.rs @@ -18,10 +18,11 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #![allow(dead_code)] -use qmetaobject::*; use std::cell::RefCell; use std::sync::Mutex; +use qmetaobject::*; + lazy_static! { pub static ref TEST_MUTEX: Mutex<()> = Mutex::new(()); pub static ref QML_LOGS: Mutex> = Mutex::new(Vec::new()); diff --git a/qmetaobject/tests/models.rs b/qmetaobject/tests/models.rs index 2697f30d..042dfe7d 100644 --- a/qmetaobject/tests/models.rs +++ b/qmetaobject/tests/models.rs @@ -15,15 +15,13 @@ 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 std::cell::RefCell; +use std::iter::FromIterator; -extern crate qmetaobject; use qmetaobject::*; mod common; -use common::*; - -use std::cell::RefCell; -use std::iter::FromIterator; +use self::common::*; #[test] fn simple_model() { diff --git a/qmetaobject/tests/tests.rs b/qmetaobject/tests/tests.rs index e12d6d84..daf4a0b6 100644 --- a/qmetaobject/tests/tests.rs +++ b/qmetaobject/tests/tests.rs @@ -15,18 +15,15 @@ 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. */ -extern crate qmetaobject; -use qmetaobject::*; - -extern crate lazy_static; use std::cell::RefCell; use std::ffi::CStr; use std::rc::Rc; -extern crate tempfile; +use if_rust_version::if_rust_version; +use qmetaobject::*; mod common; -use common::*; +use self::common::*; #[test] fn self_test() { @@ -552,7 +549,7 @@ fn connect_rust_signal() { let mut result = None; let mut result2 = None; let mut con = unsafe { - qmetaobject::connections::connect( + connect( obj_ptr, f.borrow().my_signal.to_cpp_representation(&*f.borrow()), |xx: &u32, yy: &String| { @@ -563,7 +560,7 @@ fn connect_rust_signal() { assert!(con.is_valid()); let con2 = unsafe { - qmetaobject::connections::connect( + connect( obj_ptr, f.borrow().my_signal2.to_cpp_representation(&*f.borrow()), |yy: &String| { @@ -598,13 +595,9 @@ fn connect_cpp_signal() { let obj_ptr = unsafe { QObjectPinned::new(&f).get_or_create_cpp_object() }; let mut result = None; let con = unsafe { - qmetaobject::connections::connect( - obj_ptr, - QObject::object_name_changed_signal(), - |name: &QString| { - result = Some(name.clone()); - }, - ) + connect(obj_ptr, QObject::object_name_changed_signal(), |name: &QString| { + result = Some(name.clone()); + }) }; assert!(con.is_valid()); (&*f.borrow() as &dyn QObject).set_object_name("YOYO".into()); @@ -665,7 +658,7 @@ fn qpointer() { { #[derive(Default)] struct XX(QString); - impl qmetaobject::listmodel::SimpleListItem for XX { + impl SimpleListItem for XX { fn get(&self, _idx: i32) -> QVariant { self.0.clone().into() } @@ -673,12 +666,12 @@ fn qpointer() { vec![QByteArray::from("a")] } } - let mut obj = qmetaobject::listmodel::SimpleListModel::::default(); + let mut obj = SimpleListModel::::default(); obj.push(XX("foo".into())); let obj = RefCell::new(obj); unsafe { QObjectPinned::new(&obj).get_or_create_cpp_object() }; - let obj_ref: &dyn qmetaobject::listmodel::QAbstractListModel = &*obj.borrow(); - ptr = QPointer::::from(obj_ref); + let obj_ref: &dyn QAbstractListModel = &*obj.borrow(); + ptr = QPointer::::from(obj_ref); pt2 = ptr.clone(); assert_eq!(ptr.as_ref().map_or(898, |x| x.row_count()), 1); assert_eq!(pt2.as_ref().map_or(898, |x| x.row_count()), 1); @@ -790,7 +783,7 @@ fn threading() { recompute_result: qt_method!( fn recompute_result(&self, name: String) { let qptr = QPointer::from(&*self); - let set_value = qmetaobject::queued_callback(move |val: QString| { + let set_value = queued_callback(move |val: QString| { qptr.as_pinned().map(|self_| { self_.borrow_mut().result = val; self_.borrow().result_changed(); @@ -810,7 +803,7 @@ fn threading() { let obj = std::cell::RefCell::new(MyAsyncObject::default()); let engine = QmlEngine::new(); unsafe { - qmetaobject::connect( + connect( QObject::cpp_construct(&obj), obj.borrow().result_changed.to_cpp_representation(&*obj.borrow()), || engine.quit(), @@ -841,7 +834,7 @@ fn load_data_as() { #[test] fn test_future() { - if_rust_version::if_rust_version!(>= 1.39 { + if_rust_version!(>= 1.39 { let _lock = lock_for_test(); #[derive(QObject, Default)] @@ -858,12 +851,12 @@ fn test_future() { { let result2 = result.clone(); let fut = unsafe { - qmetaobject::future::wait_on_signal( + future::wait_on_signal( obj_ptr, o.borrow().sig_with_args.to_cpp_representation(&*o.borrow()), ) }; - qmetaobject::future::execute_async(async move { + future::execute_async(async move { let (xx, yy) = fut.await; *result2.borrow_mut() = Some(format!("{}={}", yy, xx)); }); @@ -873,13 +866,13 @@ fn test_future() { let engine = Rc::new(QmlEngine::new()); { let fut = unsafe { - qmetaobject::future::wait_on_signal( + future::wait_on_signal( obj_ptr, o.borrow().sig.to_cpp_representation(&*o.borrow()), ) }; let engine2 = engine.clone(); - qmetaobject::future::execute_async(async move { + future::execute_async(async move { fut.await; engine2.quit(); }); @@ -914,7 +907,7 @@ fn create_component() { #[test] fn component_status_changed() { - if_rust_version::if_rust_version!(>= 1.39 { + if_rust_version!(>= 1.39 { let _lock = lock_for_test(); let engine = Rc::new(QmlEngine::new()); let o = Rc::new(RefCell::new(QmlComponent::new(&engine))); diff --git a/qmetaobject_impl/src/lib.rs b/qmetaobject_impl/src/lib.rs index 9cd4dcd1..dd2a00e1 100644 --- a/qmetaobject_impl/src/lib.rs +++ b/qmetaobject_impl/src/lib.rs @@ -22,14 +22,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #![cfg_attr(feature = "cargo-clippy", allow(clippy::unreadable_literal))] // Because we copy-paste constants from Qt #![cfg_attr(feature = "cargo-clippy", allow(clippy::cognitive_complexity))] -#[macro_use] -extern crate syn; -#[macro_use] -extern crate quote; - -extern crate proc_macro; -extern crate proc_macro2; use proc_macro::TokenStream; +use quote::{quote, ToTokens}; +use syn::DeriveInput; mod qbjs; mod qobject_impl; @@ -38,7 +33,7 @@ mod simplelistitem_impl; /// Get the tokens to refer to the qmetaobject crate. By default, return "::qmetaobject" unless /// the QMetaObjectCrate is specified -fn get_crate(input: &syn::DeriveInput) -> impl quote::ToTokens { +fn get_crate(input: &DeriveInput) -> impl ToTokens { for i in input.attrs.iter() { if let Ok(x) = i.parse_meta() { if x.path().is_ident("QMetaObjectCrate") { diff --git a/qmetaobject_impl/src/qbjs.rs b/qmetaobject_impl/src/qbjs.rs index bd0f76c4..afd9ba74 100644 --- a/qmetaobject_impl/src/qbjs.rs +++ b/qmetaobject_impl/src/qbjs.rs @@ -15,7 +15,6 @@ 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 std; // That's the same as parent::write_u32, but it should always be little endian fn write_u32(val: u32) -> [u8; 4] { @@ -33,7 +32,7 @@ fn write_u16(val: u16) -> [u8; 2] { } pub enum Value { - String(std::string::String), + String(String), Double(f64), Bool(bool), // TODO: other things diff --git a/qmetaobject_impl/src/qobject_impl.rs b/qmetaobject_impl/src/qobject_impl.rs index 236a3d34..9fe5fb93 100644 --- a/qmetaobject_impl/src/qobject_impl.rs +++ b/qmetaobject_impl/src/qobject_impl.rs @@ -15,12 +15,12 @@ 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::qbjs; use proc_macro::TokenStream; -use quote::ToTokens; -use std::iter::Iterator; -use syn; +use quote::{quote, ToTokens}; use syn::parse::{Parse, ParseStream, Parser, Result}; +use syn::{parse_macro_input, parse_quote, DeriveInput, Token}; + +use super::qbjs; macro_rules! unwrap_parse_error( ($e:expr) => { @@ -315,7 +315,7 @@ fn map_method_parameters2( } pub fn generate(input: TokenStream, is_qobject: bool) -> TokenStream { - let ast = parse_macro_input!(input as syn::DeriveInput); + let ast = parse_macro_input!(input as DeriveInput); let name = &ast.ident; @@ -989,7 +989,7 @@ fn is_valid_repr_attribute(attribute: &syn::Attribute) -> bool { } pub fn generate_enum(input: TokenStream) -> TokenStream { - let ast = parse_macro_input!(input as syn::DeriveInput); + let ast = parse_macro_input!(input as DeriveInput); let name = &ast.ident; diff --git a/qmetaobject_impl/src/qrc_impl.rs b/qmetaobject_impl/src/qrc_impl.rs index 20e6d15a..ef3aa36b 100644 --- a/qmetaobject_impl/src/qrc_impl.rs +++ b/qmetaobject_impl/src/qrc_impl.rs @@ -15,14 +15,15 @@ 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 proc_macro::TokenStream; use std::collections::BTreeMap; use std::env; use std::fs; use std::path::PathBuf; +use proc_macro::TokenStream; +use quote::quote; use syn::parse::{Parse, ParseStream, Result}; -use syn::{Ident, LitStr, Token, Visibility}; +use syn::{braced, parse_macro_input, Ident, LitStr, Token, Visibility}; /// Function with this name and visibility modifier will be generated by the `qrc!` macro. struct TargetFunc { diff --git a/qmetaobject_impl/src/simplelistitem_impl.rs b/qmetaobject_impl/src/simplelistitem_impl.rs index f0924c3e..0b06af85 100644 --- a/qmetaobject_impl/src/simplelistitem_impl.rs +++ b/qmetaobject_impl/src/simplelistitem_impl.rs @@ -16,23 +16,24 @@ OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ use proc_macro::TokenStream; -use syn; +use quote::quote; +use syn::{parse_macro_input, Data, DeriveInput, Ident, Visibility}; pub fn derive(input: TokenStream) -> TokenStream { - let input = parse_macro_input!(input as syn::DeriveInput); + let input = parse_macro_input!(input as DeriveInput); let crate_ = super::get_crate(&input); - let values = if let syn::Data::Struct(ref data) = input.data { + let values = if let Data::Struct(ref data) = input.data { data.fields .iter() .filter_map(|field| { - if let syn::Visibility::Public(_) = field.vis { + if let Visibility::Public(_) = field.vis { field.ident.clone() } else { None } }) - .collect::>() + .collect::>() } else { panic!("#[derive(SimpleListItem)] is only defined for structs"); };