Skip to content

Commit

Permalink
transition paths to 2018 edition
Browse files Browse the repository at this point in the history
Replaces all `extern crate ...` and especially #[macro_use] statements
with direct `use`s of relevant items.

Introduces cstr! crate & macro to examples, because we don't want our dear
users/readers to suffer with raw CStr nightmare:
    std::ffi::CStr::from_bytes_with_nul(b"...\0").unwrap()
  • Loading branch information
ratijas committed Apr 28, 2021
1 parent a2ae6c5 commit f4f80b2
Show file tree
Hide file tree
Showing 28 changed files with 70 additions and 70 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion examples/graph/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions examples/qmlextensionplugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ crate-type = ["cdylib"]
[dependencies]
qmetaobject = { path = "../../qmetaobject"}
chrono = "^0.4"
cstr = "0.2"
19 changes: 8 additions & 11 deletions examples/qmlextensionplugins/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<bool>,
Expand Down Expand Up @@ -75,13 +77,8 @@ struct QExampleQmlPlugin {
}

impl QQmlExtensionPlugin for QExampleQmlPlugin {
fn register_types(&mut self, uri: &std::ffi::CStr) {
fn register_types(&mut self, uri: &CStr) {
//assert_eq!(uri, std::ffi::CStr::from_bytes_with_nul(b"TimeExample\0"));
qml_register_type::<TimeModel>(
uri,
1,
0,
std::ffi::CStr::from_bytes_with_nul(b"Time\0").unwrap(),
);
qml_register_type::<TimeModel>(uri, 1, 0, cstr!("Time"));
}
}
1 change: 1 addition & 0 deletions examples/todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ authors = ["Olivier Goffart <ogoffart@woboq.com>"]

[dependencies]
qmetaobject = { path = "../../qmetaobject"}
cstr = "0.2"
3 changes: 2 additions & 1 deletion examples/todos/src/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use qmetaobject::*;
use std::collections::HashMap;

use qmetaobject::*;

#[derive(Default, Clone)]
struct TodosItem {
completed: bool,
Expand Down
12 changes: 3 additions & 9 deletions examples/todos/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate qmetaobject;
use qmetaobject::*;
use cstr::cstr;

use std::ffi::CStr;
use qmetaobject::*;

mod implementation;

Expand All @@ -13,12 +12,7 @@ qrc!(my_resource,

fn main() {
my_resource();
qml_register_type::<implementation::Todos>(
CStr::from_bytes_with_nul(b"RustCode\0").unwrap(),
1,
0,
CStr::from_bytes_with_nul(b"Todos\0").unwrap(),
);
qml_register_type::<implementation::Todos>(cstr!("RustCode"), 1, 0, cstr!("Todos"));
let mut engine = QmlEngine::new();
engine.load_file("qrc:/todos/qml/main.qml".into());
engine.exec();
Expand Down
1 change: 0 additions & 1 deletion examples/webengine/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
extern crate qmetaobject;
use qmetaobject::*;

qrc!(my_resource,
Expand Down
4 changes: 3 additions & 1 deletion qmetaobject/src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -282,7 +284,7 @@ impl<Args> Signal<Args> {
/// # Example
///
/// ```
/// #[macro_use] extern crate cpp;
/// use cpp::cpp;
/// use qmetaobject::*;
///
/// fn object_name_changed() -> Signal<fn(QString)> {
Expand Down
2 changes: 2 additions & 0 deletions qmetaobject/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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(
Expand Down
2 changes: 2 additions & 0 deletions qmetaobject/src/itemmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ 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
Expand Down
11 changes: 4 additions & 7 deletions qmetaobject/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Example:
```
#[macro_use] extern crate cstr;
use cstr::cstr;
use qmetaobject::*;
// The `QObject` custom derive macro allows to expose a class to Qt and QML
Expand Down Expand Up @@ -155,18 +154,14 @@ 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;

#[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)*) } }
Expand All @@ -175,6 +170,8 @@ 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::*;
Expand Down
5 changes: 4 additions & 1 deletion qmetaobject/src/listmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions qmetaobject/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use std::ffi::CStr;
use std::os::raw::c_char;

use cpp::{cpp, cpp_class};

#[cfg(feature = "log")]
use log::{logger, Level, Record, RecordBuilder};

Expand Down
2 changes: 2 additions & 0 deletions qmetaobject/src/qmetatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +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 cpp::cpp;

use super::*;

fn register_metatype_common<T: QMetaType>(
Expand Down
1 change: 1 addition & 0 deletions qmetaobject/src/qrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 *,
Expand Down
2 changes: 2 additions & 0 deletions qmetaobject/src/qtdeclarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +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 cpp::{cpp, cpp_class};

use crate::scenegraph::*;
use crate::*;

Expand Down
3 changes: 1 addition & 2 deletions qmetaobject/src/scenegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

use super::*;

Expand Down Expand Up @@ -118,7 +119,6 @@ impl SGNode<ContainerNode> {
/// call reset() if you want to change the size.
///
/// ```
/// extern crate qttypes;
/// # use qmetaobject::{QObject, qtdeclarative::QQuickItem};
/// use qmetaobject::scenegraph::{SGNode, ContainerNode, RectangleNode};
/// use qttypes::QRectF;
Expand Down Expand Up @@ -181,7 +181,6 @@ impl SGNode<ContainerNode> {
/// In this example, the node has two children node
///
/// ```
/// extern crate qttypes;
/// # use qmetaobject::{QObject, qtdeclarative::QQuickItem};
/// use qmetaobject::scenegraph::{SGNode, ContainerNode, RectangleNode};
/// use qttypes::QRectF;
Expand Down
5 changes: 4 additions & 1 deletion qmetaobject/src/tablemodel.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions qmetaobject/src/webengine.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use cpp::cpp;

cpp! {{
#include <QtWebEngine/QtWebEngine>
}}
Expand Down
3 changes: 2 additions & 1 deletion qmetaobject/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>> = Mutex::new(Vec::new());
Expand Down
8 changes: 3 additions & 5 deletions qmetaobject/tests/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 2 additions & 6 deletions qmetaobject/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@ 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 qmetaobject::*;

mod common;
use common::*;
use self::common::*;

#[test]
fn self_test() {
Expand Down
11 changes: 3 additions & 8 deletions qmetaobject_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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") {
Expand Down
1 change: 0 additions & 1 deletion qmetaobject_impl/src/qbjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
14 changes: 8 additions & 6 deletions qmetaobject_impl/src/qobject_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ 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 proc_macro::TokenStream;
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) => {
Expand Down Expand Up @@ -315,7 +317,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;

Expand Down Expand Up @@ -989,7 +991,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;

Expand Down
Loading

0 comments on commit f4f80b2

Please sign in to comment.