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 May 5, 2021
1 parent a2ae6c5 commit dc56c9d
Show file tree
Hide file tree
Showing 29 changed files with 96 additions and 100 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
3 changes: 2 additions & 1 deletion examples/graph/src/nodes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cpp::cpp;

use qmetaobject::scenegraph::SGNode;
use qmetaobject::{qrc, QColor, QQuickItem, QRectF};
use qmetaobject::{qrc, QQuickItem};
use qttypes::{QColor, QRectF};

qrc! {
init_resource,
Expand Down
3 changes: 2 additions & 1 deletion examples/qmlextensionplugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ name = "qmlqtimeexampleplugin"
crate-type = ["cdylib"]

[dependencies]
qmetaobject = { path = "../../qmetaobject"}
qmetaobject = { path = "../../qmetaobject" }
chrono = "^0.4"
cstr = "0.2"
21 changes: 9 additions & 12 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) {
//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(),
);
fn register_types(&mut self, uri: &CStr) {
//assert_eq!(uri, cstr!("TimeExample"));
qml_register_type::<TimeModel>(uri, 1, 0, cstr!("Time"));
}
}
3 changes: 2 additions & 1 deletion examples/todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ edition = "2018"
authors = ["Olivier Goffart <ogoffart@woboq.com>"]

[dependencies]
qmetaobject = { path = "../../qmetaobject"}
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
4 changes: 3 additions & 1 deletion qmetaobject/src/itemmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,6 +59,7 @@ pub trait QAbstractItemModel: QObject {
fn role_names(&self) -> HashMap<i32, QByteArray> {
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();
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
Loading

0 comments on commit dc56c9d

Please sign in to comment.