Skip to content

func! macro lifetime mismatch for generic functions #107

@ffguven

Description

@ffguven

Hi,

I'm trying to mock nix::unistd::execvp which has the function signature below:

pub fn execvp<S: AsRef<CStr>>(
    filename: &CStr,
    args: &[S],
) -> Result<Infallible>

when I use InjectorPP with the lines below:

injector.when_called(injectorpp::func!(nix::unistd::execvp, fn(&std::ffi::CStr, &[&std::ffi::CStr]) -> nix::Result<std::convert::Infallible>))
        .will_execute(injectorpp::fake!(
            func_type: fn(_cmd: &std::ffi::CStr, _args: &[&std::ffi::CStr]) -> nix::Result<std::convert::Infallible>,
            returns: Err(nix::errno::Errno::E2BIG)
        )
);

I'm getting the compile time error:

error[E0308]: mismatched types
   --> src/linux_util/namespace/mod.rs:221:48
    |
221 |         injector.when_called(injectorpp::func!(nix::unistd::execvp, fn(&std::ffi::CStr, &[&std::ffi::CStr]) -> nix::Result<std::convert::Infallible>))
    |                                                ^^^^^^^^^^^^^^^^^^^  -------------------------------------------------------------------------------- expected due to this
    |                                                |
    |                                                one type is more general than the other
    |
    = note: expected fn pointer `for<'a, 'b, 'c> fn(&'a CStr, &'b [&'c CStr]) -> Result<_, _>`
                  found fn item `for<'a, 'b> fn(&'a CStr, &'b [_]) -> Result<_, _> {nix::unistd::execvp::<_>}`

My questions are:

  • I couldn't manage to avoid the lifetime mismatch between my mock signature and the real execvp signature. I don't see a way to define the lifetimes for the parameters within the func! macro. Is there any ways to achieve this?
  • I tried to use &'static lifetime for all the parameters in the mock signature to match the lifetime expectations and it helped me to avoid the compile time failure but the injector didn't catch the call to execvp at all.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions