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

Documentation: clarify cxx::bridge for std::vector<Type*> bindings #1301

Closed
pkarneliuk opened this issue Jun 23, 2023 · 1 comment
Closed

Comments

@pkarneliuk
Copy link

Describe the bug
Documentation (https://google.github.io/autocxx/workflow.html) does not explain how to implement the #[cxx::bridge] for passing vector of pointers to objects std::vector<Type*> from C++ to Rust. This is relatively common use-case.

To Reproduce
autocxx can not generate bindings for std::vector of pointers to objects, like std::vector<Type*>

instrument.hpp
// instrument.hpp
#pragma once
#include <vector>

struct Instrument {
    int id;
    Instrument(int i) : id{i} {}
    virtual ~Instrument() = default;
    virtual int get() const {
        return id;
    }

    static Instrument* create(int i) {
        return new Instrument(i);
    } 
};

std::vector<Instrument*> instrument_list() {
    std::vector<Instrument*> instruments = {
        Instrument::create(1),
        Instrument::create(2),
        Instrument::create(3)
    };
    return instruments;
}
lib.rs
// lib.rs
use autocxx::prelude::*;

include_cpp! {
    #include "instrument.hpp"
    generate!("Instrument")
    generate!("instrument_list")
}

Generated .rs contains:

# [doc = "autocxx bindings couldn't be generated: Type std::vector was parameterized over something complex which we don't yet support"] 
pub struct instrument_list ; 

Expected behavior
It would be great to provide some example of #[cxx::bridge] when C++ code pass std::vector<Type*> to Rust code and methods of Type can be called from Rust side.

Additional context
There is the unit test test_vector_of_pointers() for similar use-case, but there is no example how to write correct #[cxx::bridge] for passing vector of pointers to objects.

@adetaylor
Copy link
Collaborator

Fundamentally, autocxx is limited to the types supported by cxx, and I don't believe that a CxxVector<*const T> is supported by cxx.

About three years ago I tried to implement this but ran out of time - feel free to take over that work! Once it's supported in cxx, it should be easy enough to support in autocxx too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants