FFI Symbol prompting for lifetime?

Hello,

I am using libloading and a dylib library. Inside my main.rs I am attempting to load the function Symbol for a function that passes a pointer to a specs::DispatcherBuilder

The function inside by dylib is declared as:

#[no_mangle]
pub fn register_plugin<'a, 'b>(
    dispatcherBuilder: *mut DispatcherBuilder<'a, 'b>,
) -> *mut DispatcherBuilder<'a, 'b> {
    let func: Symbol<fn(*mut DispatcherBuilder) -> *mut DispatcherBuilder> =
        unsafe { plugs.plugins[0].lib.get(b"register_plugin").unwrap() };

However, rustc is complaining about missing lifetimes:

error[E0106]: missing lifetime specifiers
   --> src/main.rs:105:57
    |
105 |     let func: Symbol<fn(*mut DispatcherBuilder) -> *mut DispatcherBuilder> =
    |                                                         ^^^^^^^^^^^^^^^^^ expected 2 lifetime parameters

If I try and add lifetime parameters inside the Symbol like I did in the dylib, I am told that I am using an unspecified lifetime:

error[E0261]: use of undeclared lifetime name `'a`
   --> src/main.rs:105:75
    |
105 |     let func: Symbol<fn(*mut DispatcherBuilder) -> *mut DispatcherBuilder<'a, 'b>> =
    |                                                                           ^^ undeclared lifetime

I am a total beginner and I clearly feel way in over my head. If there is a simpler solution to what I am trying to accomplish, I would appreciate any suggestions.

Thanks!

Lifetimes point to where the data is borrowed from. In your case you have 'a invented out of thin air, so it's not known where it came from.

Note that the original said that the returned value is borrowed from the input argument (by having same lifetime names on both):

fn(*mut DispatcherBuilder<'a, 'b>) -> *mut DispatcherBuilder<'a, 'b>;

and declared the names 'a, 'b exist on the outer function register_plugin<'a, 'b>, because thats where they were first used. If in your function they're used only in let func, you can delcare them there using a rather rare syntax:

let func: for<'a, 'b> fn(*mut DispatcherBuilder<'a, 'b>) -> *mut DispatcherBuilder<'a, 'b>;

but if the data related to DispatcherBuilder comes from your function's arguments, declare them in there.

2 Likes