Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to import Rust and C++ struct as .slint struct #1726

Open
ogoffart opened this issue Oct 12, 2022 · 7 comments
Open

Ability to import Rust and C++ struct as .slint struct #1726

ogoffart opened this issue Oct 12, 2022 · 7 comments
Labels
a:language-slint Compiler for the .slint language (mO,bF) enhancement New feature or request roadmap Tickets that we can use for roadmapping

Comments

@ogoffart
Copy link
Member

Something like (in slint)

import { MyNamespace::MyStruct as MyStruct } form "../include/some_file.h";
import { crate::foo::bar::SomeOtherStruct as SomeStruct } from "../some/module/foo.rs";

The slint compiler would use the syn crate to parse the rust file, or some simple C++ parser to parse the header file.

The struct themselves needs to be Clone, Default, Eq (and equivalent in C++) and only have public fields of type that are understood by Slint (without typedef or use or stuff, as the parser will stay simple)

@ogoffart ogoffart added enhancement New feature or request a:compiler Slint compiler internal (not the codegen, not the parser) a:language-c++ C++ API, codegen, CMake build system (mS,mO) a:language-rust Rust API and codegen (mO,mS) roadmap Tickets that we can use for roadmapping labels Oct 12, 2022
@Sintrastes
Copy link

This would be great for instance when I want to derive Serialize and Deserialize for a struct that I'm using in a slint file.

@xiaodusx1
Copy link

It's useful,can use it by slint 0.3.1?

@aamer-shaikh
Copy link

How will this work with interpreted Slint code ?

@ogoffart
Copy link
Member Author

ogoffart commented Feb 3, 2023

The idea is that the compiler will load and parse the C++/Rust file and extract the struct so that it works also in the preview/interpreter/...

@ogoffart
Copy link
Member Author

ogoffart commented Feb 3, 2023

For rust, we can use the syn library to parse the code, that should be easy. But that also mean quite a big dependency for the js or wasm things that might not need it, so we should have it optional.

For C++, i don't know if there is a library that does this already that we can use. Maybe we'd be good with a simple lexer and find the struct Foo { tokens or similar that don't care about C++ macros, or C++ concepts more than just structs with members.

@ogoffart ogoffart self-assigned this Apr 4, 2023
@erikjv
Copy link

erikjv commented Apr 6, 2023

You could also use libclang rust bindings (like https://github.com/KyleMayes/clang-sys ) to do this.

@ogoffart ogoffart mentioned this issue May 15, 2023
@ogoffart ogoffart modified the milestones: 1.1, 1.2 Jun 20, 2023
@ogoffart ogoffart removed their assignment Aug 17, 2023
@ogoffart ogoffart modified the milestones: 1.2, 1.3 Aug 17, 2023
@ogoffart ogoffart added a:language-slint Compiler for the .slint language (mO,bF) and removed a:compiler Slint compiler internal (not the codegen, not the parser) a:language-c++ C++ API, codegen, CMake build system (mS,mO) a:language-rust Rust API and codegen (mO,mS) labels Jan 16, 2024
@sinkyl
Copy link

sinkyl commented Apr 19, 2024

HI!
Here's my use case, and I can't wait for you guys to implement a solution for this.
I have a project structure like this one (repo)

/mod-template
│
├── crates/
│   ├── project/
│   │    ├──src/
│   │    │   └── main.rs
│   │    └── Cargo.toml
│   │
│   └── project-lib/
│       ├── src/
│       │   ├── components/
│       │   │   ├── comp1/
│       │   │   │   └── struct1.rs
│       │   │   ├── comp2/
│       │   │   │   └── struct2.rs
│       │   │   ├── comp1.rs
│       │   │   └── comp2.rs
│       │   ├── slint/
│       │   │   ├── building_blocks/
│       │   │   │   └── block1.rs
│       │   │   ├── widgets/
│       │   │   │   └── widget1.rs
│       │   │   ├── building_blocks.rs
│       │   │   └── widgets.rs
│       │   ├── components.rs
│       │   ├── slint.rs
│       │   ├── lib.rs
│       │   ├── main.rs
│       │   └── prelude.rs
│       ├── build.rs
│       └── Cargo.toml
├── Cargo.toml
└── Cargo.lock

Where I have two crates: "project" which contains the application itself and "project-lib" which is a library that holds the business logic and the UI widgets in Slint (slint macro and/or .slint file).

In the Slint/ directory, within slint macro or .slint file I can't use the business logic objects/structs. As you said, it will be awesome to import those structs as .slint structs. Also, accessing the methods of a struct so we can bind/alias a callback to an actual business logic function or just call that struct method:

import { crate::components::{Comp1, Comp2} } from "../some/module/foo.rs"; // not sure about the "from ..."

export component Example inherits Rectangle {
    // declare a callback
    property <Comp1> my_component;
    callback hello <=> my_component.print_text("my comp1")
   // or
   // callback hello <=> my_component::print_text("my comp1")

    area := TouchArea {
        // sets a handler with `=>`
        clicked => {
            // emit the callback
            root.hello()
        }
    }
}

Also, being able to cast structs, something like this:

struct TempStruct {
    prop1,
    prop2,
}

import { crate::components::{Comp1, Comp2} } from "../some/module/foo.rs"; // not sure about the "from ..."
// let say Comp1 has 6 properties

export component Example inherits Rectangle {
    proprety<{prop1: string, prop2: Comp2}>  simplified_obj: Comp1
    
    // do something  with the simplified object
}

For the above, I'm not sure about the syntax but I hope the idea is clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:language-slint Compiler for the .slint language (mO,bF) enhancement New feature or request roadmap Tickets that we can use for roadmapping
Projects
None yet
Development

No branches or pull requests

6 participants