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

Static member functions #1419

Open
JCBurnside opened this issue Dec 30, 2024 · 2 comments · May be fixed by #1430
Open

Static member functions #1419

JCBurnside opened this issue Dec 30, 2024 · 2 comments · May be fixed by #1430

Comments

@JCBurnside
Copy link

JCBurnside commented Dec 30, 2024

is there a way to declare a c++ opaque type with a static member? then have it translate to an associated function on the rust side? perhaps something like

#[cxx::bridge]
mod ffi {
    extern "C++" {
        type OpaqueType;
        impl OpaqueType {
            fn assoc_fun() -> RT;
        }
    }
}

with the c++ like

struct OpaqueType {
    // fields
    static RT assoc_fun();
};
@binarybaron
Copy link

What is the current workaround for this currently?

// Inside namespace named "Monero"
struct WalletManagerFactory
{
    // logging levels for underlying library
    enum LogLevel {
        LogLevel_Silent = -1,
        LogLevel_0 = 0,
        LogLevel_1 = 1,
        LogLevel_2 = 2,
        LogLevel_3 = 3,
        LogLevel_4 = 4,
        LogLevel_Min = LogLevel_Silent,
        LogLevel_Max = LogLevel_4
    };

    static WalletManager * getWalletManager();
    static void setLogLevel(int level);
    static void setLogCategories(const std::string &categories);
};

I want to call getWalletManager(). What does my `bridge.rs´ have to look like?

#[cxx::bridge(namespace = "Monero")]
pub mod ffi {
    unsafe extern "C++" {
        include!("monero-wrapper/monero/src/wallet/api/wallet2_api.h");

        type WalletManager;
        type WalletManagerFactory;

        #[namespace = "Monero::WalletManagerFactory"]
        fn getWalletManager() -> *mut WalletManager;
    }
}

@ulchie
Copy link

ulchie commented Jan 28, 2025

Workaround is here: #464.

Basically, create a freestanding function that acts as a shim.

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

Successfully merging a pull request may close this issue.

3 participants