Another lifetime question

⚓ Rust    📅 2025-12-20    👤 surdeus    👁️ 6      

surdeus

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Another lifetime question

My prior post asked for help on lifetimes. I am still stuck. I have

pub struct DynCaller {
    libs: HashMap<String, DynamicLibrary>,
}

with this method

pub fn define_function_by_str(&mut self, funcdef: &str) -> Result<FuncDef<'_>> {
...
}

and

pub struct FuncDef<'argval> {
    cif: ffi_cif,
    entry_point: unsafe extern "C" fn(),
    ffi_arg_types: Vec<*mut ffi_type>,
    ffi_return_type: ffi_type,
    pub(crate) arg_types: Vec<ArgType>,
    return_type: ArgType,
    pub(crate) arg_ptrs: Vec<*mut c_void>,
    pub(crate) arg_vals: Vec<ArgVal<'argval>>,
}

I had to add the lifetimes because I needed to store references in arg_vals. The problem is now that this (which worked before adding lifetimes)

        let mut dyncaller = DynCaller::new();
        let mut fopen = dyncaller
            .define_function_by_str("msvcrt.dll|fopen|cstr,cstr|ptr")
            .unwrap();

        let mut fread = dyncaller
            .define_function_by_str("msvcrt.dll|fread|ocstr,i32,i32,ptr|i32")
            .unwrap();

I get

error[E0499]: cannot borrow `dyncaller` as mutable more than once at a time
   --> src\test.rs:167:25
    |
163 |         let mut fopen = dyncaller
    |                         --------- first mutable borrow occurs here
...
167 |         let mut fread = dyncaller
    |                         ^^^^^^^^^ second mutable borrow occurs here

This I do not get at all. the return values from the define call are not holding onto the DynCaller at all.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed