Convert Figma logo to code with AI

rust-qt logoritual

Use C++ libraries from Rust

1,234
49
1,234
41

Top Related Projects

10,376

Qt binding for Go (Golang) with support for Windows / macOS / Linux / FreeBSD / Android / iOS / Sailfish OS / Raspberry Pi / AsteroidOS / Ubuntu Touch / JavaScript / WebAssembly

1,234

Use C++ libraries from Rust

Quick Overview

Ritual is a tool for generating Rust bindings to C++ libraries, with a focus on Qt. It automates the process of creating Rust wrappers for C++ APIs, making it easier to use C++ libraries in Rust projects. Ritual aims to simplify the integration of Qt and other C++ libraries into Rust applications.

Pros

  • Automates the generation of Rust bindings for C++ libraries
  • Specifically designed to work well with Qt, a popular C++ framework
  • Reduces the manual effort required to create and maintain Rust wrappers
  • Supports complex C++ features like templates and operator overloading

Cons

  • Still in development and may have limitations or bugs
  • Requires C++ build tools and libraries to be installed
  • May not handle all C++ constructs perfectly, potentially requiring manual adjustments
  • Documentation could be more comprehensive for advanced use cases

Code Examples

  1. Generating bindings for a C++ library:
use ritual::cpp_to_rust;

fn main() {
    cpp_to_rust::run(cpp_to_rust::Config {
        crate_name: "my_cpp_lib".to_string(),
        cpp_lib_name: "MyCppLib".to_string(),
        ..Default::default()
    }).unwrap();
}
  1. Using generated bindings in a Rust project:
use my_cpp_lib::MyClass;

fn main() {
    let obj = MyClass::new();
    obj.do_something();
}
  1. Customizing binding generation:
use ritual::cpp_to_rust;

fn main() {
    cpp_to_rust::run(cpp_to_rust::Config {
        crate_name: "custom_lib".to_string(),
        cpp_lib_name: "CustomLib".to_string(),
        include_dirs: vec!["path/to/include".to_string()],
        target_include_dirs: vec!["path/to/target/include".to_string()],
        ..Default::default()
    }).unwrap();
}

Getting Started

  1. Install Rust and C++ build tools
  2. Add ritual to your Cargo.toml:
    [dependencies]
    ritual = "0.4"
    
  3. Create a build script (build.rs) to generate bindings:
    use ritual::cpp_to_rust;
    
    fn main() {
        cpp_to_rust::run(cpp_to_rust::Config {
            crate_name: "your_cpp_lib".to_string(),
            cpp_lib_name: "YourCppLib".to_string(),
            ..Default::default()
        }).unwrap();
    }
    
  4. Use the generated bindings in your Rust code

Competitor Comparisons

10,376

Qt binding for Go (Golang) with support for Windows / macOS / Linux / FreeBSD / Android / iOS / Sailfish OS / Raspberry Pi / AsteroidOS / Ubuntu Touch / JavaScript / WebAssembly

Pros of qt

  • More mature and actively maintained project with frequent updates
  • Supports a wider range of Qt modules and versions
  • Offers better cross-platform compatibility (Windows, macOS, Linux, Android, iOS)

Cons of qt

  • Larger codebase and potentially steeper learning curve
  • May have more dependencies and complexity for simple projects
  • Written in Go, which might not be preferred by some developers

Code Comparison

ritual (Rust):

#[derive(ritual_derive::CppClass)]
struct QWidget {
    #[ritual(constructor)]
    fn new() -> Self;

    #[ritual(method)]
    fn show(&self);
}

qt (Go):

type QWidget struct {
    ptr unsafe.Pointer
}

func NewQWidget(parent QWidgetInterface) *QWidget {
    return NewQWidgetWithFlags(parent, 0)
}

func (ptr *QWidget) Show() {
    C.QWidget_Show(ptr.Pointer())
}

Summary

Both ritual and qt aim to provide Qt bindings for different programming languages. ritual focuses on Rust integration, while qt targets Go developers. qt offers broader Qt support and cross-platform compatibility, but may be more complex for simple projects. ritual provides a more Rust-idiomatic approach with potentially easier integration for Rust developers, but has more limited Qt module support.

1,234

Use C++ libraries from Rust

Pros of ritual

  • More recent commits and active development
  • Larger community engagement with more stars and forks
  • Better documentation and examples provided

Cons of ritual

  • Potentially less stable due to ongoing changes
  • May have more complex setup process for new users
  • Could introduce breaking changes more frequently

Code Comparison

ritual:

#[ritual_auto_bindings]
pub mod qt_core {
    include!("qt_core.rs");
}

ritual>:

#[cpp_class(name = "QObject")]
pub struct QObject {
    // ...
}

Both projects aim to provide Rust bindings for Qt, but ritual seems to be more actively maintained and has a larger community. However, this could lead to more frequent changes and potential instability. ritual> might offer a more stable, albeit potentially outdated, solution.

The code snippets show different approaches to binding generation. ritual uses a macro-based approach with auto-generated bindings, while ritual> appears to use a more manual, struct-based approach for defining Qt classes in Rust.

Ultimately, the choice between the two depends on the specific needs of the project, with ritual being better suited for those who want the latest features and are comfortable with potential changes, while ritual> might be preferable for projects requiring more stability.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

ritual

Build Status

ritual allows to use C++ libraries from Rust. It analyzes the C++ API of a library and generates a fully-featured crate that provides convenient (but still unsafe) access to this API.

The main motivation for this project is to provide access to Qt from Rust. Ritual provides large amount of automation, supports incremental runs, and implements compatible API evolution. This is mostly dictated by the huge size of API provided by Qt and significant API differences between Qt versions. However, ritual is designed to be universal and can also be used to easily create bindings for other C++ libraries.

More information is available on rust-qt.github.io:

License

This project is licensed under either of

at your option.

If you use Qt, you should also take into account Qt licensing.

Contributing

Contributions are always welcome! You can contribute in different ways:

  • Submit a bug report, a feature request, or an improvement suggestion at the issue tracker;
  • Write a test or an example for a Qt crate (porting examples from the official Qt documentation is a good option);
  • Pick up an issue with help wanted tag.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.