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

feat: support Option<fn()> in WrapperApi #4

Merged

Conversation

zitsen
Copy link

@zitsen zitsen commented Jul 5, 2023

use dlopen2::wrapper::{Container, WrapperApi};
use std::os::raw::c_int;

struct Api<'a> {
    c_fun: unsafe extern "C" fn(),
    // A function may not exist in the library.
    c_option_fun: Option<unsafe extern "C" fn() -> c_int>,
}

fn main(){
    let mut cont: Container<Api> =
        unsafe { Container::load("libexample.so") }.expect("Could not open library or load symbols");
    unsafe{ cont.c_fun() };

    if cont.has_c_option_fun() {
      // do something
    }
    // option function returns Option<fn_return_type>, it's Option<c_int> here.
    unsafe{ cont.c_option_fun().map(|i| i == 0) };
}

```rust
use dlopen2::wrapper::{Container, WrapperApi};
use std::os::raw::c_int;

struct Api<'a> {
    c_fun: unsafe extern "C" fn(),
    // A function may not exist in the library.
    c_option_fun: Option<unsafe extern "C" fn() -> c_int>,
}

fn main(){
    let mut cont: Container<Api> =
        unsafe { Container::load("libexample.so") }.expect("Could not open library or load symbols");
    unsafe{ cont.c_fun() };

    if cont.has_c_option_fun() {
      // do something
    }
    // option function returns Option<fn_return_type>, it's Option<c_int> here.
    unsafe{ cont.c_option_fun().map(|i| i == 0) };
}
```
@zitsen zitsen force-pushed the feat/support-option-fn-in-wrapper-api branch from ce50672 to 743e83e Compare July 5, 2023 13:54
@zitsen
Copy link
Author

zitsen commented Jul 5, 2023

We use dlopen2 to load multiple versions of libraries. dlopen2_allow_null is helpful, but fn pointer is a bit more complex to use. Currently we use Option fn and extract each fn mannually with dlopen2 raw api. It's better to support Option types in WrapperApi derive impl.

Hope it can be accepted. Please have a look @OpenByteDev .

@OpenByteDev OpenByteDev added the enhancement New feature or request label Jul 29, 2023
@OpenByteDev OpenByteDev merged commit b561556 into OpenByteDev:master Jul 29, 2023
16 checks passed
@OpenByteDev
Copy link
Owner

Published in 0.6.0

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

Successfully merging this pull request may close these issues.

None yet

2 participants