adding rust to the project

This commit is contained in:
Chris Cochrun 2022-12-08 15:19:53 -06:00
parent 02c3e84cb6
commit 8a7d6da991
4 changed files with 86 additions and 0 deletions

24
src/rust/Cargo.toml Normal file
View file

@ -0,0 +1,24 @@
[package]
name = "qml-minimal"
version = "0.1.0"
edition = "2021"
authors = [
"Chris Cochrun <chris@cochrun.xyz>"
]
license = "MIT OR Apache-2.0"
# This will instruct Cargo to create a static
# library which CMake can link against
[lib]
crate-type = ["staticlib"]
[dependencies]
cxx = "1.0.83"
cxx-qt = "0.4.1"
cxx-qt-lib = "0.4.1"
# cxx-qt-build generates C++ code from the `#[cxx_qt::bridge]` module
# and compiles it together with the Rust static library
[build-dependencies]
cxx-qt-build = "0.4.1"

5
src/rust/build.rs Normal file
View file

@ -0,0 +1,5 @@
use cxx_qt_build::CxxQtBuilder;
fn main() {
CxxQtBuilder::new().file("src/cxxqt_object.rs").build();
}

View file

@ -0,0 +1,41 @@
#[cxx_qt::bridge]
mod my_object {
unsafe extern "C++" {
include!("cxx-qt-lib/qstring.h");
type QString = cxx_qt_lib::QString;
}
#[cxx_qt::qobject]
pub struct MyObject {
#[qproperty]
number: i32,
#[qproperty]
string: QString,
}
impl Default for MyObject {
fn default() -> Self {
Self {
number: 0,
string: QString::from(""),
}
}
}
impl qobject::MyObject {
#[qinvokable]
pub fn increment_number(self: Pin<&mut Self>) {
let previous = *self.as_ref().number();
self.set_number(previous + 1);
}
#[qinvokable]
pub fn say_hi(&self, string: &QString, number: i32) {
println!(
"Hi from Rust! String is '{}' and number is {}",
string, number
);
}
}
}

16
src/rust/src/lib.rs Normal file
View file

@ -0,0 +1,16 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
mod cxxqt_object;